-
Notifications
You must be signed in to change notification settings - Fork 0
194 lines (182 loc) · 7.37 KB
/
docker-publish.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
name: Build & Publish Docker Image
on:
push:
branches: [ "main" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
paths-ignore:
- '**.md'
workflow_dispatch:
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
jobs:
build:
strategy:
matrix:
include:
- arch: "linux/arm64"
dotnet_rid: "linux-arm64"
runner: "ubuntu-24.04-arm"
- arch: "linux/amd64"
dotnet_rid: "linux-amd64"
runner: "ubuntu-24.04"
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref_name }}-${{ matrix.arch }}
cancel-in-progress: true
runs-on: ${{ matrix.runner }}
permissions:
contents: read
packages: write
steps:
- name: Building for ${{ matrix.arch }} on ${{ matrix.runner }}
run: |
echo "Building for $ARCH on $RUNNER"
platform=${{ matrix.arch }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
env:
ARCH: ${{ matrix.arch }}
RUNNER: ${{ matrix.runner }}
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set REPO env variable
run: |
docker_repo="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
echo "DOCKER_REPO=${docker_repo@L}" >> "${GITHUB_ENV}"
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/[email protected]
with:
images: ${{ env.DOCKER_REPO }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/[email protected]
with:
context: .
file: ./src/standalone/Dockerfile
push: ${{ github.event_name != 'pull_request' }}
labels: ${{ steps.meta.outputs.labels }}
platforms: ${{ matrix.arch }}
outputs: type=image,"name=${{ env.DOCKER_REPO }}",push-by-digest=true,name-canonical=true,push=true
provenance: false
sbom: false
cache-to: type=gha,scope=image-${{ matrix.dotnet_rid }},mode=max
cache-from: type=gha,scope=image-${{ matrix.dotnet_rid }}
build-args: |
DOTNET_BUILD_PLATFORM=${{ matrix.dotnet_rid }}
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build-and-push.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge:
runs-on: 'ubuntu-24.04'
if: ${{ github.event_name != 'pull_request' }}
needs:
- build
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write
steps:
# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/[email protected]
with:
cosign-release: 'v2.2.4'
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set REPO env variable
run: |
docker_repo="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
echo "DOCKER_REPO=${docker_repo@L}" >> "${GITHUB_ENV}"
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/[email protected]
with:
images: ${{ env.DOCKER_REPO }}
# latest tag always represents latest stable release.
# (if the tag contains a dash, it's a prerelease, e.g. refs/tags/v0.8.0-rc6. Those should not be considered stable)
# include git commit SHA, git branch name, git tag name
# when a semver tag, e.g. '1.2.3', add tags '1', '1.2' and '1.2.3'
tags: |
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') && !contains(github.ref, '-') }}
type=sha,format=long
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }}
type=semver,pattern={{major}}.{{minor}}
# disable automatic latest tag
flavor: |
latest=false
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
# extract tags from the metadata and append them to the command
# append the full image name and digest
tags=$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON")
sources=$(printf '${{ env.DOCKER_REPO }}@sha256:%s ' *)
docker buildx imagetools create $tags $sources
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.DOCKER_REPO }}:${{ steps.meta.outputs.version }}
# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
TAGS: |
${{ steps.meta.outputs.tags }}
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: |
# extract the digest using buildx imagetools inspect | take only the sha256:... | remove whitespace
DIGEST=$(docker buildx imagetools inspect ${{ env.DOCKER_REPO }}:${{ steps.meta.outputs.version }} | grep -Po "(?<=Digest:)\s*sha256:.*" | grep -Po "[^\s]*")
echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}