-
Notifications
You must be signed in to change notification settings - Fork 6
272 lines (228 loc) · 11 KB
/
_fw-ig-build-callable.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
# The goal of this callable workflow is to build and publish an IG, and
# - Have a "branch-build" that is the ref that has the latest changes for an IG.
# - Have a "merge-build" that attempts to merge the branch with a merge target and then build the merge result.
# - The git merge might fail.
# - The IG build might fail.
# - Publish the branch and merge builds, update the .md file with links to the GitHub Pages home page for the IGs' builds
# - Output useful summary information for the build
name: IG build and publish
defaults:
run:
shell: bash
on:
workflow_call:
inputs:
# --- Related to the repo and ref containing the changes being built.
ig-owner-repo:
description: The "owner/repo" that has the branch to be built. For a PR, this could be a different "owner/repo" than the "owner/repo" running this workflow.
type: string
required: true
ig-ref:
description: The ref to build. For a PR, this will be the branch name of the PR in its "ig-owner-repo".
type: string
required: true
# build related
ig-path:
description: The relative path, in the checkout of the "ig-owner-repo" at the "ig-ref", to the IG root folder. This folder is mounted to /ig in the Docker container.
required: true
type: string
ig-read-secret-name:
description: The secret name for read access to the ig-owner-repo
type: string
required: true
# ig-write-secret-name:
# description: The secret name for write access to the ig-owner-repo
# type: string
# required: true
pr-base-ref:
description: The PR base ref
type: string
pr-number:
description: PR number
type: string
# --- Publisher related
publisher-args:
description: The parameters to pass to the publisher.jar. Keep in mind that the IG root folder is at /ig for these arguments.
required: true
type: string
docker-publisher-tag:
description: "The Docker publisher tag to use from: https://hub.docker.com/repository/docker/sessaid/ig-publisher/tags"
type: string
default: latest
# --- Publishing related
io-owner:
description: the owner name of the IO repo
type: string
io-repo:
description: If set, it is a GitHub dedicated repository (repo name only) for publishing the IG's output.
type: string
io-branch:
description: The branch to use for publishing the IG's output. gh-pages by default so it works on github.io
type: string
io-path:
description: "A path to publish under. If not set, a default is calculated. TODO: document this."
type: string
io-path-merged:
description: "A path to publish the merged results under. If not set, a default is calculated. TODO: document this."
type: string
io-write-secret-name:
description: The secret name for writing to the IO repository
type: string
jobs:
docker-build:
name: Build and Publish IG
runs-on: ubuntu-latest
steps:
- name: Env setup
id: env
run: |
echo "NOW=$(date +%Y%m%d_%H%M%S)" >> $GITHUB_ENV
- name: Debug inputs
run: |
echo Running IG build with inputs: '${{ toJSON(inputs) }}'
echo Running IG build with env: '${{ toJSON(env) }}'
echo Running IG build with secrets: '${{ toJSON(secrets) }}'
- name: Checkout IG ${{ inputs.owner-repo }}/${{inputs.ig-root}} ref:${{ inputs.ig-ref }}
uses: actions/checkout@v3
with:
repository: ${{ inputs.ig-owner-repo }}
ref: ${{ inputs.ig-ref }}
path: ig-clone
token: ${{ secrets[inputs.ig-read-secret-name] }}
# We should always have a
- name: Building IG from ig-ref
id: branch
run: |
set -x
git config --global user.name 'GitHub Actions IG build'
git config --global user.email '[email protected]'
cd ig-clone
echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
echo Running IG build with inputs: '${{ toJSON(inputs) }}'
#mkdir -p "${{inputs.ig-root}}/output"; echo hello branch build $(date) > "${GITHUB_WORKSPACE}/ig-clone/${{inputs.ig-root}}/output/index.html"
#docker pull "sessaid/ig-publisher:${{inputs.docker-publisher-tag}}"
docker run --rm --volume "${GITHUB_WORKSPACE}/ig-clone/${{inputs.ig-path}}:/ig" "sessaid/ig-publisher:${{inputs.docker-publisher-tag}}" ${{ inputs.publisher-args }}
echo $NOW > "${GITHUB_WORKSPACE}/ig-clone/${{inputs.ig-path}}/output/gh-actions-build-timestamp.txt"
mv "${GITHUB_WORKSPACE}/ig-clone/${{inputs.ig-path}}/output" "${GITHUB_WORKSPACE}/output-ref"
# Do a merge build if pr-base-ref
- name: Building IG for merge with pr-base-ref
id: merge
if: inputs.pr-base-ref != ''
# try to merge and build again
# TODO: try to optimize the full fetch
run: |
set -x
# echo doing base merge and build
cd ig-clone
git fetch --unshallow
git clean -fdx
git remote add upstream "https://github.com/${{ github.repository }}"
git fetch --all
git checkout -b upstream-${{ inputs.pr-base-ref }} --track "upstream/${{ inputs.pr-base-ref }}"
# check if the pr ref and base have diverged, and need a merge
if ! git merge-base --is-ancestor upstream-${{ inputs.pr-base-ref }} ${{ inputs.ig-ref }}; then
# Diverged
if git merge "${{ inputs.ig-ref }}"; then
# Merge succeeded
echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
echo merge was successful
#mkdir -p "${{inputs.ig-path}}/output"; echo hello merged branch build $(date) > "${{inputs.ig-root}}/output/index.html"
docker run --rm --volume "${GITHUB_WORKSPACE}/ig-clone/${{inputs.ig-path}}:/ig" "sessaid/ig-publisher:${{inputs.docker-publisher-tag}}" ${{ inputs.publisher-args }}
echo $NOW > "${GITHUB_WORKSPACE}/ig-clone/${{inputs.ig-path}}/output/gh-actions-build-timestamp.txt"
mv "${GITHUB_WORKSPACE}/ig-clone/${{inputs.ig-path}}/output" "${GITHUB_WORKSPACE}/output-merge"
else
echo merge has conflicts
git merge --abort
fi
echo "skip-merge=false" >> $GITHUB_OUTPUT
else
echo "skip-merge=true" >> $GITHUB_OUTPUT
fi
- name: Create IO branch if needed
if: inputs.io-repo != ''
run: |
if git ls-remote --exit-code --heads 'https://${{inputs.io-owner}}:${{secrets[inputs.io-write-secret-name]}}@github.com/${{inputs.io-owner}}/${{inputs.io-repo}}.git' ${{inputs.io-branch}}; then
echo IO repo ${{inputs.io-owner}}/${{inputs.io-repo}} and branch ${{inputs.io-branch}} exists
else
echo repo or branch does not exist, creating branch
mkdir io-create-branch
cd io-create-branch
git init --initial-branch=${{inputs.io-branch}} .
echo "Auto created branch" > README.md
git add -A
git commit -m "Auto created branch for IG IO."
git push -f 'https://${{inputs.io-owner}}:${{secrets[inputs.io-write-secret-name]}}@github.com/${{inputs.io-owner}}/${{inputs.io-repo}}.git' ${{inputs.io-branch}}:${{inputs.io-branch}}
cd ..
rm -rf io-create-branch
fi
# Publish if requested
- name: Checking out IO repo
if: inputs.io-repo != ''
uses: actions/checkout@v3
with:
repository: ${{ inputs.io-owner }}/${{ inputs.io-repo }}
ref: ${{ inputs.io-branch }}
path: io-clone
token: ${{ secrets[inputs.io-write-secret-name] }}
- name: Publishing IG
if: inputs.io-repo != ''
run: |
#set -x
cd io-clone
attempts=1
while true; do
# Get HEAD reset to latest remote branch in case push failed
git fetch origin ${{ inputs.io-branch }}
git reset --hard "origin/${{ inputs.io-branch }}"
git clean -fdx
# race test
# sleep 300
# do the ig-ref copy
[[ -d "${{ inputs.io-path }}" ]] && rm -rf "${{ inputs.io-path }}"
mkdir -p "${{ inputs.io-path }}"
cp -ra "${GITHUB_WORKSPACE}/output-ref"/* "${{ inputs.io-path }}"
git add ${{ inputs.io-path }}
if [[ ! -f "IG_BUILDS.md" ]]; then
echo '# IG Builds ' > IG_BUILDS.md
fi
IGLink="[${{ inputs.io-path }}](https://${{ inputs.io-owner }}.github.io/${{ inputs.io-repo }}/${{ inputs.io-path }}/index.html?version=${{steps.branch.outputs.sha}})"
{
cat IG_BUILDS.md | grep -v '${{ inputs.io-path }}' ;
printf '%s \n' "$IGLink";
} | sort > IG_BUILDS.md.new
mv -f IG_BUILDS.md.new IG_BUILDS.md
echo "Branch IG build site: $IGLink" >> $GITHUB_STEP_SUMMARY
# If there was a merge build
if [[ ! -z "${{ inputs.io-path-merged }}" ]]; then
[[ -d "${{ inputs.io-path-merged }}" ]] && rm -rf "${{ inputs.io-path-merged }}"
mkdir -p "${{ inputs.io-path-merged }}"
fi
if [[ "${{ steps.merge.outputs.skip-merge }}" == false ]]; then
cp -ra "${GITHUB_WORKSPACE}/output-merge"/* "${{ inputs.io-path-merged }}"
IGLink="[${{ inputs.io-path-merged }}](https://${{ inputs.io-owner }}.github.io/${{ inputs.io-repo }}/${{ inputs.io-path-merged }}/index.html?version=${{steps.merge.outputs.sha}})"
{
cat IG_BUILDS.md;
printf '%s \n' "$IGLink";
} | sort > IG_BUILDS.md.new
mv -f IG_BUILDS.md.new IG_BUILDS.md
echo "Merge IG build site: $IGLink" >> $GITHUB_STEP_SUMMARY
fi
git add ${{ inputs.io-path-merged }}
git add IG_BUILDS.md
if [[ -z "$(git status --porcelain)" ]]; then
echo NO changes
else
git commit -m "IG: ${{ inputs.ig-owner-repo }}, ref: ${{ inputs.ig-ref }} run_id: ${{ github.run_id }}, run_number: ${{ github.run_number }}, run_attempt: ${{ github.run_attempt }} with branch SHA: ${{steps.branch.outputs.sha}} and merge SHA: ${{steps.merge.outputs.sha}}"
if git push --atomic; then
# we were able to push to remote, i.e. remote wasn't updated by another build
break
else
attempts=$((attempts+1))
if [[ $attempts -gt 3 ]]; then
# TODO: make this configurable
echo "IG site push failed 3 times, giving up!"
exit 1
fi
fi
fi
done