-
Notifications
You must be signed in to change notification settings - Fork 14
295 lines (249 loc) · 10.3 KB
/
deploy_to_github_pages.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
name: Deploy to GitHub Pages
on:
push: #Action fires anytime there is a push to the following branches
branches:
- main
- development
pull_request: #Action also fires anytime a PR is (re)opened, closed or synchronized
types:
- opened
- reopened
- synchronize
- closed
jobs:
pr-preview-setup:
# If the action is fired because of a PR, run this job
if: ${{ github.event_name == 'pull_request' }}
runs-on: ubuntu-latest
steps:
- name: Get date
run: echo "DATE=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV
- name: Setup pr-preview
# If the PR is not from the development branch and is new (or has been reopened),
# setup the message that will get updated with the URL after deployment
if: ${{ github.event.pull_request.head.ref != 'development' && github.event.action != 'closed' }}
uses: thollander/[email protected]
with:
comment_tag: pr-preview
pr_number: ${{ github.event.number }}
message: "\
PR preview
:---:
🛫 Deployment still ongoing.<br>
Preview URL will be available at the end of the deployment.
For more information, please check the [Actions](https://github.com/ACCESS-Hive/access-hive.github.io/actions) tab.
${{ env.DATE }}
"
- name: Setup development preview
# If the PR is from the development branch and is new (or has been reopened),
# setup the message that will get updated with the URL after deployment
if: ${{ github.event.pull_request.head.ref == 'development' && github.event.action != 'closed' }}
uses: thollander/[email protected]
with:
comment_tag: pr-preview
pr_number: ${{ github.event.number }}
message: "\
Development website preview
:---:
🛫 Deployment still ongoing.<br>
Preview URL will be available at the end of the deployment.
For more information, please check the [Actions](https://github.com/ACCESS-Hive/access-hive.github.io/actions) tab.
${{ env.DATE }}
"
# If the PR is closed, remove the pr-preview URL
- name: Remove pr-preview URL link
if: ${{github.event.action == 'closed' && github.event.pull_request.head.ref != 'development' }}
uses: thollander/[email protected]
with:
comment_tag: pr-preview
pr_number: ${{ github.event.number }}
message: "\
PR Preview
:---:
🛬 Link removed because the pull request was closed.
${{ env.DATE }}
"
# If the PR is closed, remove the development URL
- name: Remove development URL
if: ${{github.event.action == 'closed' && github.event.pull_request.head.ref == 'development' }}
uses: thollander/[email protected]
with:
comment_tag: pr-preview
pr_number: ${{ github.event.number }}
message: "\
Development website preview
:---:
🛬 Preview removed because the pull request was closed.
${{ env.DATE }}
"
build:
# Cancel any previous build/deploy jobs that are still running (no need to build/deploy multiple times)
concurrency:
group: build-deploy
cancel-in-progress: true
runs-on: ubuntu-latest
outputs:
pr_nums: ${{ steps.build.outputs.pr_nums }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Python setup
uses: actions/setup-python@v5
with:
python-version: 3.9.x
- name: Install dependencies
run: pip install -r requirements.txt
# Build full website using main, development and open PRs head branches
# (excluding `development` and `main` as PR head branches)
- name: Build full website
id: build
shell: bash
run: |
retry() {
command="$1"
n_tries="$2"
wait="$3"
exit_msg="$4"
eval "$command"
until [ $? == 0 ]; do
if [ $i -eq $((n_tries - 1)) ]; then
echo "$exit_msg"
exit 1
else
((i++))
fi
sleep $wait
eval "$command"
done
}
git fetch --all
echo "Build main website"
retry "git checkout main" 5 1 "Failed to checkout 'main'."
retry 'mkdocs build -f mkdocs.yml -d ../website' 5 1 "Failed to build main website."
echo "Build development website"
retry 'git checkout development' 5 1 "Failed to checkout 'development' branch."
retry 'mkdocs build -f mkdocs.yml -d ../website/development-website' 5 1 "Failed to build development website."
echo "Build PR websites"
command="pr_list=\$(curl -s https://api.github.com/repos/ACCESS-Hive/access-hive.github.io/pulls?state=opened \
| jq '.[] | select(.head.label!=\"ACCESS-Hive:development\" and .head.label!=\"ACCESS-Hive:main\")')"
retry "$command" 5 1 "Failed to fetch opened PRs."
pr_nums=($(jq '.number' <<< $pr_list))
if [[ -n $pr_nums ]]; then
echo "Found PR numbers: $(sed 's/\s/, /g' <<< ${pr_nums[@]})."
pr_sha=($(jq '.head.sha' <<< $pr_list))
echo "pr_nums=$(sed 's/\s/,/g' <<< [${pr_nums[@]}])" >> "$GITHUB_OUTPUT"
for i in ${!pr_nums[@]}; do
retry "git checkout ${pr_sha[i]}" 5 1 "Failed to checkout git hash '${pr_sha[i]}'."
retry "mkdocs build -f mkdocs.yml -d ../website/pr-preview/pr-${pr_nums[i]}" 5 1 "Failed to build pr-${pr_nums[i]} website."
done
else
echo "No open PR found."
fi
echo "Give the right file permissions"
chmod -c -R +rX ../website
- name: Create artifact for deployment to GitHub Pages
uses: actions/upload-pages-artifact@v2
with:
path: ../website
deploy:
needs: build
runs-on: ubuntu-latest
# Cancel any previous build/deploy jobs that are still running (no need to build/deploy multiple times)
concurrency:
group: build-deploy
cancel-in-progress: true
outputs:
success: ${{ steps.success.outputs.success }}
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source
steps:
- name: Deploy to GitHub Pages
uses: actions/deploy-pages@v2
- name: Output success status
id: success
run: |
echo "success=1" >> "$GITHUB_OUTPUT"
# Set pr-preview URL
pr-preview:
needs: [build,deploy]
# If there are open PRs (whose head branch is neither `development` nor `main`), run this job
if: ${{ needs.build.outputs.pr_nums }}
runs-on: ubuntu-latest
# Run the same job for each of the open PRs found
strategy:
matrix:
pr_nums: ${{fromJson(needs.build.outputs.pr_nums)}}
steps:
- name: Get date
run: echo "DATE=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV
- name: Set pr-preview URL
if: ${{github.event.action != 'closed'}}
uses: thollander/[email protected]
with:
comment_tag: pr-preview
pr_number: ${{ matrix.pr_nums }}
message: "\
PR Preview
:---:
🚀 Deployed preview to
https://access-hive.org.au/pr-preview/pr-${{ matrix.pr_nums }}
${{ env.DATE }}
"
# Add development URL
development-preview:
needs: [build,deploy]
# If there are open PRs (whose head branch is neither `development` nor `main`), run this job
if: ${{ github.event.pull_request.head.ref == 'development' }}
runs-on: ubuntu-latest
steps:
- name: Get date
run: echo "DATE=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV
- name: Add development-preview URL
if: ${{github.event.action != 'closed'}}
uses: thollander/[email protected]
with:
comment_tag: pr-preview
pr_number: ${{ github.event.number }}
message: "\
Development website preview
:---:
🚀 Development website deployed to
https://access-hive.org.au/development-website
${{ env.DATE }}
"
# Change preview message if deployment fails
failed-preview:
needs: deploy
# If the action failed (but was not cancelled) and was fired because of a pull request (not closed)
if: ${{ always() && '!cancelled()' && github.event_name == 'pull_request' && github.event.action != 'closed' && needs.deploy.outputs.success != '1' }}
runs-on: ubuntu-latest
steps:
- name: Get date
run: echo "DATE=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV
- name: Change development-preview message
if: ${{ github.event.pull_request.head.ref == 'development' }}
uses: thollander/[email protected]
with:
comment_tag: pr-preview
pr_number: ${{ github.event.number }}
message: "\
Development website preview
:---:
⚠️ There was an error in the deployment of the development website.
For more information, please check the [Actions](https://github.com/ACCESS-Hive/access-hive.github.io/actions) tab.
${{ env.DATE }}
"
- name: Change pr-preview message
if: ${{ github.event.pull_request.head.ref != 'development' }}
uses: thollander/[email protected]
with:
comment_tag: pr-preview
pr_number: ${{ github.event.number }}
message: "\
PR preview
:---:
⚠️ There was an error in the pr-preview deployment.
For more information, please check the [Actions](https://github.com/ACCESS-Hive/access-hive.github.io/actions) tab.
${{ env.DATE }}
"