-
Notifications
You must be signed in to change notification settings - Fork 0
240 lines (210 loc) · 8.09 KB
/
finalize-pr.yml
File metadata and controls
240 lines (210 loc) · 8.09 KB
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
235
236
237
238
239
240
name: finalize pr
concurrency: staging
on:
workflow_run:
workflows:
- 'validate pr'
types:
- completed
permissions:
contents: read
pull-requests: write
pages: write
id-token: write
defaults:
run:
shell: bash
jobs:
preconditions:
# this workflow should only run against the original repo
if: |
github.repository == 'lexical-cloud/lexical-cloud-data-hugo'
runs-on: ubuntu-22.04
steps:
- name: Verify artifacts
uses: actions/github-script@v3.1.0
with:
script: |
var artifacts = await github.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
const isHugoPagesArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == 'hugo-pages'
}).length > 0;
if(isHugoPagesArtifact) {
core.setFailed('found prebuilt github-pages artifact');
return;
}
const isPrDataArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == 'pr-data'
}).length == 1;
if(!isPrDataArtifact) {
core.setFailed('missing pr-data artifact');
return;
}
const isHugoStaticSiteArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "hugo-static-site"
}).length == 1;
if(!isHugoStaticSiteArtifact) {
core.setFailed('missing hugo-static-site artifact');
return;
}
- name: Precondition passed output
run: |
echo "Preconditions passed"
load-pr-data:
needs: preconditions
uses: ./.github/workflows/pr-artifact-to-outputs.yml
comment-on-stage:
needs:
- load-pr-data
runs-on: ubuntu-22.04
steps:
- uses: actions/github-script@v6
env:
PR_NUM: ${{ needs.load-pr-data.outputs.pr-number }}
RUN_NUM: ${{ github.run_number }}
with:
script: |
const { PR_NUM, RUN_NUM } = process.env
github.rest.issues.createComment({
issue_number: PR_NUM,
owner: context.repo.owner,
repo: context.repo.repo,
body: `
### Stage phase awaiting approval.
_(run ${RUN_NUM} of comment-on-stage in finalize-pr workflow)_
@lexical-cloud/admins will take these actions:
- [ ] Review artifacts generated for staging.
- [ ] Decide whether to accept changes for staging prep.
- [ ] If accepting, approve and monitor the preparation of staging.
Expect a response on this comment when actions are completed.`
})
stage:
needs: load-pr-data
runs-on: ubuntu-22.04
environment: staging
outputs:
static-site-sha: ${{ steps.save-static-site.outputs.commit-sha }}
steps:
- name: Checkout project with submodules
uses: actions/checkout@v3
with:
fetch-depth: 0
submodules: true
token: ${{ secrets.LC_DAT_CI }}
- name: Ensure artifacts downloaded to empty directory
run: |
if [ -f workflow-artifacts ] || [ -d workflow-artifacts ]; then
echo "workflow-artifacts already exist"
exit 1
fi
mkdir workflow-artifacts
- name: 'Download hugo-static-site artifact'
uses: actions/github-script@v3.1.0
with:
script: |
var artifacts = await github.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
var hugoStaticSiteArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == 'hugo-static-site'
})[0];
var download = await github.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: hugoStaticSiteArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{github.workspace}}/workflow-artifacts/hugo-static-site.zip', Buffer.from(download.data));
- name: Unzip hugo-static-site artifact
run: |
cd workflow-artifacts
unzip hugo-static-site.zip
if [ ! -f artifact.tar ]; then
echo "missing hugo-static-site artifact"
exit 1
fi
- name: Setup public submodule to capture changes in github-pages
id: public-submodule-setup
run: |
cd public
git checkout main
echo "main-sha=$(git rev-parse HEAD)" >> ${GITHUB_OUTPUT}
git branch deploy/hugo-pr-${PR_NUM}-${RUN_NUM}
git checkout deploy/hugo-pr-${PR_NUM} 2>/dev/null || git checkout -b deploy/hugo-pr-${PR_NUM}
echo "pr-sha=$(git rev-parse HEAD)" >> ${GITHUB_OUTPUT}
cd ..
rm -rf public/*
cd public
tar -xvf ../workflow-artifacts/artifact.tar
env:
PR_NUM: ${{ needs.load-pr-data.outputs.pr-number }}
RUN_NUM: ${{ github.run_number }}
- name: Preview changes to public submodule
id: preview-public-changes
run: |
cd public
git add .
git status
echo "diff-count=$(git status --porcelain | wc -l)" >> ${GITHUB_OUTPUT}
echo "main-diff-count=$(git diff --name-only main | wc -l)" >> ${GITHUB_OUTPUT}
- name: Save static site for future PR merge
id: save-static-site
if: |
steps.preview-public-changes.outputs.diff-count != '0' &&
steps.preview-public-changes.outputs.main-diff-count != '0'
run: |
cd public
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git commit -m "generated static site in pr ${PR_NUM} on lexical-cloud-data-hugo"
git checkout deploy/hugo-pr-${PR_NUM}-${RUN_NUM}
git merge --squash deploy/hugo-pr-${PR_NUM}
git commit -m "generated static site in pr ${PR_NUM} on lexical-cloud-data-hugo"
git push origin --force deploy/hugo-pr-${PR_NUM}-${RUN_NUM}:deploy/hugo-pr-${PR_NUM}
echo "commit-sha=$(git rev-parse HEAD)" >> ${GITHUB_OUTPUT}
env:
PR_NUM: ${{ needs.load-pr-data.outputs.pr-number }}
RUN_NUM: ${{ github.run_number }}
- name: Delete static site branch for pr if created and static site is now unchanged
if: |
steps.preview-public-changes.outputs.main-diff-count == '0' &&
steps.public-submodule-setup.outputs.main-sha != steps.public-submodule-setup.outputs.pr-sha
run: |
cd public
git push origin :deploy/hugo-pr-${PR_NUM}
env:
PR_NUM: ${{ needs.load-pr-data.outputs.pr-number }}
comment-on-save:
needs:
- load-pr-data
- stage
if: |
needs.stage.static-site-sha != ''
runs-on: ubuntu-22.04
steps:
- uses: actions/github-script@v6
env:
PR_NUM: ${{ needs.load-pr-data.outputs.pr-number }}
RUN_NUM: ${{ github.run_number }}
with:
script: |
const { PR_NUM, RUN_NUM } = process.env
github.rest.issues.createComment({
issue_number: PR_NUM,
owner: context.repo.owner,
repo: context.repo.repo,
body: `
### Stage phase requires further attention.
_(run ${RUN_NUM} of comment-on-save in finalize-pr workflow)_
@lexical-cloud/admins will take these actions:
- [ ] Create pr for deploy/hugo-pr-${PR_NUM} on lexical-cloud-data repo.
- [ ] Update public submodule commit to match deploy/hugo-pr-${PR_NUM} branch.
Expect a response on this comment when actions are completed.`
})