|
| 1 | +--- |
| 2 | +name: Build and push container image to image registries |
| 3 | +on: # yamllint disable-line rule:truthy |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*.*.*" |
| 7 | + release: |
| 8 | + types: |
| 9 | + - published |
| 10 | + - released |
| 11 | + workflow_dispatch: # Allow manual triggering of the workflow |
| 12 | + inputs: |
| 13 | + push_to_docker_hub: |
| 14 | + description: 'Push image to Docker Hub' |
| 15 | + required: false |
| 16 | + default: false |
| 17 | + type: boolean |
| 18 | + |
| 19 | +jobs: |
| 20 | + |
| 21 | + push_to_registries: |
| 22 | + name: Push image to container registries |
| 23 | + runs-on: ubuntu-latest |
| 24 | + env: |
| 25 | + # The registries to push the image to |
| 26 | + DOCKER_REGISTRY: docker.io |
| 27 | + DOCKER_USERNAME: "${{ secrets.DOCKER_USERNAME }}" |
| 28 | + GITHUB_REGISTRY: ghcr.io |
| 29 | + # The name of the image to push |
| 30 | + IMAGE_NAME: external-mdns |
| 31 | + |
| 32 | + permissions: |
| 33 | + # Permit generating an artifact attestation for the image |
| 34 | + attestations: write |
| 35 | + # Permits an action to list the commits |
| 36 | + contents: read |
| 37 | + # Fetch an OpenID Connect (OIDC) token |
| 38 | + id-token: write |
| 39 | + # Permit uploading and publishing packages on GitHub Packages. |
| 40 | + packages: write |
| 41 | + |
| 42 | + steps: |
| 43 | + - name: Check out the repo |
| 44 | + uses: actions/checkout@v4 |
| 45 | + |
| 46 | + - name: Set up QEMU |
| 47 | + uses: docker/setup-qemu-action@v3 |
| 48 | + with: |
| 49 | + platforms: arm64,arm |
| 50 | + |
| 51 | + - name: Set up Docker Buildx |
| 52 | + uses: docker/setup-buildx-action@v3 |
| 53 | + |
| 54 | + - name: Extract metadata (tags, labels) for Docker |
| 55 | + id: meta |
| 56 | + uses: docker/metadata-action@906ecf0fc0a80f9110f79d9e6c04b1080f4a2621 |
| 57 | + env: |
| 58 | + # Attach metadata annotations to both the image index and the manifest |
| 59 | + DOCKER_METADATA_ANNOTATIONS_LEVELS: index,manifest |
| 60 | + with: |
| 61 | + # yamllint disable rule:line-length |
| 62 | + images: | |
| 63 | + name=${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }},enable=${{ github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && inputs.push_to_docker_hub == true) }} |
| 64 | + name=${{ env.GITHUB_REGISTRY }}/${{ github.repository }} |
| 65 | + # Disable generating the 'latest' tag on the image. |
| 66 | + # It will be conditionally tagged later in the workflow. |
| 67 | + flavor: | |
| 68 | + latest=false |
| 69 | + annotations: | |
| 70 | + # The date the image was created |
| 71 | + org.opencontainers.image.created={{date 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'}} |
| 72 | + org.opencontainers.image.revision={{sha}} |
| 73 | + org.opencontainers.image.source=${{ github.event.repository.html_url }} |
| 74 | + org.opencontainers.image.url=${{ github.event.repository.html_url }} |
| 75 | + org.opencontainers.image.version={{tag}} |
| 76 | + tags: | |
| 77 | + # '{{version}}' results in a tag of '<major>.<minor>.<patch>' |
| 78 | + type=semver,pattern={{version}} |
| 79 | + type=semver,pattern={{major}}.{{minor}} |
| 80 | +
|
| 81 | + # Results in the image being tagged with same value as the Git tag |
| 82 | + # (e.g.,'v<major>.<minor>.<patch>') |
| 83 | + type=pep440,pattern={{raw}} |
| 84 | +
|
| 85 | + # set 'latest' tag on the image if the following conditions are met: |
| 86 | + # 1. The event is a push |
| 87 | + # 2. The base branch is the default branch |
| 88 | + # 3. The GitHub reference starts with 'refs/tags/v' |
| 89 | + # (i.e., the event is a tag release) |
| 90 | + type=raw,value=latest,enable=${{ github.event_name == 'push' && github.event.base_ref == format('refs/heads/{0}', github.event.repository.default_branch) && startsWith(github.ref, 'refs/tags/v') }} |
| 91 | +
|
| 92 | + # Tag the image with the commit SHA when manually triggered and is not |
| 93 | + # a tag release. This is useful for testing and debugging. |
| 94 | + type=sha,enable=${{ github.event_name == 'workflow_dispatch' && !startsWith(github.ref, 'ref/tags/v') }} |
| 95 | + # yamllint enable rule:line-length |
| 96 | + |
| 97 | + - name: Login to Docker Hub |
| 98 | + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 |
| 99 | + if: github.event_name != 'workflow_dispatch' || inputs.push_to_docker_hub == true # yamllint disable-line rule:line-length |
| 100 | + with: |
| 101 | + registry: ${{ env.DOCKER_REGISTRY }} |
| 102 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 103 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 104 | + |
| 105 | + - name: Login to GitHub Container registry |
| 106 | + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 |
| 107 | + with: |
| 108 | + registry: ${{ env.GITHUB_REGISTRY }} |
| 109 | + username: ${{ github.actor }} |
| 110 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 111 | + |
| 112 | + - name: Build and push container image |
| 113 | + id: push |
| 114 | + uses: docker/build-push-action@v6 |
| 115 | + with: |
| 116 | + context: . |
| 117 | + file: ./Dockerfile |
| 118 | + platforms: > |
| 119 | + linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/arm/v5 |
| 120 | + push: ${{ github.event_name != 'pull_request' }} |
| 121 | + annotations: ${{ steps.meta.outputs.annotations }} |
| 122 | + # Use the same annotations as labels on the image |
| 123 | + labels: ${{ steps.meta.outputs.annotations }} |
| 124 | + tags: ${{ steps.meta.outputs.tags }} |
| 125 | + |
| 126 | + - name: Generate artifact attestation |
| 127 | + uses: actions/attest-build-provenance@v2 |
| 128 | + with: |
| 129 | + subject-name: ${{ env.GITHUB_REGISTRY }}/${{ github.repository }} |
| 130 | + subject-digest: ${{ steps.push.outputs.digest }} |
| 131 | + push-to-registry: true |
0 commit comments