forked from evryfs/github-actions-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
323 lines (278 loc) · 12.5 KB
/
nbfc-build.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
320
321
322
323
name: build-arch
on:
workflow_call:
inputs:
matrix:
required: true
type: string
dockerfile:
required: true
type: string
registry:
required: true
type: string
output_tag:
required: true
type: string
default: "generic"
base_dockerfile:
required: false
type: string
default: "base"
tags:
required: false
type: string
default: "lite"
secrets:
nbfc_priv_secret:
required: true
harbor_secret:
required: true
harbor_user:
required: true
env:
manifest_file: dockerImages_build_manifest.json
jobs:
setup:
name: setup
runs-on: [ self-hosted ]
steps:
- uses: actions/checkout@v2
- name: Set test variable
id: set-variable
run: |
if [ ${{ github.ref }} != 'refs/heads/main' ]; then
echo "will NOT build dockerfiles"
echo "::set-output name=enable::false"
else
echo "WILL build dockerfiles"
echo "::set-output name=enable::true"
fi
# Enable build
echo "::set-output name=enable::true"
shell: bash
- name: Read exported variable
run: |
echo "OUTPUT: ${{ steps.set-variable.outputs.enable }}"
- name: get supported dockerfile architecture
id: get-docker-arch
run: |
docker_arch_manifest=$(cat ${{github.workspace}}/${{env.manifest_file}}| tr -d '[:space:]' | jq -rc '.dockerfile_build_components // {}' | jq -rc '.[] |select(.image_filename |test("${{inputs.dockerfile}}$"))')
echo $docker_arch_manifest
echo "docker_arch_manifest=$docker_arch_manifest" >> $GITHUB_OUTPUT
outputs:
enable: ${{ steps.set-variable.outputs.enable }}
docker_prescribed_arch: ${{ steps.get-docker-arch.outputs.docker_prescribed_arch }}
dockerimage_manifest_arch: ${{ steps.get-docker-arch.outputs.docker_arch_manifest }}
build:
runs-on: [ self-hosted, "${{ matrix.architecture }}", "${{ inputs.tags }}" ]
continue-on-error: true
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write
strategy:
matrix:
architecture: ${{ fromJson(needs.setup.outputs.dockerimage_manifest_arch).architecture }}
fail-fast: false
outputs:
image_name: ${{ steps.build.outputs.image_name }}
needs: [setup]
steps:
- name: Build
id: build
run: |
GEN=$( echo "${{ inputs.dockerfile }}" | sed s/Dockerfile\.// )
echo "image_name=gh-actions-runner-$GEN" >> "$GITHUB_OUTPUT"
L1BASE=$( echo "${{ inputs.base_dockerfile }}" | sed s/Dockerfile\.// )
echo "base_image_name=$L1BASE" >> "$GITHUB_OUTPUT"
- name: conditional base image calculations
id: base-image-calculator
if: |
${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
run: |
base_image=nubificus_base_build=docker-image://harbor.nbfc.io/nubificus/gh-actions-runner-${{ steps.build.outputs.base_image_name }}:${{ inputs.output_tag }}
echo "base_image=$base_image" >> "$GITHUB_OUTPUT"
- name: Checkout
uses: actions/checkout@v3
- name: Install cosign
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
uses: sigstore/cosign-installer@v3.1.1
- name: Set up Docker Context for Buildx
id: buildx-context
run: |
docker context create builders || true
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
with:
version: latest
endpoint: builders
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ inputs.REGISTRY }}
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ inputs.REGISTRY }}
username: ${{ secrets.harbor_user }}
password: ${{ secrets.harbor_secret }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
if: ${{ needs.setup.outputs.enable == 'true' }}
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ inputs.registry }}/${{ steps.build.outputs.image_name }}
tags: |
type=sha,prefix=${{ matrix.architecture }}-
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push ${{ inputs.dockerfile }}-${{ matrix.architecture }}
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
id: build-and-push
uses: docker/build-push-action@master
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
#cache-from: type=local,src=/tmp
#cache-to: type=local,mode=max,dest=/tmp
file: ${{ inputs.dockerfile }}
build-contexts: |
${{ steps.base-image-calculator.outputs.base_image }}
provenance: false
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
env:
COSIGN_EXPERIMENTAL: "true"
DIGEST: ${{ steps.build-and-push.outputs.digest }}
TAGS: ${{ steps.docker_meta.outputs.tags }}
# run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign {}@${{ steps.build-and-push.outputs.digest }}
run: |
cosign sign --yes ${{ inputs.registry }}/${{ steps.build.outputs.image_name }}@${{steps.build-and-push.outputs.digest}} \
-a "repo=${{github.repository}}" \
-a "workflow=${{github.workflow}}" \
-a "ref=${{github.sha}}" \
-a "author=Nubificus LTD"
- name: Clean up Docker Context for Buildx
id: buildx-context-cleanup
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
run: |
docker context remove builders || true
- name: random-number-generator
id: random-number-generator
run: echo "random_number=$(($RANDOM))" >> $GITHUB_OUTPUT
shell: bash
- name: Store docker images details in var
id: update-docker-images-details-variable
run: |
curl --location 'https://api.github.com/repos/${{github.repository}}/actions/variables?per_page=100' \
--header 'Accept: application/vnd.github+json' \
--header 'Authorization: Bearer ${{ secrets.nbfc_priv_secret }}' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
--header 'Content-Type: application/json' \
-d '{ "name": "_${{ github.run_id }}_${{ steps.random-number-generator.outputs.random_number }}", "value": "{ \"image\": \"${{inputs.dockerfile}}\", \"arch\": \"${{matrix.architecture}}\", \"result\": \"${{ steps.build-and-push.conclusion }}\" }"}'
manifest:
runs-on: [ self-hosted ]
needs: [setup, build]
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write
steps:
- uses: actions/checkout@v3
- uses: benjlevesque/short-sha@v2.2
id: short-sha
with:
length: 7
- name: query-build-variables
id: query-build-variables
run: |
sleep 5
repo_vars=$(curl --location 'https://api.github.com/repos/${{github.repository}}/actions/variables?per_page=100' --header 'Accept: application/vnd.github+json' --header 'Authorization: Bearer ${{ secrets.nbfc_priv_secret }}' --header 'X-GitHub-Api-Version: 2022-11-28' --header 'Content-Type: application/json')
job_run_vars_values=$(echo $repo_vars | jq -rce '[.variables[] | select(.name |test("_${{ github.run_id }}."))]')
echo "repo variables: ============"
echo $job_run_vars_values
echo
build_values=$(echo $job_run_vars_values | jq -rce '[.[].value]' | sed 's/[\\]//g' | sed 's/"{/{/g' | sed 's/}"/}/g' | jq -rce '[ .[] | select(.image | contains("${{ inputs.dockerfile }}")) ]')
echo "build_values: ================"
echo $build_values
echo
dockerImage_build=$(echo $build_values | jq -rce '[.[].arch]')
echo "dockerImage_build: ================"
echo $dockerImage_build
echo "dockerImage_arch=$dockerImage_build" >> "$GITHUB_OUTPUT"
arch_array=$(echo $build_values | jq -rce '[.[].arch]' | sed 's/[\[\]//g' | sed 's/\]//g' | sed 's/,/ /g')
echo "arch_array: ================"
echo $arch_array
echo "arch_array=$arch_array" >> "$GITHUB_OUTPUT"
echo "===================="
dockerImage_build_length=$(echo $build_values | jq -rce '[.[].arch] | length')
echo "dockerImage_build_length: ================"
echo $dockerImage_build_length
echo "dockerImage_build_length=$dockerImage_build_length" >> "$GITHUB_OUTPUT"
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ inputs.REGISTRY }}
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ inputs.REGISTRY }}
username: ${{ secrets.harbor_user }}
password: ${{ secrets.harbor_secret }}
- name: Set image name
id: set-image-name
run: |
NAME=$( echo "${{ needs.build.outputs.image_name }}" )
REGISTRY=$( echo "${{ inputs.REGISTRY }}" )
#NAMESPACE="runners"
#echo "image_name=$REGISTRY/$NAMESPACE/$NAME" >> "$GITHUB_OUTPUT"
echo "image_name=$REGISTRY/$NAME" >> "$GITHUB_OUTPUT"
- name: Install cosign
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
uses: sigstore/cosign-installer@v3.1.1
with:
cosign-release: 'v1.13.1'
- name: Check install!
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
run: cosign version
- name: Create manifest for ${{ needs.build.outputs.image_name }}
id: create-manifest
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
run: |
amend_command=""
for i in ${{ steps.query-build-variables.outputs.arch_array }}; do
amend_image=`echo " --amend" ${{ steps.set-image-name.outputs.image_name }}:$i-$SHA ` ;
amend_command=$amend_image$amend_command;
done
echo "-------------------- amend command -------------------"
echo $amend_command
docker manifest rm ${{ steps.set-image-name.outputs.image_name }}:${{ inputs.output_tag }} || true
docker manifest create ${{ steps.set-image-name.outputs.image_name }}:${{ inputs.output_tag }} \
`echo $amend_command`
docker manifest push ${{ steps.set-image-name.outputs.image_name }}:${{ inputs.output_tag }}
VAR=`docker manifest push ${{ steps.set-image-name.outputs.image_name }}:${{ inputs.output_tag }} | tail -1`
echo "manifest_sha=$VAR" >> "$GITHUB_OUTPUT"
env:
SHA: ${{ steps.short-sha.outputs.sha }}
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' && needs.setup.outputs.enable == 'true' }}
env:
COSIGN_EXPERIMENTAL: "true"
DIGEST: ${{ steps.build-and-push.outputs.digest }}
TAGS: ${{ steps.docker_meta.outputs.tags }}
# run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign {}@${{ steps.build-and-push.outputs.digest }}
run: |
cosign sign --yes ${{ steps.set-image-name.outputs.image_name }}@${{steps.create-manifest.outputs.manifest_sha }} \
-a "repo=${{github.repository}}" \
-a "workflow=${{github.workflow}}" \
-a "ref=${{github.sha}}" \
-a "author=Nubificus LTD"