-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
375 lines (334 loc) · 15.5 KB
/
action.yml
File metadata and controls
375 lines (334 loc) · 15.5 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
name: 'Docker and GitOps Commit'
description: 'Build and push the Docker image and commits the new version to your GitOps repo.'
author: 'Staffbase GmbH'
inputs:
docker-registry:
description: 'Docker Registry'
required: true
default: 'registry.staffbase.com'
docker-registry-api:
description: 'Docker Registry API'
required: false
default: 'https://registry.staffbase.com/v2/'
docker-image:
description: 'Docker Image'
required: true
docker-custom-tag:
description: 'Docker Custom Tag'
required: false
docker-username:
description: 'Username for the Docker Registry'
required: false
docker-password:
description: 'Password for the Docker Registry'
required: false
docker-file:
description: 'Path of the Dockerfile. Should be relative to input.working-directory'
required: true
default: './Dockerfile'
docker-build-args:
description: "List of build-time variables"
required: false
docker-build-secrets:
description: "List of secrets to expose to the build (e.g., key=string, GIT_AUTH_TOKEN=mytoken)"
required: false
docker-build-secret-files:
description: "List of secret files to expose to the build (e.g., key=filename, MY_SECRET=./secret.txt)"
required: false
docker-build-target:
description: "Sets the target stage to build"
required: false
docker-build-platforms:
description: "Sets the target platforms for build"
required: false
default: 'linux/amd64'
docker-build-provenance:
description: "Generate provenance attestation for the build"
required: false
default: 'false'
docker-build-outputs:
description: "Custom output destinations (e.g., type=registry,push=true,compression=zstd,force-compression=true). When set, this replaces the default push behavior - include push=true if pushing is desired."
required: false
docker-disable-retagging:
description: 'Disable retagging of existing images'
required: false
default: 'false'
gitops-organization:
description: 'GitHub Organization for GitOps'
required: true
default: 'Staffbase'
gitops-repository:
description: 'GitHub Repository for GitOps'
required: true
default: 'mops'
gitops-user:
description: 'GitHub User for GitOps'
required: true
default: 'Staffbot'
gitops-email:
description: 'GitHub User for GitOps'
required: true
default: 'staffbot@staffbase.com'
gitops-token:
description: 'GitHub Token for GitOps'
required: false
gitops-dev:
description: 'Files which should be updated by the GitHub Action for DEV'
required: false
gitops-stage:
description: 'Files which should be updated by the GitHub Action for STAGE'
required: false
gitops-prod:
description: 'Files which should be updated by the GitHub Action for PROD'
required: false
upwind-client-id:
description: 'Upwind Client ID'
required: false
upwind-organization-id:
description: 'Upwind Organization ID'
required: false
upwind-client-secret:
description: 'Upwind Client Secret'
required: false
working-directory:
description: 'The path relative to the repo root dir in which the GitOps action should be executed.'
required: false
default: '.'
outputs:
docker-tag:
description: 'Docker tag'
value: ${{ steps.preparation.outputs.tag }}
docker-digest:
description: 'Docker digest'
value: ${{ steps.docker_build.outputs.digest || steps.docker_retag.outputs.digest }}
runs:
using: "composite"
steps:
- name: Generate Tags
id: preparation
shell: bash
run: |
BUILD="true"
if [[ -n "${{ inputs.docker-custom-tag }}" ]]; then
TAG="${{ inputs.docker-custom-tag }}"
LATEST="latest"
PUSH="true"
BUILD="${{ inputs.docker-disable-retagging }}"
elif [[ $GITHUB_REF == refs/heads/master ]]; then
TAG="master-${GITHUB_SHA::8}"
LATEST="master"
PUSH="true"
elif [[ $GITHUB_REF == refs/heads/main ]]; then
TAG="main-${GITHUB_SHA::8}"
LATEST="main"
PUSH="true"
elif [[ $GITHUB_REF == refs/heads/dev ]]; then
TAG="dev-${GITHUB_SHA::8}"
LATEST="dev"
PUSH="true"
elif [[ $GITHUB_REF == refs/tags/v* ]]; then
TAG="${GITHUB_REF:11}"
LATEST="latest"
PUSH="true"
BUILD="${{ inputs.docker-disable-retagging }}"
elif [[ $GITHUB_REF == refs/tags/* ]]; then
TAG="${GITHUB_REF:10}"
LATEST="latest"
PUSH="true"
BUILD="${{ inputs.docker-disable-retagging }}"
else
TAG="${GITHUB_SHA::8}"
PUSH="false"
fi
TAG_LIST="${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${TAG}"
if [[ ! -z "${LATEST}" ]]; then
TAG_LIST+=",${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${LATEST}"
fi
echo "build=$BUILD" >> $GITHUB_OUTPUT
echo "latest=$LATEST" >> $GITHUB_OUTPUT
echo "push=$PUSH" >> $GITHUB_OUTPUT
echo "tag=$TAG" >> $GITHUB_OUTPUT
echo "tag_list=$TAG_LIST" >> $GITHUB_OUTPUT
- name: Verify Architecture Match
shell: bash
if: steps.preparation.outputs.build == 'true'
run: |
RUNNER_ARCH="${{ runner.arch }}" # X64 (AMD64) or ARM64
TARGET_PLATFORMS="${{ inputs.docker-build-platforms }}"
echo "Runner CPU Architecture: $RUNNER_ARCH"
echo "Requested Build Platforms: $TARGET_PLATFORMS"
# Check for AMD64 mismatch (Runner is X64, but user requests ONLY arm64, OR user requests multi-arch which requires emulation)
if [[ "$RUNNER_ARCH" == "X64" ]]; then
if [[ "$TARGET_PLATFORMS" == *"linux/arm64"* ]]; then
echo "::error::Runner is X64 (Intel/AMD) but build includes 'linux/arm64'. This requires emulation. Aborting strictly."
exit 1
fi
fi
# Check for ARM64 mismatch
if [[ "$RUNNER_ARCH" == "ARM64" ]]; then
if [[ "$TARGET_PLATFORMS" == *"linux/amd64"* ]]; then
echo "::error::Runner is ARM64 (Apple Silicon/Graviton) but build includes 'linux/amd64'. This requires emulation. Aborting strictly."
exit 1
fi
fi
echo "Architecture match verified for native build ✅"
- name: Set up Docker Buildx
if: inputs.docker-username != '' && inputs.docker-password != ''
uses: docker/setup-buildx-action@v3
- name: Login to Registry
if: inputs.docker-username != '' && inputs.docker-password != ''
uses: docker/login-action@v3
with:
registry: ${{ inputs.docker-registry }}
username: ${{ inputs.docker-username }}
password: ${{ inputs.docker-password }}
- name: Build
id: docker_build
if: steps.preparation.outputs.build == 'true' && inputs.docker-username != '' && inputs.docker-password != ''
uses: docker/build-push-action@v6
with:
context: ${{ inputs.working-directory }}
push: ${{ inputs.docker-build-outputs == '' && steps.preparation.outputs.push || 'false' }}
file: ${{ inputs.working-directory }}/${{ inputs.docker-file }}
target: ${{ inputs.docker-build-target }}
build-args: ${{ inputs.docker-build-args }}
tags: ${{ steps.preparation.outputs.tag_list }}
secrets: ${{ inputs.docker-build-secrets }}
secret-files: ${{ inputs.docker-build-secret-files }}
platforms: ${{ inputs.docker-build-platforms }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: ${{ inputs.docker-build-provenance }}
outputs: ${{ inputs.docker-build-outputs }}
- name: Retag Existing Image
id: docker_retag
if: steps.preparation.outputs.build == 'false'
shell: bash
run: |
CHECK_EXISTING_TAGS="master-${GITHUB_SHA::8} main-${GITHUB_SHA::8}"
# Accept both single-arch manifests and multi-arch manifest lists/indexes
ACCEPT_HEADER="application/vnd.docker.distribution.manifest.v2+json, application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.oci.image.manifest.v1+json, application/vnd.oci.image.index.v1+json"
echo "CHECK_EXISTING_TAGS: ${CHECK_EXISTING_TAGS}"
echo "RELEASE_TAG: ${RELEASE_TAG:1}"
echo "Check if an image already exists for ${{ inputs.docker-image }}:main|master-${GITHUB_SHA::8} 🐋 ⬇"
foundImage=false
DETECTED_CONTENT_TYPE=""
DIGEST=""
end=$((SECONDS+300))
while [ $SECONDS -lt $end ]; do
MANIFEST=""
for tag in $CHECK_EXISTING_TAGS; do
# Dump headers to file to extract Content-Type and Digest later
MANIFEST=$(curl -s -D headers.txt -H "Accept: ${ACCEPT_HEADER}" -u '${{ inputs.docker-username }}:${{ inputs.docker-password }}' "${{ inputs.docker-registry-api }}${{ inputs.docker-image}}/manifests/${tag}")
if [[ $MANIFEST == *"errors"* ]]; then
echo "No image found for ${{ inputs.docker-image }}:${tag} 🚫"
continue
else
echo "Image found for ${{ inputs.docker-image }}:${tag} 🐋 ⬇"
foundImage=true
# Extract the Content-Type returned by registry
DETECTED_CONTENT_TYPE=$(grep -i "^Content-Type:" headers.txt | cut -d' ' -f2 | tr -d '\r')
# Extract the correct digest from headers (works for lists and single images)
DIGEST=$(grep -i "^Docker-Content-Digest:" headers.txt | cut -d' ' -f2 | tr -d '\r')
break 2
fi
done
sleep 10
done
if [[ $foundImage == false ]]; then
echo "No image found for ${{ inputs.docker-image }}:main|master-${GITHUB_SHA::8} 🚫 within 300 seconds"
exit 1
fi
echo "Retagging image with release version and :latest tags for ${{ inputs.docker-image }} 🏷"
echo "Using Content-Type: ${DETECTED_CONTENT_TYPE}"
# Use the detected Content-Type to PUT the manifest back
curl --fail-with-body -X PUT -H "Content-Type: ${DETECTED_CONTENT_TYPE}" -u '${{ inputs.docker-username }}:${{ inputs.docker-password }}' -d "${MANIFEST}" "${{ inputs.docker-registry-api }}${{ inputs.docker-image}}/manifests/${{ steps.preparation.outputs.tag }}"
curl --fail-with-body -X PUT -H "Content-Type: ${DETECTED_CONTENT_TYPE}" -u '${{ inputs.docker-username }}:${{ inputs.docker-password }}' -d "${MANIFEST}" "${{ inputs.docker-registry-api }}${{ inputs.docker-image}}/manifests/${{ steps.preparation.outputs.latest }}"
echo "digest=$DIGEST" >> $GITHUB_OUTPUT
- name: Checkout GitOps Repository
if: inputs.gitops-token != ''
uses: actions/checkout@v6
with:
repository: ${{ inputs.gitops-organization }}/${{ inputs.gitops-repository }}
token: ${{ inputs.gitops-token }}
path: .github/${{ inputs.gitops-repository }}
- name: Update Docker Image in Repository
if: inputs.gitops-token != ''
working-directory: .github/${{ inputs.gitops-repository }}
shell: bash
run: |
push_to_gitops_repo () {
# In case there was another push in the meantime, we pull it again
git pull --rebase https://${{ inputs.gitops-user }}:${{ inputs.gitops-token }}@github.com/${{ inputs.gitops-organization }}/${{ inputs.gitops-repository }}.git
git push https://${{ inputs.gitops-user }}:${{ inputs.gitops-token }}@github.com/${{ inputs.gitops-organization }}/${{ inputs.gitops-repository }}.git
}
commit_changes () {
if [[ ${{ steps.preparation.outputs.push }} == "true" ]]; then
git add .
# commit with no errors if there are no changes
if git diff-index --quiet HEAD; then
echo "There were no changes..."
return
fi
git commit -m "Release ${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}"
# retry push attempt since rejections can still happen (even with pull before push)
push_to_gitops_repo || push_to_gitops_repo || push_to_gitops_repo
fi
}
# configure git user
git config --global user.email "${{ inputs.gitops-email }}" && git config --global user.name "${{ inputs.gitops-user }}"
if [[ ( $GITHUB_REF == refs/heads/master || $GITHUB_REF == refs/heads/main ) && -n "${{ inputs.gitops-stage }}" ]]; then
echo "Run update for STAGE"
while IFS= read -r line; do
array=($line)
echo "Check if path $line exists and get old current version"
yq -e .${array[1]} ${array[0]}
echo "Run update $line ${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}"
yq -i .${array[1]}=\"${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}\" ${array[0]}
done <<< "${{ inputs.gitops-stage }}"
commit_changes
elif [[ $GITHUB_REF == refs/heads/dev && -n "${{ inputs.gitops-dev }}" ]]; then
echo "Run update for DEV"
while IFS= read -r line; do
array=($line)
echo "Check if path $line exists and get old current version"
yq -e .${array[1]} ${array[0]}
echo "Run update $line ${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}"
yq -i .${array[1]}=\"${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}\" ${array[0]}
done <<< "${{ inputs.gitops-dev }}"
commit_changes
elif [[ $GITHUB_REF == refs/tags/* && -n "${{ inputs.gitops-prod }}" ]]; then
echo "Run update for PROD"
while IFS= read -r line; do
array=($line)
echo "Check if path $line exists and get old current version"
yq -e .${array[1]} ${array[0]}
echo "Run update $line ${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}"
yq -i .${array[1]}=\"${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}\" ${array[0]}
done <<< "${{ inputs.gitops-prod }}"
commit_changes
elif [[ -n "${{ inputs.gitops-dev }}" ]]; then
echo "Simulate update for DEV"
while IFS= read -r line; do
array=($line)
echo "Check if path $line exists and get old current version"
yq -e .${array[1]} ${array[0]}
echo "Run update $line ${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}"
yq -i .${array[1]}=\"${{ inputs.docker-registry }}/${{ inputs.docker-image }}:${{ steps.preparation.outputs.tag }}\" ${array[0]}
done <<< "${{ inputs.gitops-dev }}"
fi
- name: Emit Image Build Event to Upwind.io
env:
UPWIND_CLIENT_SECRET: ${{ inputs.upwind-client-secret }}
if: "${{ inputs.upwind-client-id != '' && env.UPWIND_CLIENT_SECRET != '' && inputs.upwind-organization-id != '' }}"
uses: upwindsecurity/create-image-build-event-action@v3
continue-on-error: true
with:
image: ${{ inputs.docker-image }}
image_sha: ${{ steps.docker_build.outputs.digest || steps.docker_retag.outputs.digest }}
upwind_client_id: ${{ inputs.upwind-client-id }}
upwind_client_secret: ${{ env.UPWIND_CLIENT_SECRET }}
upwind_organization_id: ${{ inputs.upwind-organization-id }}
branding:
icon: 'git-merge'
color: 'blue'