Repository navigation
220 lines (201 loc) · 8.07 KB
/
Copy pathbuild-container.yml
File metadata and controls
220 lines (201 loc) · 8.07 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
name: Build container
on:
workflow_call:
inputs:
context:
description: "Path to use for build context"
required: true
type: string
file:
description: "Path to Dockerfile"
required: true
type: string
name:
description: "Container name"
required: true
type: string
runs-on-amd64:
description: "Runner label to use for amd64 jobs"
required: true
type: string
runs-on-arm64:
description: "Runner label to use for arm64 jobs"
required: true
type: string
runs-on-manifest:
description: "Runner label for the job that combines the amd64 and arm64 builds into one multi-arch manifest"
required: true
type: string
existing-image:
description: "Digest reference (ghcr.io/...@sha256:...) of a trusted container already built from this exact context. When set, nothing is built and this container is used."
required: false
type: string
default: ""
runs-on-existing:
description: "Runner label for the one-step job that runs instead of the build when existing-image is set"
required: true
type: string
context-tag:
description: "Tag naming this build context (ctx-...). Trusted (push) builds add it to the container so later runs can find it as existing-image."
required: false
type: string
default: ""
outputs:
path:
description: "Digest reference to the container this run uses: existing-image, or the one it just built"
value: ${{ inputs.existing-image || jobs.create-manifest.outputs.path }}
jobs:
use-existing:
# Runs instead of the build jobs. A called workflow must run at least one
# job: if all of them are skipped, the caller counts as skipped, and so
# does every job that needs it (lint, depends, src, test).
name: Use existing container
if: ${{ inputs.existing-image != '' }}
runs-on: ${{ inputs.runs-on-existing }}
steps:
- name: Report existing container
env:
NAME: ${{ inputs.name }}
EXISTING_IMAGE: ${{ inputs.existing-image }}
run: |
echo "::notice title=${NAME}::Using existing container ${EXISTING_IMAGE}, build skipped"
build-amd64:
name: Build container (amd64)
if: ${{ inputs.existing-image == '' }}
runs-on: ${{ inputs.runs-on-amd64 }}
outputs:
tag: ${{ steps.prepare.outputs.tag }}
repo: ${{ steps.prepare.outputs.repo }}
digest: ${{ steps.build.outputs.digest }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
allow-unsafe-pr-checkout: true
persist-credentials: false
- name: Prepare variables
id: prepare
run: |
BRANCH_NAME=$(echo "${GITHUB_REF##*/}" | tr '[:upper:]' '[:lower:]')
REPO_NAME=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
echo "tag=${BRANCH_NAME}" >> "$GITHUB_OUTPUT"
echo "repo=${REPO_NAME}" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v7
with:
context: ${{ inputs.context }}
file: ${{ inputs.file }}
push: true
platforms: linux/amd64
# Only trusted events may write tags that other runs read
tags: |
ghcr.io/${{ steps.prepare.outputs.repo }}/${{ inputs.name }}:${{ github.event_name == 'push' && format('trusted-{0}', hashFiles(inputs.file)) || 'untrusted' }}-amd64
cache-from: |
type=registry,ref=ghcr.io/${{ steps.prepare.outputs.repo }}/${{ inputs.name }}:trusted-${{ hashFiles(inputs.file) }}-amd64
cache-to: type=inline
build-arm64:
name: Build container (arm64)
if: ${{ inputs.existing-image == '' }}
runs-on: ${{ inputs.runs-on-arm64 }}
outputs:
digest: ${{ steps.build.outputs.digest }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
allow-unsafe-pr-checkout: true
persist-credentials: false
- name: Prepare variables
id: prepare
run: |
BRANCH_NAME=$(echo "${GITHUB_REF##*/}" | tr '[:upper:]' '[:lower:]')
REPO_NAME=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
echo "tag=${BRANCH_NAME}" >> "$GITHUB_OUTPUT"
echo "repo=${REPO_NAME}" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v7
with:
context: ${{ inputs.context }}
file: ${{ inputs.file }}
push: true
platforms: linux/arm64
# Only trusted events may write tags that other runs read
tags: |
ghcr.io/${{ steps.prepare.outputs.repo }}/${{ inputs.name }}:${{ github.event_name == 'push' && format('trusted-{0}', hashFiles(inputs.file)) || 'untrusted' }}-arm64
cache-from: |
type=registry,ref=ghcr.io/${{ steps.prepare.outputs.repo }}/${{ inputs.name }}:trusted-${{ hashFiles(inputs.file) }}-arm64
cache-to: type=inline
create-manifest:
name: Create multi-arch manifest
if: ${{ inputs.existing-image == '' }}
runs-on: ${{ inputs.runs-on-manifest }}
needs: [build-amd64, build-arm64]
outputs:
path: ${{ steps.manifest.outputs.path }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
allow-unsafe-pr-checkout: true
persist-credentials: false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
with:
# imagetools create --metadata-file needs buildx v0.32.0 or later,
# newer than some runner images ship.
version: v0.38.2
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create and push multi-arch manifest
id: manifest
env:
TRUSTED: ${{ github.event_name == 'push' }}
CONTEXT_TAG: ${{ inputs.context-tag }}
REPO_NAME: ${{ needs.build-amd64.outputs.repo }}
IMAGE_NAME: ${{ inputs.name }}
TAG: ${{ needs.build-amd64.outputs.tag }}
HASH_TAG: ${{ hashFiles(inputs.file) }}
AMD64_DIGEST: ${{ needs.build-amd64.outputs.digest }}
ARM64_DIGEST: ${{ needs.build-arm64.outputs.digest }}
run: |
REPO="ghcr.io/${REPO_NAME}/${IMAGE_NAME}"
if [[ "$TRUSTED" == "true" ]]; then
TAGS=(-t "${REPO}:${HASH_TAG}" -t "${REPO}:${TAG}" -t "${REPO}:latest")
# Lets later runs with the same context use this container (resolve_ci_images.py)
if [[ -n "$CONTEXT_TAG" ]]; then
TAGS+=(-t "${REPO}:${CONTEXT_TAG}")
fi
else
TAGS=(-t "${REPO}:untrusted")
fi
# Use this run's digests: tags can be moved by concurrent runs
docker buildx imagetools create --metadata-file "${RUNNER_TEMP}/manifest.json" "${TAGS[@]}" \
"${REPO}@${AMD64_DIGEST}" \
"${REPO}@${ARM64_DIGEST}"
DIGEST=$(jq -er '."containerimage.descriptor".digest' "${RUNNER_TEMP}/manifest.json")
echo "path=${REPO}@${DIGEST}" >> "$GITHUB_OUTPUT"