-
Notifications
You must be signed in to change notification settings - Fork 0
191 lines (167 loc) · 7.21 KB
/
build.yml
File metadata and controls
191 lines (167 loc) · 7.21 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
# Adapted from https://dev.to/cloudx/multi-arch-docker-images-the-easy-way-with-github-actions-4k54
name: Build
on:
workflow_dispatch:
inputs:
scope:
required: true
type: choice
description: The version bump for all images
options:
- major
- minor
- patch
push:
branches:
- main
pull_request:
permissions:
packages: write
# We need the builds to run one after another chronologically
# If they don't, an image for a newer commit might get an older version.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
jobs:
generate-matrix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 2
- name: Get changed images
if: ${{ github.event_name != 'workflow_dispatch' }}
id: changed-files
uses: tj-actions/changed-files@24d32ffd492484c1d75e0c0b894501ddb9d30d62 # v47.0.0
with:
path: images
dir_names: true
dir_names_max_depth: 1
json: true
- name: Get all images
id: all-files
if: ${{ github.event_name == 'workflow_dispatch' }}
run: echo images=$(find images/ -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | jq -R -s -c 'split("\n") | map(select(length > 0)) | tostring') >> $GITHUB_OUTPUT
- name: Set matrix
id: matrix
run: |
if [[ "${{ github.event_name }}" != "workflow_dispatch" ]]; then
echo matrix=${{ steps.changed-files.outputs.all_modified_files }}
echo matrix=${{ steps.changed-files.outputs.all_modified_files }} >> $GITHUB_OUTPUT
else
echo matrix=${{ steps.all-files.outputs.images }}
echo matrix=${{ steps.all-files.outputs.images }} >> $GITHUB_OUTPUT
fi
outputs:
matrix: ${{ steps.matrix.outputs.matrix }}
build:
runs-on: ubuntu-latest
needs: generate-matrix
if: ${{ needs.generate-matrix.outputs.matrix != '[]' }}
strategy:
fail-fast: false
matrix:
image: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
# This is needed to skip the job when an image is deleted
- name: Check if directory still exists
id: dir-check
run: |
if [ -d images/${{ matrix.image }} ]; then
echo exists=true >> $GITHUB_OUTPUT
else
echo exists=false >> $GITHUB_OUTPUT
fi
- name: Install semver tool
run: wget -O /usr/local/bin/semver https://raw.githubusercontent.com/fsaintjacques/semver-tool/3.4.0/src/semver && chmod +x /usr/local/bin/semver
- name: Set up QEMU
if: ${{ steps.dir-check.outputs.exists == 'true' }}
uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0
- name: Set up Docker Buildx
if: ${{ steps.dir-check.outputs.exists == 'true' }}
id: buildx
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
- name: Login to GHCR
if: steps.dir-check.outputs.exists == 'true' && (github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'push-image'))
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1 # v3.5.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get most recent tag
id: tag
# Some of our older tags are "sha-${checksum}", so we need to ignore them
# If no tags are published, assume 0.0.0 as old tag
run: |
tag=$(skopeo list-tags "docker://ghcr.io/community-tooling/oci-images/${{ matrix.image }}" | jq -r '.Tags | sort | .[] | select(contains("sha-") | not)' | grep -E '[0-9]+\.[0-9]+\.[0-9]+' | tail -n 1)
if [[ $tag -eq "" ]]; then
tag="0.0.0"
fi
echo tag="$tag" >> $GITHUB_OUTPUT
- name: Get commit scope
id: commit-scope
env:
# Use the workflow_dispatch input on workflow_dispatch
SCOPE: ${{ github.event.inputs.scope }}
# Use the PR title if it exists since we use this as the commit message on squashing (only exists on PRs)
# Use the head_commit message on all other events (does not exist on workflow_dispatch)
MESSAGE: ${{ github.event.pull_request.title || github.event.head_commit.message }}
run: |
if [[ $SCOPE != "" ]]; then
echo "Running with workflow_dispatch, using input directly"
echo scope=$SCOPE >> $GITHUB_OUTPUT
exit 0
fi
scope=$(echo $MESSAGE | head -n 1 | sed -E 's/^.*\(([a-z]+)\).*$/\1/g')
if [[ "$scope" =~ ^(major|minor|patch)$ ]]; then
echo scope=$scope >> $GITHUB_OUTPUT
else
echo "::error title=Invalid commit scope::The commit scope is not an allowed value, check the README section on versioning. The PR title must have the correct scope."
exit 1
fi
- name: Generate tag
if: ${{ steps.dir-check.outputs.exists == 'true' }}
id: version
run: |
echo version="$(semver bump ${{ steps.commit-scope.outputs.scope }} ${{ steps.tag.outputs.tag }})" >> $GITHUB_OUTPUT
- name: Docker meta
if: ${{ steps.dir-check.outputs.exists == 'true' }}
id: meta
uses: docker/metadata-action@c1e51972afc2121e065aed6d45c65596fe445f3f # v5.8.0
with:
flavor: |
latest=false
images: |
ghcr.io/community-tooling/oci-images/${{ matrix.image }}
# If we're in a PR and pushing for the PR, use the SHA sum as tag, else use the defined version
tags: |
type=raw,value=${{ contains(github.event.pull_request.labels.*.name, 'push-image') && github.sha || steps.version.outputs.version }}
- name: Build and push
if: ${{ steps.dir-check.outputs.exists == 'true' }}
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
with:
context: images/${{ matrix.image }}
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name == 'workflow_dispatch' || github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'push-image') }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# This is used so that we have one job that is successful once all the matrix builds are done
build-skip:
runs-on: ubuntu-latest
needs: generate-matrix
if: ${{ needs.generate-matrix.outputs.matrix == '[]' }}
steps:
- run: echo "No images changed, no build necessary"
# This is used so that we have one job that is successful once all the matrix builds are done
build-results:
if: ${{ always() }}
runs-on: ubuntu-latest
needs: [build, build-skip]
steps:
- run: exit 1
# see https://stackoverflow.com/a/67532120/4907315
if: >-
${{
contains(needs.*.result, 'failure')
|| contains(needs.*.result, 'cancelled')
}}