-
Notifications
You must be signed in to change notification settings - Fork 48.3k
/
Copy pathpublish-using-ci-workflow.js
148 lines (133 loc) · 3.99 KB
/
publish-using-ci-workflow.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
'use strict';
const fetch = require('node-fetch');
const {logPromise} = require('./utils');
const theme = require('./theme');
const CIRCLE_TOKEN = process.env.CIRCLE_CI_API_TOKEN;
if (!CIRCLE_TOKEN) {
console.error(
theme.error(
'Missing required environment variable: CIRCLE_CI_API_TOKEN\n' +
'Grab it here: https://app.circleci.com/settings/user/tokens'
)
);
process.exit(1);
}
function sleep(ms) {
return new Promise(resolve => {
setTimeout(() => resolve(), ms);
});
}
async function getPublishWorkflowID(pipelineID) {
// Since we just created the pipeline in a POST request, the server may 404.
// Try a few times before giving up.
for (let i = 0; i < 20; i++) {
const pipelineWorkflowsResponse = await fetch(
`https://circleci.com/api/v2/pipeline/${pipelineID}/workflow`
);
if (pipelineWorkflowsResponse.ok) {
const pipelineWorkflowsJSON = await pipelineWorkflowsResponse.json();
const workflows = pipelineWorkflowsJSON.items;
if (workflows.length !== 0) {
return workflows[0].id;
}
}
// CircleCI server may be stale. Wait a sec and try again.
await sleep(1000);
}
return null;
}
async function pollUntilWorkflowFinishes(workflowID) {
while (true) {
const workflowResponse = await fetch(
`https://circleci.com/api/v2/workflow/${workflowID}`
);
const workflow = await workflowResponse.json();
switch (workflow.status) {
case 'running':
// Workflow still running. Wait a bit then check again.
await sleep(2000);
continue;
case 'success':
// Publish succeeded! Continue.
return;
case 'not_run':
case 'failed':
case 'error':
case 'failing':
case 'on_hold':
case 'canceled':
case 'unauthorized':
default:
console.error(
theme.error(
`Failed to publish. Workflow exited with status: ${workflow.status}`
)
);
console.error(
`Visit https://app.circleci.com/pipelines/workflows/${workflowID} for details.`
);
process.exit(1);
break;
}
}
}
async function main() {
const headCommitResponse = await fetch(
'https://api.github.com/repos/facebook/react/commits/main'
);
const headCommitJSON = await headCommitResponse.json();
const headCommitSha = headCommitJSON.sha;
const pipelineResponse = await fetch(
'https://circleci.com/api/v2/project/github/facebook/react/pipeline',
{
method: 'post',
body: JSON.stringify({
parameters: {
prerelease_commit_sha: headCommitSha,
},
}),
headers: {
'Circle-Token': CIRCLE_TOKEN,
'Content-Type': 'application/json',
},
}
);
if (!pipelineResponse.ok) {
console.error(
theme.error(
`Failed to access CircleCI. Responded with status: ${pipelineResponse.status}`
)
);
process.exit(1);
}
const pipelineJSON = await pipelineResponse.json();
const pipelineID = pipelineJSON.id;
const workflowID = await logPromise(
getPublishWorkflowID(pipelineID),
theme`{header Creating CI workflow}`,
2 * 1000 // Estimated time: 2 seconds,
);
if (workflowID === null) {
console.warn(
theme.yellow(
'Created a CI pipeline to publish the packages, but the script timed ' +
"out when requesting the associated workflow ID. It's still " +
'possible the workflow was created.\n\n' +
'Visit ' +
'https://app.circleci.com/pipelines/github/facebook/react?branch=main ' +
'for a list of the latest workflows.'
)
);
process.exit(1);
}
await logPromise(
pollUntilWorkflowFinishes(workflowID),
theme`{header Publishing in CI workflow}: https://app.circleci.com/pipelines/workflows/${workflowID}`,
2 * 60 * 1000 // Estimated time: 2 minutes,
);
}
main().catch(error => {
console.error(theme.error('Failed to trigger publish workflow.'));
console.error(error.message);
process.exit(1);
});