|
| 1 | +name: Build and Push Docker Images |
| 2 | + |
| 3 | +# This workflow uses actions that are not certified by GitHub. |
| 4 | +# They are provided by a third-party and are governed by |
| 5 | +# separate terms of service, privacy policy, and support |
| 6 | +# documentation. |
| 7 | + |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: |
| 11 | + - "main" |
| 12 | + - "github-actions" |
| 13 | + # Publish semver tags as releases. |
| 14 | + tags: |
| 15 | + - 'v*.*.*' |
| 16 | + workflow_dispatch: |
| 17 | + pull_request: |
| 18 | + branches: |
| 19 | + - "main" |
| 20 | + |
| 21 | +jobs: |
| 22 | + build: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + permissions: |
| 25 | + contents: read |
| 26 | + packages: write |
| 27 | + # This is used to complete the identity challenge |
| 28 | + # with sigstore/fulcio when running outside of PRs. |
| 29 | + id-token: write |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Checkout repository |
| 33 | + uses: actions/checkout@v3 |
| 34 | + |
| 35 | + # Install the cosign tool except on PR |
| 36 | + # https://github.com/sigstore/cosign-installer |
| 37 | + - name: Install cosign |
| 38 | + if: github.event_name != 'pull_request' |
| 39 | + uses: sigstore/cosign-installer@f3c664df7af409cb4873aa5068053ba9d61a57b6 #v2.6.0 |
| 40 | + with: |
| 41 | + cosign-release: 'v1.13.1' |
| 42 | + |
| 43 | + # Setup QEMU for multi-arch build |
| 44 | + - name: Set up QEMU |
| 45 | + uses: docker/setup-qemu-action@v2 |
| 46 | + |
| 47 | + # Workaround: https://github.com/docker/build-push-action/issues/461 |
| 48 | + - name: Setup Docker buildx |
| 49 | + uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf |
| 50 | + |
| 51 | + # Login against a Docker registry except on PR |
| 52 | + # https://github.com/docker/login-action |
| 53 | + - name: Log into registry ${{ env.REGISTRY }} |
| 54 | + if: github.event_name != 'pull_request' |
| 55 | + uses: docker/login-action@v2 |
| 56 | + with: |
| 57 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 58 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 59 | + |
| 60 | + - name: Login to GHCR |
| 61 | + if: github.event_name != 'pull_request' |
| 62 | + uses: docker/login-action@v2 |
| 63 | + with: |
| 64 | + registry: ghcr.io |
| 65 | + username: ${{ github.repository_owner }} |
| 66 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 67 | + |
| 68 | + # Extract metadata (tags, labels) for Docker |
| 69 | + # https://github.com/docker/metadata-action |
| 70 | + - name: Extract Docker metadata |
| 71 | + id: meta |
| 72 | + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 |
| 73 | + with: |
| 74 | + # github.repository as <account>/<repo> |
| 75 | + images: | |
| 76 | + ${{ github.repository }} |
| 77 | + ghcr.io/${{ github.repository }} |
| 78 | + tags: | |
| 79 | + type=raw,value=latest,enable=${{ endsWith(github.ref, github.event.repository.default_branch) }} |
| 80 | + type=ref,event=branch |
| 81 | + type=ref,event=pr |
| 82 | + type=semver,pattern={{version}} |
| 83 | + type=semver,pattern={{major}} |
| 84 | + type=semver,pattern={{major}}.{{minor}} |
| 85 | + |
| 86 | + # Build and push Docker image with Buildx (don't push on PR) |
| 87 | + # https://github.com/docker/build-push-action |
| 88 | + - name: Build and push Docker image |
| 89 | + id: build-and-push |
| 90 | + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a |
| 91 | + with: |
| 92 | + context: . |
| 93 | + platforms: linux/amd64,linux/arm64 |
| 94 | + push: ${{ github.event_name != 'pull_request' }} |
| 95 | + tags: ${{ steps.meta.outputs.tags }} |
| 96 | + labels: ${{ steps.meta.outputs.labels }} |
| 97 | + cache-from: type=gha |
| 98 | + cache-to: type=gha,mode=max |
| 99 | + |
| 100 | + # Sign the resulting Docker image digest except on PRs. |
| 101 | + # This will only write to the public Rekor transparency log when the Docker |
| 102 | + # repository is public to avoid leaking data. If you would like to publish |
| 103 | + # transparency data even for private images, pass --force to cosign below. |
| 104 | + # https://github.com/sigstore/cosign |
| 105 | + - name: Sign the published Docker image |
| 106 | + if: ${{ github.event_name != 'pull_request' }} |
| 107 | + env: |
| 108 | + COSIGN_EXPERIMENTAL: "true" |
| 109 | + # This step uses the identity token to provision an ephemeral certificate |
| 110 | + # against the sigstore community Fulcio instance. |
| 111 | + run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign {}@${{ steps.build-and-push.outputs.digest }} |
0 commit comments