Skip to content

Commit e37138d

Browse files
authored
Add Docker package workflow (#347)
# Description Add Docker package workflow. ## Changes Build and push Docker image to GHCR with tags: - `latest` for master branch. - `release-*` or `v*` for Git tags, depends on the Git tag format. - `sha-<sha>` for Git commit hash, only with pull requests, master branch and Git tags. For pull request, only ensures that the docker build succeeds, does not push the image. See: docker/build-push-action#751
1 parent c2d849e commit e37138d

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-docker-images
2+
3+
name: Docker Package
4+
5+
on:
6+
workflow_dispatch:
7+
push:
8+
branches:
9+
- master
10+
tags:
11+
- 'v*'
12+
- 'release-*'
13+
pull_request:
14+
branches:
15+
- master
16+
17+
env:
18+
# Use docker.io for Docker Hub if empty
19+
REGISTRY: ghcr.io
20+
# github.repository as <account>/<repo>
21+
IMAGE_NAME: ${{ github.repository }}
22+
23+
jobs:
24+
package:
25+
runs-on: ubuntu-latest
26+
27+
# Sets the permissions granted to the GITHUB_TOKEN for the actions in this job.
28+
permissions:
29+
contents: read
30+
packages: write
31+
attestations: write
32+
id-token: write
33+
34+
steps:
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
37+
38+
# https://github.com/docker/login-action
39+
- name: Log in to the Container registry ${{ env.REGISTRY }}
40+
if: github.event_name != 'pull_request'
41+
uses: docker/login-action@v3
42+
with:
43+
registry: ${{ env.REGISTRY }}
44+
username: ${{ github.actor }}
45+
# https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication
46+
password: ${{ secrets.GITHUB_TOKEN }}
47+
48+
# https://github.com/docker/metadata-action
49+
- name: Extract metadata (tags, labels) for Docker
50+
id: meta
51+
uses: docker/metadata-action@v5
52+
with:
53+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
54+
# generate Docker tags based on the following events/attributes
55+
tags: |
56+
# set latest tag for master branch
57+
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
58+
type=ref,event=tag
59+
type=ref,event=pr
60+
type=sha
61+
62+
# https://github.com/docker/build-push-action
63+
# For pull request, only ensures that the docker build succeeds, does not push the image.
64+
# See: https://github.com/docker/build-push-action/issues/751
65+
- name: Build and push Docker image
66+
id: push
67+
uses: docker/build-push-action@v6
68+
with:
69+
context: .
70+
push: ${{ github.event_name != 'pull_request' }}
71+
file: ./provisioning/Dockerfile
72+
tags: ${{ steps.meta.outputs.tags }}
73+
labels: ${{ steps.meta.outputs.labels }}
74+
75+
# https://github.com/actions/attest-build-provenance
76+
- name: Generate artifact attestation
77+
if: github.event_name != 'pull_request'
78+
uses: actions/attest-build-provenance@v2
79+
with:
80+
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
81+
subject-digest: ${{ steps.push.outputs.digest }}
82+
# https://github.com/actions/attest-build-provenance/issues/71#issuecomment-2108140285
83+
push-to-registry: false

0 commit comments

Comments
 (0)