|
| 1 | +name: Docker to GHCR |
| 2 | + |
| 3 | +# Adapted from |
| 4 | +# https://github.com/actions/starter-workflows/blob/main/ci/docker-publish.yml |
| 5 | +# (and some times ago, from .github/workflows/main-ci.yml |
| 6 | +# of https://github.com/metcalfc/docker-action-examples/blob/main/) |
| 7 | +# for the OpenDRR Platform |
| 8 | + |
| 9 | +on: |
| 10 | + push: |
| 11 | + #schedule: |
| 12 | + # - cron: '34 11 * * *' |
| 13 | + branches: [ master ] |
| 14 | + # Publish semver tags as releases. |
| 15 | + tags: [ 'v*.*.*' ] |
| 16 | + pull_request: |
| 17 | + branches: [ master ] |
| 18 | + |
| 19 | +env: |
| 20 | + # Use docker.io for Docker Hub if empty |
| 21 | + REGISTRY: ghcr.io |
| 22 | + # github.repository as <account>/<repo> |
| 23 | + IMAGE_NAME: ${{ github.repository }} |
| 24 | + |
| 25 | +jobs: |
| 26 | + build: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + permissions: |
| 29 | + contents: read |
| 30 | + packages: write |
| 31 | + # This is used to complete the identity challenge |
| 32 | + # with sigstore/fulcio when running outside of PRs. |
| 33 | + id-token: write |
| 34 | + |
| 35 | + steps: |
| 36 | + - name: Checkout |
| 37 | + uses: actions/checkout@v3 |
| 38 | + |
| 39 | + # Install the cosign tool except on PR |
| 40 | + # https://github.com/sigstore/cosign-installer |
| 41 | + - name: Install cosign |
| 42 | + if: github.event_name != 'pull_request' |
| 43 | + uses: sigstore/cosign-installer@main |
| 44 | + |
| 45 | + # Workaround: https://github.com/docker/build-push-action/issues/461 |
| 46 | + - name: Setup Docker buildx |
| 47 | + uses: docker/setup-buildx-action@v1 |
| 48 | + |
| 49 | + # Login against a Docker registry except on PR |
| 50 | + # https://github.com/docker/login-action |
| 51 | + - name: Log into registry ${{ env.REGISTRY }} |
| 52 | + if: github.event_name != 'pull_request' |
| 53 | + uses: docker/login-action@v1 |
| 54 | + with: |
| 55 | + registry: ${{ env.REGISTRY }} |
| 56 | + username: ${{ github.actor }} |
| 57 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + # Extract metadata (tags, labels) for Docker |
| 60 | + # https://github.com/docker/metadata-action |
| 61 | + - name: Extract Docker metadata |
| 62 | + id: meta |
| 63 | + uses: docker/metadata-action@v3 |
| 64 | + with: |
| 65 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 66 | + tags: | |
| 67 | + type=schedule |
| 68 | + type=semver,pattern={{version}} |
| 69 | + type=edge |
| 70 | + type=ref,event=branch |
| 71 | + type=ref,event=pr |
| 72 | +
|
| 73 | + # Build and push Docker image with Buildx (don't push on PR) |
| 74 | + # https://github.com/docker/build-push-action |
| 75 | + - name: Build and push Docker image |
| 76 | + id: build-and-push |
| 77 | + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc |
| 78 | + with: |
| 79 | + context: . |
| 80 | + push: ${{ github.event_name != 'pull_request' }} |
| 81 | + tags: ${{ steps.meta.outputs.tags }} |
| 82 | + labels: ${{ steps.meta.outputs.labels }} |
| 83 | + cache-from: type=gha |
| 84 | + cache-to: type=gha,mode=max |
| 85 | + |
| 86 | + # Sign the resulting Docker image digest except on PRs. |
| 87 | + # This will only write to the public Rekor transparency log when the Docker |
| 88 | + # repository is public to avoid leaking data. If you would like to publish |
| 89 | + # transparency data even for private images, pass --force to cosign below. |
| 90 | + # https://github.com/sigstore/cosign |
| 91 | + - name: Sign the published Docker image |
| 92 | + if: ${{ github.event_name != 'pull_request' }} |
| 93 | + env: |
| 94 | + COSIGN_EXPERIMENTAL: "true" |
| 95 | + # This step uses the identity token to provision an ephemeral certificate |
| 96 | + # against the sigstore community Fulcio instance. |
| 97 | + run: cosign sign ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }} |
0 commit comments