-
Notifications
You must be signed in to change notification settings - Fork 0
240 lines (214 loc) · 9.31 KB
/
docker-build-images.yml
File metadata and controls
240 lines (214 loc) · 9.31 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
name: Build Docker images
on:
workflow_call:
inputs:
runs-on:
description: |
JSON array of runner(s) to use.
See https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job.
type: string
default: '["ubuntu-latest"]'
required: false
oci-registry:
description: "OCI registry where to pull and push images."
type: string
default: "ghcr.io"
required: false
oci-registry-username:
description: |
Username used to log against the OCI registry.
See https://github.com/docker/login-action#usage.
type: string
default: ${{ github.repository_owner }}
required: false
platforms:
description: |
JSON array of platforms to build images for.
See https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images.
type: string
default: '["linux/amd64","linux/arm64"]'
required: false
images:
description: |
JSON array of images to build.
If not provided, all available images will be considered.
Example: `["php-8", "nodejs-24"]`
type: string
required: false
outputs:
built-images:
description: |
Built images data.
See https://github.com/hoverkraft-tech/ci-github-container/blob/main/.github/workflows/docker-build-images.md#outputs.
value: ${{ jobs.build-images.outputs.built-images }}
secrets:
oci-registry-password:
description: |
Password or GitHub token (packages:read and packages:write scopes) used to log against the OCI registry.
Defaults to GITHUB_TOKEN if not provided.
required: false
permissions: {}
jobs:
prepare-images-to-build:
runs-on: ${{ fromJson(inputs.runs-on) }}
permissions:
contents: read
id-token: write # Needed for getting local workflow actions
outputs:
images: ${{ steps.set-images-to-build.outputs.images }}
steps:
- id: local-workflow-actions
uses: hoverkraft-tech/ci-github-common/actions/local-workflow-actions@4b53189212d5810f710bed89711002626977215b # 0.33.0
with:
actions-path: actions
- id: get-available-images
if: ${{ !inputs.images || inputs.images == '[]' }}
uses: ./self-workflow/actions/get-available-images
- uses: hoverkraft-tech/ci-github-common/actions/local-workflow-actions@4b53189212d5810f710bed89711002626977215b # 0.33.0
with:
actions-path: actions
repository: ${{ steps.local-workflow-actions.outputs.repository }}
ref: ${{ steps.local-workflow-actions.outputs.ref }}
- id: should-build-images
if: ${{ !inputs.images || inputs.images == '[]' }}
uses: ./self-workflow/actions/should-build-images
with:
images: ${{ steps.get-available-images.outputs.images }}
- id: set-images-to-build
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
SHOULD_BUILD_IMAGES: ${{ steps.should-build-images.outputs.should-build-images }}
IMAGES: ${{ inputs.images }}
PLATFORMS: ${{ inputs.platforms }}
with:
script: |
let images = [];
if (process.env.IMAGES) {
images = JSON.parse(process.env.IMAGES);
if(!Array.isArray(images)) {
throw new Error("Input 'images' is not a valid JSON array.");
}
} else if (process.env.SHOULD_BUILD_IMAGES){
images = Object.entries(JSON.parse(process.env.SHOULD_BUILD_IMAGES))
.filter(([image, shouldBuild]) => shouldBuild === true)
.map(([image, shouldBuild]) => {
return { name: image };
});
}
if (images.length === 0) {
core.info("No images to build.");
return;
}
const platforms = JSON.parse(process.env.PLATFORMS);
const imagesToBuild = [];
for (const image of images) {
if (typeof image !== 'object' || !image.name) {
throw new Error("Image entry is not valid. It should be either a string or an object with a 'name' property.");
}
imagesToBuild.push({
context: `images/${image.name}`,
dockerfile: 'Dockerfile',
platforms,
...image,
});
}
core.setOutput('images', JSON.stringify(imagesToBuild));
# jscpd:ignore-start
- uses: hoverkraft-tech/ci-github-common/actions/local-workflow-actions@4b53189212d5810f710bed89711002626977215b # 0.33.0
if: always() && steps.local-workflow-actions.outputs.repository
with:
actions-path: actions
repository: ${{ steps.local-workflow-actions.outputs.repository }}
ref: ${{ steps.local-workflow-actions.outputs.ref }}
# jscpd:ignore-end
build-images:
needs: prepare-images-to-build
if: needs.prepare-images-to-build.outputs.images
uses: hoverkraft-tech/ci-github-container/.github/workflows/docker-build-images.yml@bcbbcaff24e053e38ebab02dd0e41442df196719 # 0.32.0
permissions:
contents: read
issues: read
packages: write
pull-requests: read
id-token: write
with:
oci-registry-username: ${{ inputs.oci-registry-username }}
oci-registry: ${{ inputs.oci-registry }}
images: ${{ needs.prepare-images-to-build.outputs.images }}
secrets:
oci-registry-password: ${{ secrets.oci-registry-password || secrets.GITHUB_TOKEN || github.token }}
summary:
name: Built Images
if: needs.build-images.outputs.built-images
runs-on: ${{ fromJson(inputs.runs-on) }}
needs: build-images
permissions:
contents: read
pull-requests: write
steps:
- id: generate-summary
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
BUILT_IMAGES: ${{ needs.build-images.outputs.built-images }}
with:
script: |
const builtImages = JSON.parse(process.env.BUILT_IMAGES || '{}');
const imageNames = Object.keys(builtImages);
if (imageNames.length === 0) {
core.setOutput('summary', '');
return;
}
const headers = ['Image', 'Tags', 'Platforms', 'Digest'];
const tableRows = [];
for (const imageName of imageNames) {
const imageData = builtImages[imageName];
const tags = imageData.tags ? imageData.tags.join(', ') : '-';
const platforms = imageData.platforms ? imageData.platforms.join(', ') : '-';
const digest = imageData.digest ? `${imageData.digest.substring(0, 20)}...` : '-';
const fullImage = (imageData.registry && imageData.repository)
? `${imageData.registry}/${imageData.repository}`
: imageName;
tableRows.push([fullImage, tags, platforms, digest]);
}
await core.summary.clear();
core.summary
.addHeading('Built Images Summary', 2)
.addTable([
headers.map((header) => ({ data: header, header: true })),
...tableRows.map((row) => row.map((cell) => ({ data: cell }))),
])
.addHeading('Image Details', 3);
for (const imageName of imageNames) {
const imageData = builtImages[imageName];
let detailMarkup = '';
if (imageData.images && imageData.images.length > 0) {
const primaryImage = imageData.images[0];
const pullCommand = `docker pull ${primaryImage}`;
detailMarkup += '<p><strong>Pull command:</strong></p>\n';
detailMarkup += '<pre><code>\n';
detailMarkup += `${pullCommand}\n`;
detailMarkup += '</code></pre>\n';
detailMarkup += '<p><strong>Full image references:</strong></p>\n<ul>\n';
for (const image of imageData.images) {
detailMarkup += `<li><code>${image}</code></li>\n`;
}
detailMarkup += '</ul>\n';
}
if (imageData.annotations) {
const annotationsMarkup = Object.entries(imageData.annotations)
.map(([key, value]) => `<li><code>${key}</code>: ${value}</li>`)
.join('\n');
detailMarkup += '<p><strong>Annotations:</strong></p>\n';
detailMarkup += `<ul>\n${annotationsMarkup}\n</ul>`;
}
const detailsBody = detailMarkup || '<em>No additional details.</em>';
core.summary.addDetails(imageName, detailsBody);
}
const summaryContent = await core.summary.stringify();
await core.summary.write();
core.setOutput('summary', summaryContent);
- uses: hoverkraft-tech/ci-github-common/actions/create-or-update-comment@4b53189212d5810f710bed89711002626977215b # 0.33.0
if: github.event.pull_request.number && steps.generate-summary.outputs.summary
with:
title: "## Built Images Summary"
body: ${{ steps.generate-summary.outputs.summary }}