-
Notifications
You must be signed in to change notification settings - Fork 0
226 lines (199 loc) · 7.56 KB
/
main.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
name: Build, Deploy, and Index site
on:
push:
branches:
- 'staging'
- 'main'
pull_request:
branches:
- 'staging'
- 'main'
concurrency:
group: ${{ format('{0}-{1}', github.job, github.ref) }}
cancel-in-progress: true
jobs:
build:
name: Build the site
runs-on: ubuntu-latest
steps:
- name: Token configuration (PAT available)
if: github.ref_name == 'main' || github.ref_name == 'staging'
run: |
echo "PAT=${{ secrets.IMAGE_BOT_PAT }}" >> $GITHUB_ENV
- name: Token configuration (PAT unavailable)
if: github.ref_name != 'main' && github.ref_name != 'staging'
run: |
echo "PAT=${{ github.token }}" >> $GITHUB_ENV
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ env.PAT }}
- name: Staging site configuration
if: github.ref_name != 'main'
run: |
echo "XARGS=--drafts" >> $GITHUB_ENV
echo 'User-agent: * Disallow: /' > ${{ github.workspace }}/robots.txt
sed -i 's_blog.sebsscholarship.org_test.blog.sebsscholarship.org_g' ${{ github.workspace }}/_config.yml
- name: Load Gem Cache
uses: actions/cache/restore@v4
id: gem-cache
with:
key: gem-cache-${{ hashFiles('Gemfile.lock') }}
path: _vendor/bundle
restore-keys: gem-cache-
- name: Build site
run: |
docker run -v ${{ github.workspace }}:/srv/jekyll -e JEKYLL_UID=1001 -e JEKYLL_GID=1001 \
jekyll/builder:4.2.2 /bin/bash -c \
"apk add --no-cache imagemagick wget \
&& wget 'https://api.fontsource.org/v1/download/montserrat' -O font.zip \
&& unzip font.zip -d /usr/share/fonts/googlefonts/ \
&& rm font.zip \
&& bundle config --local path '_vendor/bundle' \
&& bundle install \
&& chmod 777 /srv/jekyll \
&& bundle exec jekyll build --future ${{ env.XARGS }}"
- name: Save Gems to Cache
uses: actions/cache/save@v4
if: steps.gem-cache.outputs.cache-hit != 'true'
with:
key: ${{ steps.gem-cache.outputs.cache-primary-key }}
path: _vendor/bundle
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: Built Site
path: ${{ github.workspace }}/_site/
- name: GitHub Pages Artifact
uses: actions/upload-pages-artifact@v3
if: github.ref_name == 'main'
- name: Commit images
if: github.ref_name == 'main' || github.ref_name == 'staging'
run: |
git config core.fileMode false
git config --global user.name 'SSF Image Bot'
git config --global user.email '[email protected]'
git add assets/images/
((git commit -m "bot: Add newly generated images" && git push) || true)
deploy-staging:
name: Deploy the staging site
runs-on: ubuntu-latest
needs:
- build
if: github.ref_name == 'staging'
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: Built Site
path: _site/
- name: Install SSH key
if: github.ref_name == 'staging'
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Trust SSH key
if: github.ref_name == 'staging'
run: ssh-keyscan sebsscholarship.org > ~/.ssh/known_hosts
- name: Deploy Staging
if: github.ref_name == 'staging'
run: |
scp -r ${{ github.workspace }}/_site/* ${{ secrets.USERNAME }}@sebsscholarship.org:~/staging/
ssh ${{ secrets.USERNAME }}@sebsscholarship.org 'rm -rf ~/test.blog.sebsscholarship.org/* \
&& mv ~/staging/* ~/test.blog.sebsscholarship.org/'
deploy-prod:
name: Deploy the production site
runs-on: ubuntu-latest
needs:
- build
if: github.ref_name == 'main'
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
index:
name: Index the site
runs-on: ubuntu-latest
needs:
- deploy-staging
- deploy-prod
# Only index if deploy completes successfully
if: always() && (needs.deploy-staging.result == 'success' || needs.deploy-prod.result == 'success')
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Staging site configuration
if: github.ref_name != 'main'
run: |
echo "XARGS=--drafts" >> $GITHUB_ENV
echo 'User-agent: * Disallow: /' > ${{ github.workspace }}/robots.txt
sed -i 's_blog.sebsscholarship.org_test.blog.sebsscholarship.org_g' ${{ github.workspace }}/_config.yml
- name: Load Gem Cache
uses: actions/cache/restore@v4
id: gem-cache
with:
key: gem-cache-${{ hashFiles('Gemfile.lock') }}
path: _vendor/bundle
restore-keys: gem-cache-
- name: Index site
run: |
docker run -v ${{ github.workspace }}:/srv/jekyll -e JEKYLL_UID=1001 -e JEKYLL_GID=1001 \
jekyll/builder:4.2.2 /bin/bash -c \
"bundle config --local path '_vendor/bundle' \
&& bundle install \
&& chmod 777 /srv/jekyll \
&& ALGOLIA_API_KEY='${{ secrets.ALGOLIA_KEY }}' bundle exec jekyll algolia --future ${{ env.XARGS }}"
notify:
name: Send job complete notification
runs-on: ubuntu-latest
needs:
- build
- deploy-staging
- deploy-prod
- index
# Run if on staging or main
if: always() && (github.ref_name == 'staging' || github.ref_name == 'main')
steps:
- name: Set test environment
if: github.ref_name == 'staging'
run: |
echo "SITE_NAME=test" >> $GITHUB_ENV
echo "SITE_ADDR=https://test.blog.sebsscholarship.org" >> $GITHUB_ENV
- name: Set main environment
if: github.ref_name == 'main'
run: |
echo "SITE_NAME=main" >> $GITHUB_ENV
echo "SITE_ADDR=https://blog.sebsscholarship.org" >> $GITHUB_ENV
- name: Notify on success
# If the site was successfully built and indexed and deployed if staging
if: needs.build.result == 'success' && (needs.deploy-staging.result == 'success' || needs.deploy-prod.result == 'success') && needs.index.result == 'success'
uses: appleboy/[email protected]
with:
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#800000"
username: "SSF Blog Status Bot"
message: >
Updates were successfully pushed to ${{ env.SITE_NAME }} blog:
${{ env.SITE_ADDR }}
- name: Notify on failure
# If the site failed at any point
if: needs.build.result == 'failure' || needs.deploy-staging.result == 'failure' || needs.deploy-prod.result == 'failure' || needs.index.result == 'failure'
uses: appleboy/[email protected]
with:
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#800000"
username: "SSF Blog Status Bot"
message: >
Aborting ${{ env.SITE_NAME }} blog deployment due to unexpected error:
https://github.com/sebs-scholarship/Blog/actions/runs/${{ github.run_id }}