generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
240 lines (211 loc) · 8.39 KB
/
images.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
---
name: Images
on:
pull_request:
branches:
- main
paths:
- images/**
push:
branches:
- main
paths:
- images/**
permissions: {}
jobs:
detect-changes:
name: Detect Changes
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
images: ${{ steps.detect_changes.outputs.changes }}
steps:
- name: Checkout
id: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Build path-filters file
id: build_path_filters
run: bash scripts/path-filter/configuration-generator.sh images
- name: Detect changes
id: detect_changes
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
with:
filters: .github/path-filters/images.yml
preflight-checks:
needs: [detect-changes]
if: ${{ needs.detect-changes.outputs.images != '[]' && github.ref != 'refs/heads/main' }}
name: Preflight Checks
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
packages: read
strategy:
fail-fast: false
matrix:
image: ${{ fromJson(needs.detect-changes.outputs.images) }}
steps:
- name: Checkout
id: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Check Version
id: check_version
env:
GH_TOKEN: ${{ github.token }}
run: |
echo "${{ env.GH_TOKEN }}" | skopeo login ghcr.io --username ${{ github.actor }} --password-stdin
version=$(jq -r '.version' images/${{ matrix.image }}/config.json)
export version
if skopeo list-tags docker://ghcr.io/ministryofjustice/devcontainer-${{ matrix.image }}; then
image_exists=true
else
echo "image_exists=false" >>"${GITHUB_ENV}"
echo "tag_exists=false" >>"${GITHUB_ENV}"
fi
if [[ "${image_exists}" == "true" ]]; then
checkTag=$(skopeo list-tags docker://ghcr.io/ministryofjustice/devcontainer-${{ matrix.image }} | jq -r --arg version "${version}" '.Tags | index($version)')
export checkTag
if [[ -z "${imageTag}" ]]; then
echo "tag_exists=false" >>"${GITHUB_ENV}"
else
echo "tag_exists=true" >>"${GITHUB_ENV}"
fi
fi
- name: Check CHANGELOG Updates
id: check_changelog_updates
env:
GH_TOKEN: ${{ github.token }}
run: |
mainSha=$(gh api --method GET /repos/"${GITHUB_REPOSITORY}"/contents/images/${{ matrix.image }}/CHANGELOG.md --field ref="main" | jq -r '.sha')
branchSha=$(gh api --method GET /repos/"${GITHUB_REPOSITORY}"/contents/images/${{ matrix.image }}/CHANGELOG.md --field ref="${GITHUB_HEAD_REF}" | jq -r '.sha')
if [[ -z "${mainSha}" ]]; then
SHA not found for main branch, assuming CHANGELOG.md does not exist
elif [[ -z "${branchSha}" ]]; then
SHA not found for "${GITHUB_HEAD_REF}" branch, assuming CHANGELOG.md does not exist
"changelog_updated=false" >>"${GITHUB_ENV}"
elif [[ "${mainSha}" == "${branchSha}" ]]; then
echo "CHANGELOG.md matches main branch, needs to be updated"
echo "changelog_updated=false" >>"${GITHUB_ENV}"
elif [[ "${mainSha}" != "${branchSha}" ]]; then
echo "CHANGELOG.md does not match main branch, does not need to be updated"
echo "changelog_updated=true" >>"${GITHUB_ENV}"
fi
- name: Evaluate Checks
id: evaluate_checks
run: |
if [[ "${{ env.tag_exists }}" == "true" ]]; then
echo "::error::FAIL: Container tag already exists"
export failBuild="true"
else
echo "::notice::OK: Container tag does not exist"
export failBuild="false"
fi
if [[ "${{ env.changelog_updated }}" == "true" ]]; then
echo "::notice::OK: CHANGELOG.md has been updated"
export failBuild="false"
elif [[ "${{ env.changelog_updated }}" == "false" ]]; then
echo "::error::FAIL: CHANGELOG.md needs to be updated"
export failBuild="true"
fi
if [[ "${failBuild}" == "true" ]]; then
exit 1
fi
build-test-scan:
needs: [detect-changes]
if: ${{ needs.detect-changes.outputs.images != '[]' && github.ref != 'refs/heads/main' }}
name: Build and Test
runs-on: ubuntu-latest
permissions:
contents: read
strategy:
fail-fast: false
matrix:
image: ${{ fromJson(needs.detect-changes.outputs.images) }}
platform: [linux/amd64, linux/arm64]
steps:
- name: Checkout
id: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Set Up Container Structure Test
id: setup_container_structure_test
uses: ministryofjustice/github-actions/setup-container-structure-test@db1a54895bf5fb975c60af47e5a3aab96505ca3e # v18.6.0
- name: Set Up QEMU
id: setup_qemu
uses: docker/setup-qemu-action@53851d14592bedcffcf25ea515637cff71ef929a # v3.3.0
- name: Set Up Docker Buildx
id: setup_docker_buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0
- name: Build Image
uses: docker/build-push-action@b32b51a8eda65d6793cd0494a773d4f6bcef32dc # v6.11.0
with:
file: images/${{ matrix.image }}/Dockerfile
context: images/${{ matrix.image }}
platforms: ${{ matrix.platform }}
load: true
tags: ghcr.io/ministryofjustice/devcontainer-${{ matrix.image }}:${{ github.sha }}
- name: Container Structure Test
id: container_structure_test
run: |
container-structure-test test \
--platform ${{ matrix.platform }} \
--config images/${{ matrix.image }}/test/container-structure-test.yml \
--image ghcr.io/ministryofjustice/devcontainer-${{ matrix.image }}:${{ github.sha }}
- name: Scan Image
id: scan_image
uses: aquasecurity/trivy-action@18f2510ee396bbf400402947b394f2dd8c87dbb0 # 0.29.0
env:
TRIVY_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-db:2
TRIVY_JAVA_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-java-db:1
with:
scan-type: image
image-ref: ghcr.io/ministryofjustice/devcontainer-${{ matrix.image }}:${{ github.sha }}
exit-code: 1
severity: HIGH,CRITICAL
ignore-unfixed: true
hide-progress: true
publish:
needs: [detect-changes]
if: ${{ needs.detect-changes.outputs.images != '[]' && github.ref == 'refs/heads/main' }}
name: Publish
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
packages: write
strategy:
fail-fast: false
matrix:
image: ${{ fromJson(needs.detect-changes.outputs.images) }}
steps:
- name: Checkout
id: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Set Up QEMU
id: setup_qemu
uses: docker/setup-qemu-action@53851d14592bedcffcf25ea515637cff71ef929a # v3.3.0
- name: Set Up Docker Buildx
id: setup_docker_buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0
- name: Log in to GitHub Container Registry
id: login_ghcr
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Prepare Environment
id: prepare_environment
run: |
version=$(jq -r '.version' images/${{ matrix.image }}/config.json)
echo "version=${version}" >>"${GITHUB_ENV}"
- name: Publish Image
uses: docker/build-push-action@b32b51a8eda65d6793cd0494a773d4f6bcef32dc # v6.11.0
with:
file: images/${{ matrix.image }}/Dockerfile
context: images/${{ matrix.image }}
platforms: linux/amd64,linux/arm64
push: true
tags: |
ghcr.io/ministryofjustice/devcontainer-${{ matrix.image }}:${{ env.version }}
ghcr.io/ministryofjustice/devcontainer-${{ matrix.image }}:latest