Repository navigation
200 lines (180 loc) · 7.78 KB
/
Copy pathdocker.yaml
File metadata and controls
200 lines (180 loc) · 7.78 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
name: Build and Push Docker Image
on:
push:
branches:
- master
tags:
- "v*.*.*" # v1.2.3
- "v*.*.*-*" # v1.2.3-rc1, v1.2.3-alpha.1, v1.2.3-beta
- "v*.*.*-*.*" # v1.2.3-alpha.1, v1.2.3-rc.2
pull_request:
branches:
- master
# Skip PRs that cannot change the image. Later patterns override earlier
# ones, so edits to this workflow itself still trigger a build.
paths:
- "**"
- "!**/*.md"
- "!.github/**"
- ".github/workflows/docker.yaml"
workflow_dispatch:
inputs:
ref:
description: 'Git ref (branch, tag, or commit SHA) to build'
required: false
default: 'master'
type: string
push_image:
description: 'Push image to registry'
required: false
default: true
type: boolean
# A new push to the same PR supersedes the previous build. Other events get a
# unique group so every master commit and tag still builds its own image.
concurrency:
group: docker-${{ github.event_name == 'pull_request' && github.ref || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
PUSH_IMAGE: ${{ github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || github.event.inputs.push_image == 'true') }}
jobs:
# Each platform builds natively on its own runner (no QEMU) and pushes an
# untagged image by digest; the merge job then publishes the tagged
# multi-platform manifest list. PRs only compile-check linux/amd64.
build:
runs-on: ${{ matrix.platform == 'linux/arm64' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }}
strategy:
fail-fast: false
matrix:
platform: ${{ github.event_name == 'pull_request' && fromJSON('["linux/amd64"]') || fromJSON('["linux/amd64","linux/arm64"]') }}
permissions:
contents: read
packages: write
outputs:
commit_short: ${{ steps.package_info.outputs.COMMIT_SHORT }}
steps:
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v7
with:
ref: ${{ github.event.inputs.ref || github.ref }}
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Log in to Container Registry
if: env.PUSH_IMAGE == 'true'
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Get package info
id: package_info
run: |
echo "PACKAGE_VERSION=$(git describe --tags --always)" >> $GITHUB_OUTPUT
echo "PACKAGE_COMMIT_ID=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
echo "COMMIT_SHORT=$(git rev-parse --short=7 HEAD)" >> $GITHUB_OUTPUT
GIT_STATUS=$(git status --porcelain)
if [ -n "$GIT_STATUS" ]; then
echo "GIT_STATUS=dirty" >> $GITHUB_OUTPUT
else
echo "GIT_STATUS=clean" >> $GITHUB_OUTPUT
fi
- name: Build and push by digest
id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=${{ env.PUSH_IMAGE }}
build-args: |
PACKAGE_VERSION=${{ steps.package_info.outputs.PACKAGE_VERSION }}
PACKAGE_COMMIT_ID=${{ steps.package_info.outputs.PACKAGE_COMMIT_ID }}
GIT_STATUS=${{ steps.package_info.outputs.GIT_STATUS }}
cache-from: type=gha,scope=${{ env.PLATFORM_PAIR }}
cache-to: type=gha,mode=max,scope=${{ env.PLATFORM_PAIR }}
- name: Export digest
if: env.PUSH_IMAGE == 'true'
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.build.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"
- name: Upload digest
if: env.PUSH_IMAGE == 'true'
uses: actions/upload-artifact@v7
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
merge:
needs: build
if: github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || github.event.inputs.push_image == 'true')
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
attestations: write
steps:
- name: Download digests
uses: actions/download-artifact@v8
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Log in to Container Registry
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# github.ref/github.sha describe the dispatching branch, not inputs.ref,
# so ref-derived tags are only set for push events. A manual dispatch
# is tagged with the commit that was actually built and never moves
# latest, a branch tag, or a version tag.
tags: |
# set latest tag for master branch pushes
type=raw,value=latest,enable=${{ github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
# set version tag for tags
type=ref,event=tag,enable=${{ github.event_name == 'push' }}
# set branch name for branch pushes
type=ref,event=branch,enable=${{ github.event_name == 'push' }}
# set sha-<short_sha> for any push
type=sha,prefix=sha-,enable=${{ github.event_name == 'push' }}
# set manual-<short_sha> of the built commit for manual workflow dispatch
type=raw,value=manual-${{ needs.build.outputs.commit_short }},enable=${{ github.event_name == 'workflow_dispatch' }}
- name: Create manifest list and push
id: manifest
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create \
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
ref=$(jq -r '.tags[0]' <<< "$DOCKER_METADATA_OUTPUT_JSON")
docker buildx imagetools inspect "$ref"
digest=$(docker buildx imagetools inspect "$ref" --format '{{json .Manifest}}' | jq -r .digest)
echo "digest=$digest" >> $GITHUB_OUTPUT
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v4
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.manifest.outputs.digest }}
push-to-registry: true