|
1 | | -name: Release |
2 | | -# Controls when the workflow will run |
| 1 | +name: Create and publish a Docker image |
| 2 | + |
| 3 | +# Configures this workflow to run every time a change is pushed to the branch called `release`. |
3 | 4 | on: |
4 | | - release: |
5 | | - types: |
6 | | - - published |
| 5 | + pull_request: |
| 6 | + branches: main |
| 7 | + push: |
| 8 | + branches: main |
| 9 | + tags: v.* |
| 10 | + |
| 11 | +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. |
| 12 | +env: |
| 13 | + REGISTRY: ghcr.io |
7 | 14 |
|
| 15 | +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. |
8 | 16 | jobs: |
9 | | - buildpush: |
| 17 | + build-and-push-image: |
10 | 18 | runs-on: ubuntu-latest |
| 19 | + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. |
11 | 20 | permissions: |
| 21 | + id-token: write |
12 | 22 | contents: read |
13 | 23 | packages: write |
| 24 | + attestations: write |
| 25 | + |
14 | 26 | steps: |
15 | | - - # Get the repository's code |
16 | | - name: Checkout |
17 | | - uses: actions/checkout@v2 |
18 | | - - # https://github.com/vegardit/docker-gitea-act-runner/issues/23 |
19 | | - name: Fix docker sock permissions |
20 | | - run: sudo chmod 666 /var/run/docker.sock |
21 | | - - # https://github.com/docker/setup-qemu-action |
22 | | - name: Set up QEMU |
23 | | - uses: docker/setup-qemu-action@v1 |
24 | | - - # https://github.com/docker/setup-buildx-action |
25 | | - name: Set up Docker Buildx |
26 | | - id: buildx |
27 | | - uses: docker/setup-buildx-action@v1 |
28 | | - - # https://github.com/docker/login-action |
29 | | - name: Log in to the Container registry |
30 | | - uses: docker/login-action@v2 |
| 27 | + - name: Set image name |
| 28 | + run: | |
| 29 | + echo "IMAGE_NAME=${GITHUB_REPOSITORY@L}" >> "${GITHUB_ENV}" |
| 30 | +
|
| 31 | + - name: Checkout repository |
| 32 | + uses: actions/checkout@v4 |
| 33 | + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. |
| 34 | + - name: Log in to the Container registry |
| 35 | + uses: docker/login-action@v3 |
31 | 36 | with: |
32 | | - # Maybe there is a default env var for this? |
33 | | - registry: git.maronato.dev |
34 | | - username: ${{ github.repository_owner }}} |
35 | | - # Ideally, we should only need to set "permissions: package: write", but |
36 | | - # Gitea is having issues with that. For now, this is a manually created |
37 | | - # token available user-wise, with the "package:write" permission. |
38 | | - password: ${{ secrets.PACKAGE_WRITE_TOKEN }} |
39 | | - - # https://github.com/docker/metadata-action |
40 | | - # Generate tags and labels for the image |
41 | | - # according to the commit and the branch |
42 | | - name: Docker meta |
| 37 | + registry: ${{ env.REGISTRY }} |
| 38 | + username: ${{ github.actor }} |
| 39 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 40 | + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. |
| 41 | + - name: Extract metadata (tags, labels) for Docker |
43 | 42 | id: meta |
44 | | - uses: docker/metadata-action@v4 |
| 43 | + uses: docker/metadata-action@v5 |
45 | 44 | with: |
46 | | - # The container image name needs the custom registry in it. |
47 | | - # Maybe there is a default env var for this? |
48 | | - images: git.maronato.dev/${{ github.repository }} |
| 45 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
49 | 46 | tags: | |
50 | 47 | type=ref,event=branch |
51 | 48 | type=ref,event=pr |
52 | 49 | type=semver,pattern={{version}} |
53 | 50 | type=semver,pattern={{major}}.{{minor}} |
54 | | - - # httos://github.com/actions/cache |
55 | | - name: Cache Docker layers |
56 | | - uses: actions/cache@v3 |
57 | | - with: |
58 | | - path: | |
59 | | - /go/pkg/mod/ |
60 | | - /tmp/.go-build-cache |
61 | | - /tmp/.buildx-cache |
62 | | - key: ${{ runner.os }}-buildx-${{ github.sha }} |
63 | | - restore-keys: | |
64 | | - ${{ runner.os }}-buildx- |
65 | | - - # https://github.com/docker/build-push-action |
66 | | - name: Build and push |
67 | | - uses: docker/build-push-action@v2 |
| 51 | +
|
| 52 | + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. |
| 53 | + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. |
| 54 | + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. |
| 55 | + - name: Build and push Docker image |
| 56 | + id: push |
| 57 | + uses: docker/build-push-action@v6 |
68 | 58 | with: |
69 | 59 | context: . |
70 | 60 | build-args: | |
71 | 61 | VERSION=${{ steps.meta.outputs.version }} |
72 | 62 | platforms: linux/amd64,linux/arm64 |
73 | | - push: ${{ github.event_name != 'pull_request' }} |
| 63 | + push: true |
74 | 64 | tags: ${{ steps.meta.outputs.tags }} |
75 | 65 | labels: ${{ steps.meta.outputs.labels }} |
76 | | - cache-from: type=local,src=/tmp/.buildx-cache |
77 | | - cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max |
78 | | - - # Temp fix |
79 | | - # https://github.com/docker/build-push-action/issues/252 |
80 | | - # https://github.com/moby/buildkit/issues/1896 |
81 | | - name: Move cache |
82 | | - run: | |
83 | | - rm -rf /tmp/.buildx-cache |
84 | | - mv /tmp/.buildx-cache-new /tmp/.buildx-cache |
| 66 | + |
| 67 | + # This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)." |
| 68 | + - name: Generate artifact attestation |
| 69 | + uses: actions/attest-build-provenance@v1 |
| 70 | + with: |
| 71 | + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 72 | + subject-digest: ${{ steps.push.outputs.digest }} |
| 73 | + push-to-registry: true |
0 commit comments