1+ #
2+ name : Create and publish a Docker image
3+
4+ # Configures this workflow to run every time a change is pushed to the branch called
5+ # `main` or `dev`.
6+ on :
7+ push :
8+ branches : ['main', 'dev']
9+ tags : ['*']
10+
11+ # Defines two custom environment variables for the workflow. These are used for the
12+ # Container registry domain, and a name for the Docker image that this workflow builds.
13+ env :
14+ REGISTRY : ghcr.io
15+ IMAGE_NAME : ${{ github.repository }}
16+
17+ # There is a single job in this workflow. It's configured to run on the latest available
18+ # version of Ubuntu.
19+ jobs :
20+ build-and-push-image :
21+ runs-on : ubuntu-latest
22+ # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
23+ permissions :
24+ contents : read
25+ packages : write
26+ attestations : write
27+ id-token : write
28+
29+ steps :
30+ - name : Checkout repository
31+ uses : actions/checkout@v6
32+ with :
33+ submodules : true
34+
35+ # Uses the `docker/login-action` action to log in to the Container registry registry
36+ # using the account and password that will publish the packages. Once published, the
37+ # packages are scoped to the account defined here.
38+ - name : Log in to the Container registry
39+ uses : docker/login-action@v3
40+ with :
41+ registry : ${{ env.REGISTRY }}
42+ username : ${{ github.actor }}
43+ password : ${{ secrets.GITHUB_TOKEN }}
44+
45+ # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about)
46+ # to extract tags and labels that will be applied to the specified image.
47+ # The `id` "meta" allows the output of this step to be referenced in a subsequent
48+ # step. The `images` value provides the base name for the tags and labels.
49+ # It will automatically create the latest Docker tag, if a git tag is found: https://github.com/docker/metadata-action?tab=readme-ov-file#latest-tag
50+ - name : Extract metadata (tags, labels) for Docker
51+ id : meta
52+ uses : docker/metadata-action@v5
53+ with :
54+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
55+
56+ # This step gets the short git commit hash
57+ # https://dev.to/hectorleiva/github-actions-and-creating-a-short-sha-hash-8b7
58+ - name : Set short git commit SHA
59+ id : vars
60+ run : |
61+ calculatedSha=$(git rev-parse --short ${{ github.sha }})
62+ echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV
63+
64+ # This step uses the `docker/build-push-action` action to build the image, based on
65+ # your repository's `Dockerfile`. If the build succeeds, it pushes the image to
66+ # GitHub Packages.
67+ # It uses the `context` parameter to define the build's context as the set of files
68+ # located in the specified path. For more information, see [Usage](https://github.com/docker/build-push-action#usage)
69+ # in the README of the `docker/build-push-action` repository.
70+ # It uses the `tags` and `labels` parameters to tag and label the image with the
71+ # output from the "meta" step.
72+ - name : Build and push Docker image
73+ id : push
74+ uses : docker/build-push-action@v6
75+ with :
76+ context : .
77+ push : true
78+ tags : ${{ steps.meta.outputs.tags }}
79+ labels : ${{ steps.meta.outputs.labels }}
80+ build-args : |
81+ GIT_HASH=${{ github.sha }}
82+ GIT_HASH_SHORT=${{ env.COMMIT_SHORT_SHA }}
83+
84+ # This step generates an artifact attestation for the image, which is an unforgeable
85+ # statement about where and how it was built. It increases supply chain security for
86+ # people who consume the image. For more information, see [Using artifact attestations
87+ # to establish provenance for builds](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds).
88+ - name : Generate artifact attestation
89+ uses : actions/attest-build-provenance@v3
90+ with :
91+ subject-name : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
92+ subject-digest : ${{ steps.push.outputs.digest }}
93+ push-to-registry : true
0 commit comments