-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (140 loc) · 4.55 KB
/
index.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const core = require("@actions/core");
const { Octokit } = require("@octokit/rest");
const fetch = require("node-fetch");
const DEPLOYMENT_SEARCH_INTERVAL = 5;
const DEPLOYMENT_READY_INTERVAL = 30;
function wait(s) {
return new Promise((resolve) => setTimeout(resolve, s * 1000));
}
async function main() {
const githubToken = process.env.GITHUB_TOKEN;
if (!githubToken) {
throw new Error(
"This action needs a GitHub token, you need to set the env `GITHUB_TOKEN`"
);
}
const vercelToken = process.env.VERCEL_TOKEN;
if (!vercelToken) {
throw new Error(
"This action needs a Vercel token, you need to set the env `VERCEL_TOKEN`"
);
}
const projectId = core.getInput("project-id", { required: true });
const teamId = core.getInput("team-id");
const searchRetries = parseInt(core.getInput("search-retries"), 10) || 3;
const readyRetries = parseInt(core.getInput("ready-retries"), 10) || 10;
const octokit = new Octokit({ auth: githubToken });
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
async function api(path) {
const res = await fetch(`https://api.vercel.com${path}`, {
headers: {
authorization: `Bearer ${vercelToken}`,
},
});
const json = await res.json();
if (!res.ok) {
console.error(json);
throw new Error(
"Something went wrong while trying to fetch deployments from the Vercel API, see the error above."
);
}
return json;
}
async function findLatestDeployment(commitSha, retries) {
if (typeof retries !== "number" || retries <= 0) {
return null;
}
console.log(`Searching for deployments related to commit ${commitSha}.`);
const { deployments } = await api(
`/v5/now/deployments?${
teamId ? `teamId=${teamId}&` : ""
}projectId=${projectId}&meta-githubCommitSha=${commitSha}`
);
if (Array.isArray(deployments) && deployments.length > 0) {
console.log(
`Found ${deployments.length} deployment${
deployments.length > 1 ? "s" : ""
}, using the latest one.`
);
return api(
`/v11/now/deployments/${deployments[0].uid}${
teamId ? `?teamId=${teamId}&` : ""
}`
);
}
console.log(
`No deployments found yet, waiting for ${DEPLOYMENT_SEARCH_INTERVAL} seconds before trying again (${retries} retries remaining)`
);
await wait(DEPLOYMENT_SEARCH_INTERVAL);
return findLatestDeployment(commitSha, retries - 1);
}
async function findDeployment(commitSha, numberOfRecursiveCalls) {
const deployment = await findLatestDeployment(
commitSha,
numberOfRecursiveCalls === 0 ? searchRetries : 1
);
if (deployment) {
return deployment;
}
try {
const commit = await octokit.repos.getCommit({
owner,
repo,
ref: commitSha,
});
if (commit.data.parents[1]) {
return findDeployment(
commit.data.parents[1].sha,
numberOfRecursiveCalls + 1
);
}
return null;
} catch (error) {
console.error(error);
return null;
}
}
async function waitForDeploymentToBeReady(deployment, retries) {
if (typeof retries !== "number" || retries <= 0) {
throw new Error(
"The Vercel deployment is still not ready after running out of retries."
);
}
switch (deployment.readyState) {
case "READY":
console.log(`The deployment is ready under ${deployment.url}.`);
return deployment;
case "ERROR":
throw new Error("The Vercel deployment did not succeed.");
case "QUEUED":
case "BUILDING":
default: {
console.log(
`The latest deployment is still in the '${deployment.readyState}' state, waiting for ${DEPLOYMENT_READY_INTERVAL} more seconds (${retries} retries remaining)`
);
await wait(DEPLOYMENT_READY_INTERVAL);
const updatedDeployment = await api(
`/v11/now/deployments/${deployment.id}${
teamId ? `?teamId=${teamId}&` : ""
}`
);
return waitForDeploymentToBeReady(updatedDeployment, retries - 1);
}
}
}
const commitSha = process.env.GITHUB_SHA;
const deployment = await findDeployment(commitSha, 0);
if (!deployment) {
throw new Error(
`Could not find any Vercel deployments for the commit with SHA ${commitSha}.`
);
}
const readyDeployment = await waitForDeploymentToBeReady(
deployment,
readyRetries
);
core.setOutput("url", readyDeployment.url);
}
main().catch((error) => {
core.setFailed(error.message);
});