Skip to content

Commit 8a3efc3

Browse files
committed
ci: add github action to publish a docker image for each new release
1 parent 8cd1ba4 commit 8a3efc3

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
name: Docker Publish
2+
3+
on:
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
8+
concurrency:
9+
group: docker-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
permissions:
13+
contents: read
14+
packages: write
15+
16+
jobs:
17+
prepare:
18+
runs-on: ubuntu-24.04
19+
20+
outputs:
21+
image: ${{ steps.image.outputs.name }}
22+
labels: ${{ steps.meta.outputs.labels }}
23+
json: ${{ steps.meta.outputs.json }}
24+
25+
steps:
26+
- name: Define image name
27+
id: image
28+
run: echo "name=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
29+
30+
- name: Extract Docker metadata
31+
id: meta
32+
uses: docker/metadata-action@v6
33+
with:
34+
images: ${{ steps.image.outputs.name }}
35+
tags: |
36+
type=semver,pattern={{version}}
37+
type=semver,pattern={{major}}.{{minor}}
38+
type=semver,pattern={{major}}
39+
type=raw,value=latest,enable=${{ github.event_name == 'release' }}
40+
type=sha,prefix=sha-,enable=${{ github.event_name == 'workflow_dispatch' }}
41+
42+
build:
43+
needs: prepare
44+
runs-on: ${{ matrix.runner }}
45+
46+
strategy:
47+
fail-fast: false
48+
matrix:
49+
include:
50+
- platform: linux/amd64
51+
platform-id: linux-amd64
52+
runner: ubuntu-24.04
53+
- platform: linux/arm64
54+
platform-id: linux-arm64
55+
runner: ubuntu-24.04-arm
56+
57+
steps:
58+
- name: Checkout repository
59+
uses: actions/checkout@v6
60+
61+
- name: Set up Docker Buildx
62+
uses: docker/setup-buildx-action@v4
63+
64+
- name: Log in to GitHub Container Registry
65+
uses: docker/login-action@v4
66+
with:
67+
registry: ghcr.io
68+
username: ${{ github.actor }}
69+
password: ${{ secrets.GITHUB_TOKEN }}
70+
71+
- name: Build and push Docker image by digest
72+
id: build
73+
uses: docker/build-push-action@v7
74+
with:
75+
context: .
76+
platforms: ${{ matrix.platform }}
77+
push: true
78+
labels: ${{ needs.prepare.outputs.labels }}
79+
outputs: type=image,name=${{ needs.prepare.outputs.image }},name-canonical=true,push-by-digest=true,push=true
80+
cache-from: type=gha,scope=${{ matrix.platform-id }}
81+
cache-to: type=gha,mode=max,scope=${{ matrix.platform-id }}
82+
83+
- name: Export image digest
84+
run: |
85+
mkdir -p /tmp/digests
86+
digest="${{ steps.build.outputs.digest }}"
87+
touch "/tmp/digests/${digest#sha256:}"
88+
89+
- name: Upload image digest
90+
uses: actions/upload-artifact@v4
91+
with:
92+
name: digest-${{ matrix.platform-id }}
93+
path: /tmp/digests/*
94+
if-no-files-found: error
95+
retention-days: 1
96+
97+
merge:
98+
needs:
99+
- prepare
100+
- build
101+
runs-on: ubuntu-24.04
102+
103+
steps:
104+
- name: Download image digests
105+
uses: actions/download-artifact@v4
106+
with:
107+
path: /tmp/digests
108+
pattern: digest-*
109+
merge-multiple: true
110+
111+
- name: Set up Docker Buildx
112+
uses: docker/setup-buildx-action@v4
113+
114+
- name: Log in to GitHub Container Registry
115+
uses: docker/login-action@v4
116+
with:
117+
registry: ghcr.io
118+
username: ${{ github.actor }}
119+
password: ${{ secrets.GITHUB_TOKEN }}
120+
121+
- name: Create multi-platform manifest
122+
env:
123+
DOCKER_METADATA_OUTPUT_JSON: ${{ needs.prepare.outputs.json }}
124+
IMAGE_NAME: ${{ needs.prepare.outputs.image }}
125+
run: |
126+
cd /tmp/digests
127+
# Build the list of digest references
128+
digest_refs=""
129+
for digest in *; do
130+
digest_refs="$digest_refs $IMAGE_NAME@sha256:$digest"
131+
done
132+
# Create the manifest
133+
docker buildx imagetools create \
134+
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
135+
$digest_refs
136+
137+
- name: Inspect published image
138+
env:
139+
DOCKER_METADATA_OUTPUT_JSON: ${{ needs.prepare.outputs.json }}
140+
run: |
141+
docker buildx imagetools inspect "$(jq -r '.tags[0]' <<< "$DOCKER_METADATA_OUTPUT_JSON")"

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ See: [`.github/workflows/`](./.github/workflows/)
9999
- [`dotnet.yml`](./.github/workflows/dotnet.yml): Main .NET build and test pipeline.
100100
- [`codeql.yml`](./.github/workflows/codeql.yml): Security scanning.
101101
- [`localization.yml`](./.github/workflows/localization.yml): Localization checks.
102+
- [`docker-publish.yml`](./.github/workflows/docker-publish.yml): Builds and publishes multi-arch container images to GHCR on every published release.
102103

103104
### Usage Guidelines for Agents
104105
1. **Context awareness**: Before generating code, always check [`.github/instructions/`](./.github/instructions/) for relevant guidelines. For example, if editing a Blazor component, consult [`blazor.instructions.md`](./.github/instructions/blazor.instructions.md).

0 commit comments

Comments
 (0)