-
Notifications
You must be signed in to change notification settings - Fork 1
249 lines (228 loc) · 9.1 KB
/
builder.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
---
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Build & publish images v2
on:
workflow_call:
inputs:
appsToBuild:
required: false
type: string
default: ''
channelsToBuild:
required: false
type: string
default: ""
pushImages:
required: false
default: false
type: boolean
force:
required: false
default: true
type: boolean
description: Force rebuild
secrets:
RIVERBOT_APP_ID:
description: The App ID of the GitHub App
required: true
RIVERBOT_APP_PRIVATE_KEY:
description: The private key of the GitHub App
required: true
jobs:
prepare-matrix:
runs-on: "ubuntu-latest"
outputs:
matrices: ${{ steps.prepare-matrices.outputs.matrices }}
name: Go - Prepare matrices
steps:
- name: Generate Token
uses: actions/create-github-app-token@c1a285145b9d317df6ced56c09f525b5c2b6f755 # v1.11.1
id: app-token
with:
app-id: "${{ secrets.RIVERBOT_APP_ID }}"
private-key: "${{ secrets.RIVERBOT_APP_PRIVATE_KEY }}"
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
token: "${{ steps.app-token.outputs.token }}"
fetch-depth: 1
- name: Setup Go
uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5
with:
go-version-file: go.mod
- name: Get dependencies
run: |
go mod download
- name: Matrices
id: prepare-matrices
shell: bash
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }}
run: |
if [[ -z "${{ inputs.appsToBuild }}" ]]; then
matrices=$(go run cmd/main.go "all" "${{inputs.pushImages}}" "${{inputs.force}}" "stable")
else
if [[ -z "${{ inputs.channelsToBuild }}" ]]; then
matrices=$(go run cmd/main.go "${{ inputs.appsToBuild }}" "${{inputs.pushImages}}" "${{inputs.force}}" "stable")
else
matrices=$(go run cmd/main.go "${{ inputs.appsToBuild }}" "${{inputs.pushImages}}" "${{inputs.force}}" "${{inputs.channelsToBuild}}")
fi
fi
echo "matrices=${matrices}" >> $GITHUB_OUTPUT
echo "${matrices}"
build-and-push-image:
name: Build ${{ matrix.image.name }} (${{ matrix.image.platform }})
needs: prepare-matrix
runs-on: ubuntu-latest
if: ${{ toJSON(fromJSON(needs.prepare-matrix.outputs.matrices).image_platforms) != '[]' && toJSON(fromJSON(needs.prepare-matrix.outputs.matrices).image_platforms) != '' }}
strategy:
fail-fast: false
matrix:
image: ["${{ fromJSON(needs.prepare-matrix.outputs.matrices).image_platforms }}"]
permissions:
contents: read
packages: write
steps:
- name: Lowercase repository owner
shell: bash
run: echo "LOWERCASE_REPO_OWNER=${GITHUB_REPOSITORY_OWNER,,}" >> "${GITHUB_ENV}"
- name: Log Matrix Input
shell: bash
run: |
cat << EOF
${{ toJSON(matrix.image)}}
EOF
- name: Validate Matrix Input
shell: bash
run: |
if [[ -z "${{ matrix.image.name }}" ]]; then
echo "image.name is empty"
exit 1
fi
if [[ -z "${{ matrix.image.version }}" ]]; then
echo "image.version is empty"
exit 1
fi
if [[ -z "${{ matrix.image.context }}" ]]; then
echo "image.context is empty"
exit 1
fi
if [[ -z "${{ matrix.image.dockerfile }}" ]]; then
echo "image.dockerfile is empty"
exit 1
fi
if [[ -z "${{ matrix.image.platform }}" ]]; then
echo "image.platform is empty"
exit 1
fi
echo "${{ matrix.image.name }}" | grep -E "[a-zA-Z0-9_\.\-]+" || "Image Name is invalid"
echo "${{ matrix.image.version }}" | grep -E "[a-zA-Z0-9_\.\-]+" || "Image Version is invalid"
- name: Generate Token
uses: actions/create-github-app-token@c1a285145b9d317df6ced56c09f525b5c2b6f755 # v1.11.1
id: app-token
with:
app-id: "${{ secrets.RIVERBOT_APP_ID }}"
private-key: "${{ secrets.RIVERBOT_APP_PRIVATE_KEY }}"
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
token: "${{ steps.app-token.outputs.token }}"
fetch-depth: 1
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0
- name: Prepare Build Outputs
id: prepare-build-outputs
shell: bash
run: |
image_name="ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{ matrix.image.name }}"
outputs="type=image,name=${image_name},push-by-digest=true,name-canonical=true,push=true"
echo "image_name=${image_name}" >> $GITHUB_OUTPUT
echo "outputs=${outputs}" >> $GITHUB_OUTPUT
- name: Login to GitHub Container Registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Push
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
id: build
with:
build-args: |-
VERSION=${{ matrix.image.version }}
REVISION=${{ github.sha }}
TARGETPLATFORM=${{ matrix.image.platform }}
CHANNEL=${{ matrix.image.channel }}
context: ${{ matrix.image.context }}
file: ${{ matrix.image.dockerfile }}
push: ${{ github.event_name != 'pull_request' }}
platforms: ${{ matrix.image.platform }}
outputs: ${{ steps.prepare-build-outputs.outputs.outputs }}
cache-from: type=gha
cache-to: type=gha,mode=max
labels: |-
org.opencontainers.image.title=${{ matrix.image.name }}
org.opencontainers.image.url=https://ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{ matrix.image.name }}
org.opencontainers.image.source=https://github.com/${{ env.LOWERCASE_REPO_OWNER }}/containers
org.opencontainers.image.version=${{ matrix.image.version }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.vendor=${{ env.LOWERCASE_REPO_OWNER }}
- name: Export Digest
id: export-digest
if: ${{ inputs.pushImages }}
shell: bash
run: |
mkdir -p /tmp/${{ matrix.image.name }}/digests
digest="${{ steps.build.outputs.digest }}"
echo "${{ matrix.image.name }}" > "/tmp/${{ matrix.image.name }}/digests/${digest#sha256:}"
- name: Upload Digest
if: ${{ inputs.pushImages }}
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.image.name }}-${{ matrix.image.target_os }}-${{ matrix.image.target_arch }}
path: /tmp/${{ matrix.image.name }}/*
if-no-files-found: error
retention-days: 1
merge:
name: Merge ${{ matrix.image.name }}
runs-on: ubuntu-latest
needs: ["prepare-matrix", "build-and-push-image"]
if: ${{ always() && inputs.pushImages && toJSON(fromJSON(needs.prepare-matrix.outputs.matrices).images) != '[]' && toJSON(fromJSON(needs.prepare-matrix.outputs.matrices).images) != '' }}
strategy:
matrix:
image: ["${{ fromJSON(needs.prepare-matrix.outputs.matrices).images }}"]
fail-fast: false
steps:
- name: Lowercase repository owner
shell: bash
run: echo "LOWERCASE_REPO_OWNER=${GITHUB_REPOSITORY_OWNER,,}" >> "${GITHUB_ENV}"
- name: Download Digests
uses: actions/download-artifact@v4
with:
pattern: ${{ matrix.image.name }}-*
path: /tmp/${{ matrix.image.name }}
merge-multiple: true
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0
- name: Login to GitHub Container Registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
with:
registry: ghcr.io
username: "${{ github.actor }}"
password: "${{ secrets.GITHUB_TOKEN }}"
- name: Log Files
working-directory: /tmp/${{ matrix.image.name }}/digests
shell: bash
run: |
ls -la
cat *
- name: Merge Manifests
id: merge
working-directory: /tmp/${{ matrix.image.name }}/digests
env:
TAGS: ${{ toJSON(matrix.image.tags) }}
shell: bash
run: |
docker buildx imagetools create $(jq -cr '. | map("-t ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{matrix.image.name}}:" + .) | join(" ")' <<< "$TAGS") \
$(printf 'ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{ matrix.image.name }}@sha256:%s ' *)