-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
234 lines (208 loc) · 8.71 KB
/
ecosystem-ci-trigger.yml
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
name: ecosystem-ci trigger
on:
issue_comment:
types: [created]
jobs:
trigger:
runs-on: ubuntu-latest
if: github.repository == 'vitejs/vite' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/ecosystem-ci run')
steps:
- name: Check User Permissions
uses: actions/github-script@v7
id: check-permissions
with:
script: |
const user = context.payload.sender.login
console.log(`Validate user: ${user}`)
const additionalAllowedUsers = ['lukastaegert']
let hasTriagePermission = false
try {
const { data } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: user,
});
hasTriagePermission = data.user.permissions.triage
} catch (e) {
console.warn(e)
}
if (hasTriagePermission || additionalAllowedUsers.includes(user)) {
console.log('User is allowed. Adding +1 reaction.')
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: '+1',
})
} else {
console.log('User is not allowed. Adding -1 reaction.')
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: '-1',
})
throw new Error('User does not have the necessary permissions.')
}
- name: Get PR Data
uses: actions/github-script@v7
id: get-pr-data
with:
script: |
console.log(`Get PR info: ${context.repo.owner}/${context.repo.repo}#${context.issue.number}`)
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
})
core.setOutput('head_sha', pr.head.sha)
return {
num: context.issue.number,
branchName: pr.head.ref,
commit: pr.head.sha,
repo: pr.head.repo.full_name
}
- name: Check Package Existence
uses: actions/github-script@v7
id: check-package
with:
script: |
const prData = ${{ steps.get-pr-data.outputs.result }}
const url = `https://pkg.pr.new/vite@${prData.commit}`
const response = await fetch(url)
console.log(`Package check URL: ${url}, Status: ${response.status}`)
// Add 'rocket' reaction to the issue comment
if (response.status === 404) {
const { data: reaction } = await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket',
})
return { exists: false, reaction: reaction.id }
}
return { exists: true, reaction: null }
- name: Generate Token
id: generate-token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.ECOSYSTEM_CI_GITHUB_APP_ID }}
installation_retrieval_payload: "${{ github.repository_owner }}/vite-ecosystem-ci"
private_key: ${{ secrets.ECOSYSTEM_CI_GITHUB_APP_PRIVATE_KEY }}
- name: Trigger Preview Release (if Package Not Found)
if: fromJSON(steps.check-package.outputs.result).exists == false
uses: actions/github-script@v7
id: trigger-preview-release
with:
github-token: ${{ steps.generate-token.outputs.token }}
script: |
const prData = ${{ steps.get-pr-data.outputs.result }}
console.log('Package not found, triggering preview release...')
// Add label "trigger: preview" to the PR
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prData.num,
labels: ['trigger: preview']
})
console.log('Added "trigger: preview" label.')
- name: Wait for Preview Release Completion (if Package Not Found)
if: fromJSON(steps.check-package.outputs.result).exists == false
uses: actions/github-script@v7
id: wait-preview-release
with:
script: |
const prData = ${{ steps.get-pr-data.outputs.result }}
const reaction = ${{ fromJSON(steps.check-package.outputs.result).reaction }}
const workflowFileName = 'preview-release.yml'
const workflow = await github.rest.actions.getWorkflow({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowFileName,
})
const workflowId = workflow.data.id
console.log(`Waiting for workflow ID ${workflowId} to complete...`)
const maxRetries = 60 // Wait up to 10 minutes
const delay = 10000 // 10 seconds
let completed = false
for (let i = 0; i < maxRetries; i++) {
const runsData = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId,
head_sha: prData.commit,
per_page: 100,
page: 1,
})
const runs = runsData.data.workflow_runs
if (runs.length > 0) {
const latestRun = runs[0]
console.log(`Latest run status: ${latestRun.status}, conclusion: ${latestRun.conclusion}`)
if (latestRun.status === 'completed') {
if (latestRun.conclusion === 'success') {
console.log('Preview release workflow completed successfully.')
completed = true
break
} else if (latestRun.conclusion === 'skipped') {
// noop
} else {
throw new Error('Preview Release workflow failed.')
}
}
}
console.log(`Retrying... (${i + 1}/${maxRetries})`)
await new Promise(resolve => setTimeout(resolve, delay))
}
if (!completed) {
throw new Error('Preview Release workflow did not complete in time.')
}
// Remove the 'rocket' reaction
if (reaction) {
await github.rest.reactions.deleteForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
reaction_id: reaction,
})
console.log('Removed "rocket" reaction.')
}
- name: Checkout
uses: actions/checkout@v4
with:
ref: refs/pull/${{ fromJSON(steps.get-pr-data.outputs.result).num }}/head
fetch-depth: 0
# This step can be removed on May 26 2025
- name: Check Commit Hash Ambiguity
id: check_ambiguity
run: |
HEAD_SHA=${{ steps.get-pr-data.outputs.head_sha }}
COMMIT_SHORT=${HEAD_SHA:0:7}
if git show "$COMMIT_SHORT"; then
echo "COLLISION=false" >> $GITHUB_ENV
else
echo "COLLISION=true" >> $GITHUB_ENV
fi
- name: Trigger Downstream Workflow
uses: actions/github-script@v7
id: trigger
env:
COMMENT: ${{ github.event.comment.body }}
with:
github-token: ${{ steps.generate-token.outputs.token }}
script: |
const comment = process.env.COMMENT.trim()
const prData = ${{ steps.get-pr-data.outputs.result }}
const suite = comment.split('\n')[0].replace(/^\/ecosystem-ci run/, '').trim()
await github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: 'vite-ecosystem-ci',
workflow_id: 'ecosystem-ci-from-pr.yml',
ref: 'main',
inputs: {
prNumber: '' + prData.num,
branchName: prData.branchName,
repo: prData.repo,
commit: process.env.COLLISION === 'false' ? prData.commit : '',
suite: suite === '' ? '-' : suite
}
})