forked from spidernet-io/spiderpool
-
Notifications
You must be signed in to change notification settings - Fork 0
319 lines (293 loc) · 12.3 KB
/
build-image-ci.yaml
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
name: Auto Build Image CI
env:
ONLINE_REGISTER: ghcr.io
BUILD_PLATFORM: linux/amd64,linux/arm64
ONLINE_REGISTER_USER: ${{ github.actor }}
ONLINE_REGISTER_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
on:
workflow_run:
workflows:
- "Image CI Cache Cleaner"
branches:
- main
- release-*
types:
- completed
# called by daily build and push image
workflow_call:
inputs:
ref:
required: true
type: string
push:
required: true
type: string
imageTarName:
required: false
type: string
default: "image-tar"
outputs:
imageTag:
description: "tag of image ci"
value: ${{ jobs.build_and_push_prs.outputs.imageTag }}
# concurrency:
# group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
# cancel-in-progress: true
jobs:
build_and_push_prs:
runs-on: ubuntu-latest
# run if filters.src was changed
outputs:
imageTag: ${{ env.tag }}
e2e: ${{ steps.tag.outputs.e2e }}
strategy:
matrix:
include:
- name: spiderpool-agent
dockerfile: ./images/spiderpool-agent/Dockerfile
context: ./
- name: spiderpool-controller
dockerfile: ./images/spiderpool-controller/Dockerfile
context: ./
steps:
- name: Set up Docker Buildx
uses: docker/[email protected]
# commit sha is used for image tag
- name: Getting image tag
id: tag
run: |
echo ${{ github.event_name }}
if ${{ inputs.ref != '' }}; then
echo "trigger by workflow_call"
echo "tag=${{ inputs.ref }}" >> $GITHUB_ENV
echo "push=${{ inputs.push }}" >> $GITHUB_ENV
elif ${{ github.event_name == 'push' }} ; then
echo "trigger by push"
echo "tag=${{ github.sha }}" >> $GITHUB_ENV
echo "push=false" >> $GITHUB_ENV
elif ${{ github.event_name == 'pull_request_target' }} ; then
echo "trigger by pull_request_target"
echo "tag=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
echo "push=false" >> $GITHUB_ENV
elif ${{ github.event_name == 'workflow_run' }} ; then
echo "trigger by workflow_run"
echo "tag=main" >> $GITHUB_ENV
echo "push=false" >> $GITHUB_ENV
else
echo "trigger by ${{ github.event_name }}"
echo "tag=${{ github.sha }}" >> $GITHUB_ENV
echo "push=false" >> $GITHUB_ENV
fi
- name: Login to online register
uses: docker/[email protected]
if: ${{ env.push == 'true' }}
with:
username: ${{ env.ONLINE_REGISTER_USER }}
password: ${{ env.ONLINE_REGISTER_PASSWORD }}
registry: ${{ env.ONLINE_REGISTER }}
# checkout the changed code
- name: Checkout Source Code
uses: actions/checkout@v4
with:
persist-credentials: false
ref: ${{ env.tag }}
- name: Getting Build Arg
id: arg
continue-on-error: false
run: |
GIT_COMMIT_VERSION=$( git show -s --format='format:%H')
GIT_COMMIT_TIME=$( git show -s --format='format:%aI')
echo "commitver=${GIT_COMMIT_VERSION}" >> $GITHUB_ENV
echo "committime=${GIT_COMMIT_TIME}" >> $GITHUB_ENV
# ============= get cache ===========
# Load Golang cache build from GitHub
# Cache dependencies and build outputs in GitHub Actions
# the github will keep the cache for 7 days at most
# we could see cache-hit result for this step
- name: Load ${{ matrix.name }} Golang cache build from GitHub
uses: actions/cache@v3
id: cache
with:
# if find the cache mapping to "key" , will restore to "path"
# when the key doesn't match an existing cache. A list of restore-keys is useful when you are restoring a cache from another branch
path: /tmp/.cache/${{ matrix.name }}
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}-${{ matrix.name }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}-${{ matrix.name }}-
${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}-
${{ runner.os }}-go-
- name: Create ${{ matrix.name }} cache directory
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
shell: bash
run: |
mkdir -p /tmp/.cache/${{ matrix.name }}
# Import GitHub's cache build to docker cache
- name: Copy ${{ matrix.name }} Golang cache to docker cache
uses: docker/[email protected]
with:
context: /tmp/.cache/${{ matrix.name }}
file: ./images/cache/Dockerfile
github-token: ${{ secrets.WELAN_PAT }}
push: false
platforms: linux/amd64
target: import-cache
# ========== main branch pushes , build 2 images
# build normal image
- name: CI Build ${{ matrix.name }}
if: ${{ github.event_name != 'pull_request_target' }}
uses: docker/[email protected]
continue-on-error: false
id: docker_build_ci_master
with:
context: .
file: ${{ matrix.dockerfile }}
# Only push when the event name was a GitHub push, this is to avoid
# re-pushing the image tags when we only want to re-create the Golang
# docker cache after the workflow "Image CI Cache Cleaner" was terminated.
push: ${{ env.push }}
provenance: false
platforms: linux/amd64,linux/arm64
github-token: ${{ secrets.WELAN_PAT }}
tags: |
${{ env.ONLINE_REGISTER }}/${{ github.repository }}/${{ matrix.name }}-ci:${{ env.tag }}
build-args: |
GIT_COMMIT_VERSION=${{ env.commitver }}
GIT_COMMIT_TIME=${{ env.committime }}
VERSION=${{ env.tag }}
# build debug image who turn on race and deadlock detection
- name: CI race detection Build ${{ matrix.name }}
if: ${{ github.event_name != 'pull_request_target' }}
uses: docker/[email protected]
continue-on-error: false
id: docker_build_ci_master_detect_race_condition
with:
context: .
file: ${{ matrix.dockerfile }}
# Only push when the event name was a GitHub push, this is to avoid
# re-pushing the image tags when we only want to re-create the Golang
# docker cache after the workflow "Image CI Cache Cleaner" was terminated.
push: ${{ env.push }}
platforms: linux/amd64
outputs: type=tar,dest=/tmp/${{ matrix.name }}-race.tar
github-token: ${{ secrets.WELAN_PAT }}
tags: |
${{ env.ONLINE_REGISTER }}/${{ github.repository }}/${{ matrix.name }}-ci:${{ env.tag }}-race
build-args: |
RACE=1
GIT_COMMIT_VERSION=${{ env.commitver }}
GIT_COMMIT_TIME=${{ env.committime }}
VERSION=${{ env.tag }}
- name: CI Image Releases digests
if: ${{ github.event_name != 'pull_request_target' }}
shell: bash
run: |
mkdir -p image-digest/
echo "${{ env.ONLINE_REGISTER }}/${{ github.repository }}/${{ matrix.name }}-ci:${{ env.tag }}@${{ steps.docker_build_ci_master.outputs.digest }}" > image-digest/${{ matrix.name }}.txt
echo "${{ env.ONLINE_REGISTER }}/${{ github.repository }}/${{ matrix.name }}-ci:${{ env.tag }}-race@${{ steps.docker_build_ci_master_detect_race_condition.outputs.digest }}" >> image-digest/${{ matrix.name }}.txt
# =========== trigger by PR updates , build 2 images
- name: CI Build ${{ matrix.name }}
if: ${{ github.event_name == 'pull_request_target' }}
uses: docker/[email protected]
continue-on-error: false
id: docker_build_ci_pr
with:
context: .
file: ${{ matrix.dockerfile }}
push: ${{ env.push }}
provenance: false
github-token: ${{ secrets.WELAN_PAT }}
platforms: linux/amd64,linux/arm64
tags: |
${{ env.ONLINE_REGISTER }}/${{ github.repository }}/${{ matrix.name }}-ci:${{ env.tag }}
build-args: |
GIT_COMMIT_VERSION=${{ env.commitver }}
GIT_COMMIT_TIME=${{ env.committime }}
VERSION=${{ env.tag }}
- name: CI race detection Build ${{ matrix.name }}
if: ${{ github.event_name == 'pull_request_target' }}
uses: docker/[email protected]
continue-on-error: false
id: docker_build_ci_pr_detect_race_condition
with:
context: .
file: ${{ matrix.dockerfile }}
push: ${{ env.push }}
platforms: linux/amd64
github-token: ${{ secrets.WELAN_PAT }}
outputs: type=tar,dest=/tmp/${{ matrix.name }}-race.tar
tags: |
${{ env.ONLINE_REGISTER }}/${{ github.repository }}/${{ matrix.name }}-ci:${{ env.tag }}-race
build-args: |
RACE=1
GIT_COMMIT_VERSION=${{ env.commitver }}
GIT_COMMIT_TIME=${{ env.committime }}
VERSION=${{ env.tag }}
- name: CI Image Releases digests
if: ${{ github.event_name == 'pull_request_target' }}
shell: bash
run: |
mkdir -p image-digest/
echo "${{ env.ONLINE_REGISTER }}/${{ github.repository }}/${{ matrix.name }}-ci:${{ env.tag }}@${{ steps.docker_build_ci_pr.outputs.digest }}" > image-digest/${{ matrix.name }}.txt
echo "${{ env.ONLINE_REGISTER }}/${{ github.repository }}/${{ matrix.name }}-ci:${{ env.tag }}-race@${{ steps.docker_build_ci_pr_detect_race_condition.outputs.digest }}" >> image-digest/${{ matrix.name }}.txt
# Upload artifact digests
- name: Upload artifact digests
uses: actions/[email protected]
with:
name: image-digest-${{ matrix.name }}
path: image-digest
retention-days: 1
# Upload artifact race images tar
- name: Upload artifact race image tar
uses: actions/[email protected]
with:
name: ${{ inputs.imageTarName }}-${{ matrix.name }}
path: /tmp/${{ matrix.name }}-race.tar
retention-days: 1
# ============= restore cache ===========
# Store docker's golang's cache build locally only on the main branch
- name: Store ${{ matrix.name }} Golang cache build locally
if: ${{ github.event_name != 'pull_request_target' && steps.cache.outputs.cache-hit != 'true' }}
uses: docker/[email protected]
with:
context: .
file: ./images/cache/Dockerfile
push: false
outputs: type=local,dest=/tmp/docker-cache-${{ matrix.name }}
platforms: linux/amd64
target: export-cache
# Store docker's golang's cache build locally only on the main branch
- name: Store ${{ matrix.name }} Golang cache in GitHub cache path
if: ${{ github.event_name != 'pull_request_target' && steps.cache.outputs.cache-hit != 'true' }}
shell: bash
run: |
mkdir -p /tmp/.cache/${{ matrix.name }}/
if [ -f /tmp/docker-cache-${{ matrix.name }}/tmp/go-build-cache.tar.gz ]; then
cp /tmp/docker-cache-${{ matrix.name }}/tmp/go-build-cache.tar.gz /tmp/.cache/${{ matrix.name }}/
fi
if [ -f /tmp/docker-cache-${{ matrix.name }}/tmp/go-pkg-cache.tar.gz ]; then
cp /tmp/docker-cache-${{ matrix.name }}/tmp/go-pkg-cache.tar.gz /tmp/.cache/${{ matrix.name }}/
fi
image-digests:
name: Display Digests
runs-on: ubuntu-latest
needs: build_and_push_prs
steps:
- name: Downloading Image Digests
shell: bash
run: |
mkdir -p image-digest/
- name: Download digests of all images built
uses: actions/download-artifact@v3
with:
path: image-digest/
name: image-digest-spiderpool-agent
- name: Download digests of all images built
uses: actions/download-artifact@v3
with:
path: image-digest/
name: image-digest-spiderpool-controller
- name: Image Digests Output
shell: bash
run: |
cd image-digest/
find -type f | sort | xargs -d '\n' cat