-
Notifications
You must be signed in to change notification settings - Fork 3
167 lines (165 loc) · 7.41 KB
/
CI_publish.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
name: 'Publish Site'
on:
push:
branches:
- main
repository_dispatch:
types: [github_release_update, development_build_update, scheduled_update]
concurrency: publish_site
jobs:
publish_site:
name: 'Publish Site'
runs-on: ubuntu-latest
environment: publish-site
steps:
- name: Checkout main branch
uses: actions/checkout@v3
with:
ref: main
path: main
submodules: recursive
persist-credentials: false
- name: Create working directories
id: preparefolders
run: |
CHANGELOG_DIR="${GITHUB_WORKSPACE}/temp/changes"
mkdir -p "${CHANGELOG_DIR}"
echo "CHANGELOG_DIR=${CHANGELOG_DIR}" >> $GITHUB_OUTPUT
- name: Checkout gh-pages branch
uses: actions/checkout@v3
with:
ref: gh-pages
path: gh-pages
ssh-key: '${{ secrets.PAGES_DEPLOY_KEY }}'
persist-credentials: true
- name: Copy site files to gh-pages branch
id: copy_updates
run: |
rsync -a "${{ github.workspace }}/main/wz2100-database-project/" "${{ github.workspace }}/gh-pages/"
- name: Publish any changes to data files
id: publishpages
working-directory: "./gh-pages"
run: |
git config user.name "wzdev-ci"
git config user.email "61424532+wzdev-ci@users.noreply.github.com"
git add -A
timestamp=$(date -u)
git commit -m "Generate site: ${timestamp}" || { echo "PROCESS_DEPLOYMENT=false" >> $GITHUB_OUTPUT && exit 0; }
#git pull --rebase
# 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
echo "PROCESS_DEPLOYMENT=true" >> $GITHUB_OUTPUT
echo "GH_PAGES_BRANCH_COMMIT_SHA=${NEW_COMMIT_SHA}" >> $GITHUB_OUTPUT
echo "Done."
# Get the list of files / paths changed in the latest commit
CHANGED_FILES_LIST="${{ steps.preparefolders.outputs.CHANGELOG_DIR }}/changedpaths.txt"
git diff-tree --no-commit-id --name-only -r -z HEAD | tr '\0' '\n' > "${CHANGED_FILES_LIST}"
echo "CHANGED_FILES_LIST=${CHANGED_FILES_LIST}" >> $GITHUB_OUTPUT
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: 'Generate Cloudflare Cache Purge URLs List'
id: purgeurls
if: success() && (steps.publishpages.outputs.PROCESS_DEPLOYMENT == 'true')
run: |
PURGE_URLS_DATA_FILES_DIR="${{ steps.preparefolders.outputs.CHANGELOG_DIR }}/output"
python3 "${GITHUB_WORKSPACE}/main/.ci/gen_purge_url_batches.py" "betaguide.wz2100.net" "${{ steps.publishpages.outputs.CHANGED_FILES_LIST }}" "${PURGE_URLS_DATA_FILES_DIR}"
echo "PURGE_URLS_DATA_FILES_DIR=${PURGE_URLS_DATA_FILES_DIR}" >> $GITHUB_OUTPUT
- 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: |
# Needs to handle multiple data files, since each purge command can only send a max of 30 URLs
for file in ${{ steps.purgeurls.outputs.PURGE_URLS_DATA_FILES_DIR }}/*
do
echo "File: $file"
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-binary "@$file"
done; # file
echo "Done."