-
Notifications
You must be signed in to change notification settings - Fork 0
301 lines (254 loc) · 10.4 KB
/
deploy.yml
File metadata and controls
301 lines (254 loc) · 10.4 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
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
296
297
298
299
300
301
name: Publish all books to Pages
on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
publish_index:
uses: ./.github/workflows/publish_index.yml
setup_array:
runs-on: ubuntu-slim
outputs:
sites: ${{ steps.set_sites.outputs.sites }}
steps:
- name: Writing the book url names into a sharable matrix input
id: set_sites
run: |
{
echo 'sites<<EOF'
cat << 'JSON'
[
"user-guide",
"tutorials",
"quick-help",
"origin-help",
"x-function",
"originc",
"labtalk",
"python",
"externalpython",
"com",
"labview",
"app",
"appdev",
"codebuild",
"orglab",
"license"
]
JSON
echo 'EOF'
} >> "$GITHUB_OUTPUT"
start_array:
name: Start ${{ matrix.site }} deployment
needs: [setup_array, publish_index]
runs-on: ubuntu-slim
environment: deploy
env:
GH_TOKEN: ${{ secrets.PAT_DOC_WORKFLOW }}
strategy:
matrix:
site: ${{ fromJson(needs.setup_array.outputs.sites) }}
steps:
- name: Dispatch deploy.yml and capture the run url
id: dispatch
run: echo "deploy_run_url=$(gh workflow run deploy.yml --repo originlab/${{ matrix.site }})" >> "$GITHUB_ENV"
- name: Save the run id
run: echo "$deploy_run_url" | grep -oE "[0-9]+$" >> run_id.txt
- uses: actions/upload-artifact@v7
with:
name: run_id-${{ matrix.site }}
path: run_id.txt
retention-days: 1
get_result:
name: Watch ${{ matrix.site }} deployment
needs: [setup_array, start_array]
runs-on: ubuntu-slim
environment: deploy_watch
env:
GH_TOKEN: ${{ secrets.PAT_DOC_WORKFLOW }}
strategy:
fail-fast: false
matrix:
site: ${{ fromJson(needs.setup_array.outputs.sites) }}
steps:
- name: Download run_id-${{ matrix.site }}
uses: actions/download-artifact@v8
with:
name: run_id-${{ matrix.site }}
- name: Set $deploy_run_id
run: echo "deploy_run_id=$(cat run_id.txt)" >> "$GITHUB_ENV"
- name: Delete run_id-${{ matrix.site }}
run: |
gh api /repos/originlab/originlab.github.io/actions/runs/$GITHUB_RUN_ID/artifacts \
-q '.artifacts[] | select(.name == "run_id-${{ matrix.site }}") | .id' \
| xargs -I {} gh api --method DELETE /repos/originlab/originlab.github.io/actions/artifacts/{}
- name: Watch the deployment
run: gh run watch $deploy_run_id -R originlab/${{ matrix.site }} -i 10 --exit-status
- name: Set $build_job_id and $optimize_job_id
run: |
echo "build_job_id=$(gh run view $deploy_run_id -R originlab/${{ matrix.site }} | grep 'publish_book / convert' | grep -oP '\(ID \K\d+')" >> "$GITHUB_ENV"
echo "optimize_job_id=$(gh run view $deploy_run_id -R originlab/${{ matrix.site }} | grep 'publish_book / optimize' | grep -oP '\(ID \K\d+')" >> "$GITHUB_ENV"
- name: Setup helper scripts
run: |
cat << 'EOF' > helpers.sh
#!/bin/bash
get_human_time() {
local total_seconds=$1
if [ "$total_seconds" -eq 0 ]; then
echo "0s"
return
fi
local hours=$((total_seconds / 3600))
local minutes=$(((total_seconds % 3600) / 60))
local seconds=$((total_seconds % 60))
local result=""
[ $hours -gt 0 ] && result+="${hours}h "
[ $minutes -gt 0 ] && result+="${minutes}m "
[ $seconds -gt 0 ] && result+="${seconds}s"
# Trim trailing spaces
echo "$result" | xargs
}
get_duration() {
local repo="$1"
local job_id="$2"
local job_data
job_data=$(gh api "/repos/${repo}/actions/jobs/${job_id}")
local start_time end_time
start_time=$(echo "$job_data" | jq -r '.started_at')
end_time=$(echo "$job_data" | jq -r '.completed_at')
if [ "$start_time" = "null" ] || [ "$end_time" = "null" ]; then
echo "0"
return
fi
local start_epoch end_epoch raw_seconds
start_epoch=$(date -d "$start_time" +%s)
end_epoch=$(date -d "$end_time" +%s)
raw_seconds=$((end_epoch - start_epoch))
get_human_time "$raw_seconds"
}
get_sorted_groups() {
awk -F'\t' '
{
k = $1; v = $2
if (k != "" && v != "") {
n[k]++
vs[k] = vs[k] "#" v
}
}
END {
for (k in n) {
print n[k] "\t" k "\t" vs[k]
}
}
' \
| sort -rn -t$'\t' -k1,1
}
get_human_bytes() {
local bytes=$1
if [ -z "$bytes" ] || [ "$bytes" -eq 0 ]; then
echo "0 B"
return
fi
if [ "$bytes" -gt 1048576 ]; then
awk "BEGIN {printf \"%.1f MB\", $bytes/1048576}"
else
awk "BEGIN {printf \"%.1f KB\", $bytes/1024}"
fi
}
EOF
chmod +x helpers.sh
- name: Summarize the conversion run
run: |
source ./helpers.sh
duration=$(get_duration "originlab/${{ matrix.site }}" "$build_job_id")
echo "# Conversion <sub><sup>:stopwatch:$duration</sup></sub>" >> "$GITHUB_STEP_SUMMARY"
gh run view $deploy_run_id -j $build_job_id -R originlab/${{ matrix.site }} --log \
| awk '/generator\.dll"/ {f=1} /upload-artifact/ {f=0} f' \
| cut -f3- | cut -d' ' -f2- \
| sed -E \
-e 's/\r//g' \
-e 's/\^\[\[[0-9;]*[a-zA-Z]//g' \
-e 's/^##\[group\](.*)/<details><summary>\1<\/summary><pre>/g' \
-e 's/^##\[endgroup\]/<\/pre><\/details>/g' \
-e 's/^##\[warning\](.*)/> [!WARNING]\n> \1/g' \
>> "$GITHUB_STEP_SUMMARY"
- name: Summarize the optimization run
run: |
source ./helpers.sh
duration=$(get_duration "originlab/${{ matrix.site }}" "$optimize_job_id")
echo "# Optimization <sub><sup>:stopwatch:$duration</sup></sub>" >> "$GITHUB_STEP_SUMMARY"
opt_log=$(gh run view $deploy_run_id -j $optimize_job_id -R originlab/${{ matrix.site }} --log \
| awk '/find pages\// {f=1} /upload-artifact/ {f=0} f' \
| cut -f3- | cut -d' ' -f2- \
| sed -E -e 's/\r//g' -e 's/\^\[\[[0-9;]*[a-zA-Z]//g')
warnings=$(echo "$opt_log" | grep '^##\[warning\]' || true)
notices=$(echo "$opt_log" | grep '^##\[notice\]' || true)
if [ -z "$warnings" ] && [ -z "$notices" ]; then
echo ':white_check_mark: All files are optimized without problems.' >> "$GITHUB_STEP_SUMMARY"
else
if [ -n "$warnings" ]; then
echo "$warnings" \
| sed -E 's/^##\[warning\]Skipped `(.*)` because `(.*)`/\2\t\1/' \
| get_sorted_groups \
| awk -F'\t' '
{
cnt = $1; msg = $2; fls = $3
gsub(/#/, "\n- ", fls)
print "\n> [!WARNING]"
print "> " cnt " file(s) were skipped because :x:`" msg "`"
print fls
}
' >> "$GITHUB_STEP_SUMMARY"
fi
if [ -n "$notices" ]; then
echo "$notices" \
| sed -E 's/^##\[notice\]Optimized `(.*)` despite `(.*)`/\2\t\1/' \
| get_sorted_groups \
| awk -F'\t' '
{
cnt = $1; msg = $2; fls = $3
gsub(/#/, "\n- ", fls)
print "\n> [!NOTE]"
print "> " cnt " file(s) were optimized despite :warning:`" msg "`"
print "<details><summary>Show affected files</summary>"
print fls
print "</details>"
}
' >> "$GITHUB_STEP_SUMMARY"
fi
fi
# Need some single quote to workaround a VS colorizer bug
echo 'Done.'
- name: Summarize artifacts
run: |
source ./helpers.sh
artifacts_json=$(gh api /repos/originlab/${{ matrix.site }}/actions/runs/$deploy_run_id/artifacts)
converted_size=$(echo "$artifacts_json" | jq -r '.artifacts[] | select(.name == "converted") | .size_in_bytes // 0' | head -n 1)
optimized_size=$(echo "$artifacts_json" | jq -r '.artifacts[] | select(.name == "optimized") | .size_in_bytes // 0' | head -n 1)
if [ -z "$converted_size" ] || [ "$converted_size" -eq 0 ] || [ -z "$optimized_size" ] || [ "$optimized_size" -eq 0 ]; then
header="# Artifacts"
else
bytes_delta=$((converted_size - optimized_size))
if [ "$bytes_delta" -ge 0 ]; then
desc_delta=":green_circle: Saved"
else
desc_delta=":yellow_circle: Increased"
bytes_delta=$((optimized_size - converted_size))
fi
pct_delta=$(( 100 * bytes_delta / converted_size ))
size_delta=$(get_human_bytes "$bytes_delta")
header="# Artifacts <sub><sup>$desc_delta $size_delta ($pct_delta%)</sup></sub>"
fi
echo "$header" >> "$GITHUB_STEP_SUMMARY"
echo "| Name | Size |" >> "$GITHUB_STEP_SUMMARY"
echo "| :--- | ---: |" >> "$GITHUB_STEP_SUMMARY"
echo "$artifacts_json" | jq -c '[.artifacts[]] | sort_by(.size_in_bytes) | reverse | .[]' | while read -r artifact; do
name=$(echo "$artifact" | jq -r '.name')
size_bytes=$(echo "$artifact" | jq -r '.size_in_bytes')
size_human=$(get_human_bytes "$size_bytes")
echo "| $name | $size_human |" >> "$GITHUB_STEP_SUMMARY"
done
# Need some single quote to workaround a VS colorizer bug
echo 'Done.'