-
Notifications
You must be signed in to change notification settings - Fork 43
428 lines (359 loc) · 13.4 KB
/
build-and-push-services.yml
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# Copyright 2022 Open Ag Data Alliance
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Build and push OADA images
permissions: read-all
on:
push:
pull_request:
jobs:
setup:
runs-on: ubuntu-latest
outputs:
services: ${{ steps.services.outputs.list }}
version: ${{ steps.ref.outputs.version }}
release: ${{ steps.ref.outputs.release }}
major: ${{ steps.semver.outputs.major }}
minor: ${{ steps.semver.outputs.minor }}
patch: ${{ steps.semver.outputs.patch }}
prerelease: ${{ steps.semver.outputs.prerelease }}
build: ${{ steps.semver.outputs.build }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Find Services
id: services
run: |
SERVICES=$(ls -1 oada/services | tr '\n' ',' | sed 's/,/","/g' | sed 's/^/["/' | sed 's/,"$/]/')
echo list="${SERVICES}" >> "$GITHUB_OUTPUT"
- name: Parse Ref
id: ref
run: |
echo 'Processing git ref:' $GITHUB_REF
# Release version is just the release number
if [[ $GITHUB_REF == refs/heads/release/* ]]; then
VERSION=${GITHUB_REF#refs/heads/release/}
#RELEASE=true
elif [[ $GITHUB_REF == refs/tags/* ]]; then
if [[ $GITHUB_REF == refs/tags/v* ]]; then
VERSION=${GITHUB_REF#refs/tags/v}
RELEASE=true
else
VERSION=tag-${GITHUB_REF#refs/tags/}
fi
# Branch version is branch name (with '/' -> '-')
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
# Expect for the default_branch, which gets version "edge"
if [ "$VERSION" == "${{ github.event.repository.default_branch }}" ]; then
VERSION=edge
fi
# PR versions are pr-<github pr number>
elif [[ $GITHUB_REF == refs/pull/* ]]; then
VERSION=pr-${{ github.event.number }}
else
echo ::error ::Can not determine version of service -- unexpected job trigger? Stopping.
exit 1
fi
echo version="${VERSION}" >> "$GITHUB_OUTPUT"
echo release="${RELEASE}" >> "$GITHUB_OUTPUT"
- name: Parse Semver
id: semver
if: ${{ steps.ref.outputs.release }}
uses: booxmedialtd/[email protected]
with:
input_string: ${{ steps.ref.outputs.version }}
#version_extractor_regex: '\/v(.*)$'
prebuild:
name: Build and cache shared Docker layers
runs-on: ubuntu-latest
# Only run one build at a time to fix layer reuse?
concurrency: prebuild-${{ github.sha }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/[email protected]
- name: Set up Docker Buildx
id: buildx
uses: docker/[email protected]
with:
version: latest
- name: Cache docker layers
uses: actions/cache@v4
id: cache
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}-oada
restore-keys: |
${{ runner.os }}-buildx-${{ github.sha }}-
${{ runner.os }}-buildx-
- name: Build base OADA images
uses: docker/[email protected]
with:
context: oada
file: oada/Dockerfile
platforms: linux/amd64
build-args: |
GIT_REF=${{ github.sha }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- # Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
build-and-push-services:
name: Build and Push images to DockerHub and GHCR
needs:
- setup
- prebuild
permissions:
id-token: write
packages: write
strategy:
matrix:
service: ${{ fromJson(needs.setup.outputs.services) }}
context:
- oada
include:
- service: support/proxy
context: support/proxy
- service: support/proxy-http-only
context: support/proxy
target: http-only
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/[email protected]
- name: Set up Docker Buildx
id: buildx
uses: docker/[email protected]
with:
version: latest
- name: Cache docker layers
uses: actions/cache@v4
id: cache
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}-${{ matrix.service }}
restore-keys: |
${{ runner.os }}-buildx-${{ github.sha }}-
${{ runner.os }}-buildx-
- name: Prepare Images
id: images
run: |
# Doesn't like upper case
OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')
# Name on DockerHub (doesn't like extra / in images?)
DOCKER_IMAGE=${OWNER}/$(echo ${{ matrix.service }} | tr '/' '-')
# Name on GHCR
GHCR_IMAGE=ghcr.io/${OWNER}/${{ matrix.service}}
echo dockerhub="${DOCKER_IMAGE}" >> "$GITHUB_OUTPUT"
echo ghcr="${GHCR_IMAGE}" >> "$GITHUB_OUTPUT"
- name: Login to DockerHub
#if: github.event_name != 'pull_request'
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GitHub Container Registry
#if: github.event_name != 'pull_request'
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ steps.images.outputs.dockerhub }}
${{ steps.images.outputs.ghcr}}
labels: |
org.opencontainers.image.title=${{ matrix.service }}
org.opencontainers.image.description=An OADA microservice
# Don't update latest on prereleases?
flavor: latest=${{ !!(needs.setup.outputs.release && !needs.setup.outputs.prerelease) }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=schedule,pattern=nightly
type=edge
type=ref,event=pr
type=sha
- name: Build and push to images
uses: docker/[email protected]
with:
context: ${{ matrix.context }}
file: ${{ matrix.context }}/Dockerfile
target: ${{ matrix.target }}
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
build-args: |
GIT_REF=${{ github.sha }}
OADA_SERVICE=${{ matrix.service }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- # Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
# Use Snyk to check service docker images
snyk-services:
name: Snyk Checks
needs:
- setup
- build-and-push-services
permissions:
security-events: write
strategy:
matrix:
service: ${{ fromJson(needs.setup.outputs.services) }}
context:
- oada
include:
- service: support/proxy
context: support/proxy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Find Docker tag for Snyk
id: tag
run: |
# Doesn't like upper case
OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')
# Name on DockerHub (doesn't like extra / in images?)
DOCKER_IMAGE=${OWNER}/$(echo ${{ matrix.service }} | tr '/' '-')
# Name on GHCR
#GHCR_IMAGE=ghcr.io/${OWNER}/${{ matrix.service}}
TAG="${DOCKER_IMAGE}:edge"
if [[ "${{ needs.setup.outputs.release }}" ]]; then
if [[ "${{ needs.setup.outputs.prerelease }}" ]]; then
TAG="${DOCKER_IMAGE}:edge"
else
TAG="${DOCKER_IMAGE}:latest"
fi
fi
echo tag="${TAG}" >> "$GITHUB_OUTPUT"
echo org="${OWNER}" >> "$GITHUB_OUTPUT"
echo cur="${GHCR_IMAGE}:sha-${GITHUB_SHA::8}" >> "$GITHUB_OUTPUT"
- name: Monitor Service image with Snyk
uses: snyk/actions/docker@master
# Don't break workflow on errors?
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
command: container monitor
image: ${{ steps.tag.outputs.tag }}
args: --org=${{ steps.tag.outputs.org }} --file=${{ matrix.context }}/Dockerfile
- name: Test current Service image with Snyk
uses: snyk/actions/docker@master
# Don't break workflow on errors?
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
image: ${{ steps.tag.outputs.tag }}
args: --org=${{ steps.tag.outputs.org }} --file=${{ matrix.context }}/Dockerfile
- name: Replace security-severity undefined for license-related findings
run: |
sed -i 's/"security-severity": "undefined"/"security-severity": "0"/g' snyk.sarif
sed -i 's/"security-severity": "null"/"security-severity": "0"/g' snyk.sarif
- name: Secure Code Warrior
uses: SecureCodeWarrior/github-action-add-sarif-contextual-training@v1
with:
inputSarifFile: ./snyk.sarif
outputSarifFile: ./securecodewarrior.sarif
githubToken: ${{ secrets.GITHUB_TOKEN }}
- name: Upload SARIF file to GitHub Code Scanning
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ./securecodewarrior.sarif
#sarif_file: ./snyk.sarif
# Create a "release" compose file and attack it to a draft GitHub release.
# TODO: Disallow release if high severity vulnerabilities, failing tests, etc.?
create-release:
needs:
- setup
- build-and-push-services
permissions:
contents: write
discussions: write
if: ${{ needs.setup.outputs.release }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up yq
uses: chrisdickinson/setup-yq@latest
with:
yq-version: v4.25.2
- name: Login to DockerHub
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get Release Compose File
id: release
run: |
RELEASE=docker-compose.release.yml
# Use commit of release as version??
OADA_VERSION="sha-${GITHUB_SHA::7}" \
RELEASE_VERSION="${{ needs.setup.outputs.version }}" \
./release/make.sh | tee $RELEASE
echo composefile="${RELEASE}" >> "$GITHUB_OUTPUT"
# TODO: More detailed check?
- name: Verify Compose File
run: docker-compose -f ${{ steps.release.outputs.composefile }} config
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
name: OADA v${{ needs.setup.outputs.version }}
# Make draft and wait for person to release it?
draft: true
prerelease: ${{ !!needs.setup.outputs.prerelease }}
token: ${{ secrets.GITHUB_TOKEN}}
- name: Upload Release Compose File
id: upload-release-asset
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ steps.release.outputs.composefile }}
asset_name: docker-compose.yml
asset_content_type: application/yaml