|
| 1 | +name: Docker |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '23 1 * * 6' |
| 6 | + push: |
| 7 | + branches: [ "main" ] |
| 8 | + # Publish semver tags as releases. |
| 9 | + tags: [ 'v*.*.*' ] |
| 10 | + pull_request: |
| 11 | + branches: [ "main" ] |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +env: |
| 15 | + REGISTRY: ghcr.io |
| 16 | + # github.repository as <account>/<repo> |
| 17 | + IMAGE_NAME: ${{ github.repository }} |
| 18 | + |
| 19 | +jobs: |
| 20 | + build: |
| 21 | + |
| 22 | + runs-on: ubuntu-24.04 |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + packages: write |
| 26 | + # This is used to complete the identity challenge |
| 27 | + # with sigstore/fulcio when running outside of PRs. |
| 28 | + id-token: write |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout repository |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + # Install the cosign tool except on PR |
| 35 | + # https://github.com/sigstore/cosign-installer |
| 36 | + - name: Install cosign |
| 37 | + if: github.event_name != 'pull_request' |
| 38 | + uses: sigstore/cosign-installer@v3.8.2 |
| 39 | + with: |
| 40 | + cosign-release: 'v2.4.3' |
| 41 | + |
| 42 | + - name: Set up QEMU |
| 43 | + uses: docker/setup-qemu-action@v3 |
| 44 | + |
| 45 | + # Set up BuildKit Docker container builder to be able to build |
| 46 | + # multi-platform images and export cache |
| 47 | + # https://github.com/docker/setup-buildx-action |
| 48 | + - name: Set up Docker Buildx |
| 49 | + uses: docker/setup-buildx-action@v3 |
| 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@v3 |
| 56 | + with: |
| 57 | + registry: ${{ env.REGISTRY }} |
| 58 | + username: ${{ github.actor }} |
| 59 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + |
| 61 | + # Extract metadata (tags, labels) for Docker |
| 62 | + # https://github.com/docker/metadata-action |
| 63 | + - name: Extract Docker metadata |
| 64 | + id: meta |
| 65 | + uses: docker/metadata-action@v5 |
| 66 | + with: |
| 67 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 68 | + tags: | |
| 69 | + # set latest tag for default branch |
| 70 | + type=raw,value=latest,enable={{is_default_branch}} |
| 71 | +
|
| 72 | + # Build and push Docker image with Buildx (don't push on PR) |
| 73 | + # https://github.com/docker/build-push-action |
| 74 | + - name: Build and push Docker image |
| 75 | + id: build-and-push |
| 76 | + uses: docker/build-push-action@v6 |
| 77 | + with: |
| 78 | + context: . |
| 79 | + push: ${{ github.event_name != 'pull_request' }} |
| 80 | + tags: ${{ steps.meta.outputs.tags }} |
| 81 | + labels: ${{ steps.meta.outputs.labels }} |
| 82 | + cache-from: type=gha |
| 83 | + cache-to: type=gha,mode=max |
| 84 | + platforms: linux/amd64,linux/arm64 |
| 85 | + |
| 86 | + # Sign the resulting Docker image digest except on PRs. |
| 87 | + # https://github.com/sigstore/cosign |
| 88 | + - name: Sign the published Docker image |
| 89 | + if: ${{ github.event_name != 'pull_request' }} |
| 90 | + env: |
| 91 | + # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable |
| 92 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 93 | + DIGEST: ${{ steps.build-and-push.outputs.digest }} |
| 94 | + # This step uses the identity token to provision an ephemeral certificate |
| 95 | + # against the sigstore community Fulcio instance. |
| 96 | + run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} |
| 97 | + |
| 98 | + # Delete old container package versions |
| 99 | + # https://github.com/actions/delete-package-versions |
| 100 | + - uses: actions/delete-package-versions@v5 |
| 101 | + if: ${{ github.event_name != 'pull_request' }} |
| 102 | + with: |
| 103 | + package-name: ${{ github.event.repository.name }} |
| 104 | + package-type: container |
| 105 | + min-versions-to-keep: 5 |
| 106 | + delete-only-untagged-versions: true |
0 commit comments