-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrailway.ts
99 lines (88 loc) · 2.43 KB
/
railway.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import axios from "axios";
const RAILWAY_SERVICE_ID = process.env.RAILWAY_SERVICE_ID;
const RAILWAY_API_TOKEN = process.env.RAILWAY_API_TOKEN;
const RAILWAY_PROJECT_ID = process.env.RAILWAY_PROJECT_ID;
const RAILWAY_ENVIRONMENT_ID = process.env.RAILWAY_ENVIRONMENT_ID;
export async function getLatestDeployment() {
console.log("Getting latest deployment...");
// Use the exact query format provided but with proper authentication
const getDeploymentQuery = {
query: `
query deployments {
deployments(
first: 1
input: {
projectId: "${RAILWAY_PROJECT_ID}"
environmentId: "${RAILWAY_ENVIRONMENT_ID}"
serviceId: "${RAILWAY_SERVICE_ID}"
}
) {
edges {
node {
id
staticUrl
}
}
}
}
`,
};
// Try with the API token instead of the project token
const response = await axios.post(
"https://backboard.railway.com/graphql/v2", // Use .com like in the successful testConnection
getDeploymentQuery,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${RAILWAY_API_TOKEN}`,
},
},
);
console.log("Response:", response.data);
const deployment = response.data.data.deployments.edges[0]?.node;
return deployment as {
id: string;
staticUrl: string;
};
}
export async function redeployDeployment(deploymentId: string | undefined) {
console.log(`Redeploying deployment with ID: ${deploymentId}...`);
if (!RAILWAY_API_TOKEN) {
console.error(
"Error: RAILWAY_API_TOKEN is not set in your environment variables",
);
return null;
}
const redeployMutation = {
query: `
mutation deploymentRedeploy {
deploymentRedeploy(id: "${deploymentId}") {
id
status
staticUrl
}
}
`,
};
const response = await axios.post(
"https://backboard.railway.com/graphql/v2",
redeployMutation,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${RAILWAY_API_TOKEN}`,
},
},
);
console.log("Deployment redeploy successful!");
console.log("Response:", response.data);
const deployment = response.data.data.deploymentRedeploy;
if (!deployment) {
throw new Error("No deployment found");
}
return deployment as {
id: string;
status: string;
staticUrl: string;
};
}