-
Notifications
You must be signed in to change notification settings - Fork 7
188 lines (186 loc) · 8.08 KB
/
ci_hugo_build.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
name: CI
on:
push:
branches-ignore:
- 'l10n_**' # Push events to translation service branches (that begin with "l10n_")
- 'translation_integration' # Separate branch for translation-service-formatted files
- 'gh-pages'
pull_request:
# Match all pull requests
jobs:
build:
name: 'Test Hugo Build'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
path: master
- name: Install Hugo
run: . ./master/.ci/install_hugo.sh
- name: Build
working-directory: "${{ github.workspace }}/master"
run: |
# Set up output directory
OUTPUT_DIR="${HOME}/output/public"
echo "OUTPUT_DIR=${OUTPUT_DIR}"
echo "OUTPUT_DIR=${OUTPUT_DIR}" >> $GITHUB_ENV
mkdir -p "${OUTPUT_DIR}"
# Run Hugo build
hugo --minify --gc --printI18nWarnings --printPathWarnings --verbose --destination "${OUTPUT_DIR}"
deploy:
concurrency: serialize-pages-deploy
name: 'Deploy to GitHub Pages'
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v4
with:
ref: master
path: master
persist-credentials: false
- name: Checkout gh-pages branch
uses: actions/checkout@v4
with:
ref: gh-pages
path: gh-pages
persist-credentials: false
- name: Clean gh-pages branch
working-directory: "${{ github.workspace }}/gh-pages"
run: |
BACKUP_GH_PAGES_FILES="${GITHUB_WORKSPACE}/temp/backup_gh_pages_files"
mkdir -p "${BACKUP_GH_PAGES_FILES}"
# Copy CNAME file, if it exists, temporarily elsewhere
if [ -f "CNAME" ]; then
cp "CNAME" "${BACKUP_GH_PAGES_FILES}/CNAME"
fi
# Copy _config.yml file, if it exists, temporarily elsewhere
if [ -f "_config.yml" ]; then
cp "_config.yml" "${BACKUP_GH_PAGES_FILES}/_config.yml"
fi
# Clean the gh-pages branch
git rm --ignore-unmatch -rf .
git clean -fxd
# Restore saved files
rsync -r "${BACKUP_GH_PAGES_FILES}/" .
- name: Install Hugo
run: . ./master/.ci/install_hugo.sh
- name: Build with Hugo
working-directory: "${{ github.workspace }}/master"
run: |
# Set up output directory
OUTPUT_DIR="${GITHUB_WORKSPACE}/gh-pages"
# Run Hugo build
HUGO_ENV=production hugo --minify --gc --printI18nWarnings --printPathWarnings --verbose --baseURL="https://wz2100.net/" --destination "${OUTPUT_DIR}"
- name: Publish gh-pages
id: publishpages
env:
GITHUB_ACTOR: ${{ secrets.WZ2100_PUSH_USERNAME }}
PUSH_PAT: ${{ secrets.WZ2100_PUSH_SECRET_TOKEN }} # use a PAT so that subsequent workflows are triggered on push
working-directory: "${{ github.workspace }}/gh-pages"
run: |
git config user.name "wzdev-ci"
git config user.email "[email protected]"
git add -A
short_sha="$(echo ${GITHUB_SHA} | cut -c1-8)"
git commit -m "Deploy master branch: ${short_sha}" || { echo "PROCESS_DEPLOYMENT=false" >> $GITHUB_OUTPUT && exit 0; }
# Get the new commit's SHA
NEW_COMMIT_SHA=$(git rev-parse --verify HEAD)
echo "NEW_COMMIT_SHA=${NEW_COMMIT_SHA}"
# Push the new commit to the gh-pages branch
git push "https://${PUSH_PAT}@github.com/Warzone2100/wz2100.net.git" gh-pages:gh-pages
echo "PROCESS_DEPLOYMENT=true" >> $GITHUB_OUTPUT
echo "GH_PAGES_BRANCH_COMMIT_SHA=${NEW_COMMIT_SHA}" >> $GITHUB_OUTPUT
echo "Done."
exit 0
- name: 'Wait for Deployment'
id: deployments
if: success() && (steps.publishpages.outputs.PROCESS_DEPLOYMENT == 'true')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_PAGES_BRANCH_COMMIT_SHA: ${{ steps.publishpages.outputs.GH_PAGES_BRANCH_COMMIT_SHA }}
shell: bash --noprofile --norc {0}
run: |
echo "Searching for deployment matching commit: ${GH_PAGES_BRANCH_COMMIT_SHA} ..."
# Poll until we find a deployment with a sha == the push's commit's SHA
status=1
POLL_ATTEMPTS=0
while [ $POLL_ATTEMPTS -le 15 ]
do
sleep_interval=$(( POLL_ATTEMPTS * POLL_ATTEMPTS ))
if [ $sleep_interval -ne 0 ]; then
echo "Sleeping ${sleep_interval} seconds..."
sleep ${sleep_interval}
echo "Finished sleep"
fi
curl -H "Authorization: token ${GITHUB_TOKEN}" -s "https://api.github.com/repos/${GITHUB_REPOSITORY}/deployments" | jq --exit-status --arg desired_sha "${GH_PAGES_BRANCH_COMMIT_SHA}" '.[] | select(.sha == $desired_sha and .environment == "github-pages")' > "deployment.json"
status=$?
if [ $status -eq 0 ]; then
break
fi
echo "Not found yet ..."
(( POLL_ATTEMPTS++ ))
done
if [ $status -ne 0 ]; then
# Did not find matching deployment
echo "::error ::Failed to find matching deployment for: ${GITHUB_SHA}"
exit 1
fi
DEPLOYMENT_ID=$(cat "deployment.json" | jq --raw-output '.id')
if [ -z "$DEPLOYMENT_ID" ]; then
echo "::error ::Missing expected '.id' field"
exit 1
fi
echo "Found deployment ID: ${DEPLOYMENT_ID}"
echo "DEPLOYMENT_ID=${DEPLOYMENT_ID}" >> $GITHUB_OUTPUT
- name: 'Wait for Deployment Success'
if: success() && (steps.publishpages.outputs.PROCESS_DEPLOYMENT == 'true')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEPLOYMENT_ID: ${{ steps.deployments.outputs.DEPLOYMENT_ID }}
shell: bash --noprofile --norc {0}
run: |
echo "Waiting for deployment ${DEPLOYMENT_ID} to finish ..."
# Poll deployment statuses until we find a status with:
# "state": "success"
# "environment": "github-pages"
DEPLOYMENT_STATE=""
POLL_ATTEMPTS=0
while [ $POLL_ATTEMPTS -le 12 ]
do
sleep_interval=$(( POLL_ATTEMPTS * POLL_ATTEMPTS ))
if [ $sleep_interval -ne 0 ]; then
echo "Sleeping ${sleep_interval} seconds..."
sleep ${sleep_interval}
echo "Finished sleep"
fi
DEPLOYMENT_STATE=$(curl -H "Authorization: token ${GITHUB_TOKEN}" -s "https://api.github.com/repos/${GITHUB_REPOSITORY}/deployments/${DEPLOYMENT_ID}/statuses" | jq --raw-output --exit-status --argjson end_states '["success","error","failure"]' '.[] | select( (.state as $state | $end_states | index($state) != null ) and (.environment == "github-pages") ) | .state')
status=$?
(( POLL_ATTEMPTS++ ))
if [ $status -eq 0 ]; then
break
fi
done
if [ $status -ne 0 ]; then
# Did not find matching deployment
echo "::error ::Deployment did not finish before timeout"
exit 1
fi
echo "Found deployment state: ${DEPLOYMENT_STATE}"
if [ "$DEPLOYMENT_STATE" != "success" ]; then
echo "::error ::Deployment did not appear to succeed? (state: ${DEPLOYMENT_STATE})"
exit 1
fi
# Sleep for 10 seconds
sleep 10
echo "Done."
- name: 'Purge Cloudflare Cache'
if: success() && (steps.publishpages.outputs.PROCESS_DEPLOYMENT == 'true')
env:
CLOUDFLARE_ZONE: ${{ secrets.CLOUDFLARE_WZ2100_ZONE }}
CLOUDFLARE_CACHEPURGE_TOKEN: ${{ secrets.CLOUDFLARE_WZ2100_CACHEPURGE_TOKEN }}
run: |
curl -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache" \
-H "Authorization: Bearer ${CLOUDFLARE_CACHEPURGE_TOKEN}" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'