From d76b3dd977a838b692383a4a567c59e3054fafc5 Mon Sep 17 00:00:00 2001 From: Thomas Yu Date: Tue, 18 Jul 2023 10:40:37 -0700 Subject: [PATCH] Update docker.yml --- .github/workflows/docker.yml | 121 +++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 42 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index cfd0863..a207a0a 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,57 +1,94 @@ -# This workflow builds and pushes the docker image using docker's v2 action -# which requires explicitly setting up buildx and logging in - -name: Build and publish container to Docker Hub +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. on: +# schedule: +# - cron: '40 17 * * *' push: - tags: ['v[0-9]*', '[0-9]+.[0-9]+*'] # Match tags that resemble a version + branches: [ "main" ] + # Publish semver tags as releases. + tags: [ 'v*.*.*' ] workflow_dispatch: +env: + # Use docker.io for Docker Hub if empty + REGISTRY: ghcr.io + # github.repository as / + IMAGE_NAME: ${{ github.repository }} + + jobs: build: + runs-on: ubuntu-latest - env: - DOCKER_REPO: ${{ secrets.DOCKER_REPO }} # Example: sagebionetworks/vcf2maf + permissions: + contents: read + packages: write + # This is used to complete the identity challenge + # with sigstore/fulcio when running outside of PRs. + id-token: write + steps: - - - name: Checkout + - name: Checkout repository uses: actions/checkout@v3 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - - - name: Set up Docker Buildx + + # Install the cosign tool except on PR + # https://github.com/sigstore/cosign-installer + - name: Install cosign + if: github.event_name != 'pull_request' + uses: sigstore/cosign-installer@v3 + with: + cosign-release: 'v2.1.1' + + # Workaround: https://github.com/docker/build-push-action/issues/461 + - name: Setup Docker buildx uses: docker/setup-buildx-action@v2 - - - name: Login to DockerHub - uses: docker/login-action@v2 + + # Login against a Docker registry except on PR + # https://github.com/docker/login-action + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - - - name: Compute short commit SHA ID - id: vars - run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)" - - - name: Build and push (tagged release) - uses: docker/build-push-action@v3 - if: ${{ github.event_name == 'push' }} + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v2 with: - platforms: linux/amd64 - context: . - push: true - tags: | - ${{ env.DOCKER_REPO }}:latest - ${{ env.DOCKER_REPO }}:${{ github.ref_name }} - ${{ env.DOCKER_REPO }}:commit-${{ steps.vars.outputs.sha_short }} - - - name: Build and push (manual release) - uses: docker/build-push-action@v3 - if: ${{ github.event_name == 'workflow_dispatch' }} + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v4 with: - platforms: linux/amd64 context: . - push: true - tags: | - ${{ env.DOCKER_REPO }}:commit-${{ steps.vars.outputs.sha_short }} + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + + + # Sign the resulting Docker image digest except on PRs. + # This will only write to the public Rekor transparency log when the Docker + # repository is public to avoid leaking data. If you would like to publish + # transparency data even for private images, pass --force to cosign below. + # https://github.com/sigstore/cosign + - name: Sign the published Docker image + if: ${{ github.event_name != 'pull_request' }} + env: + # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable + TAGS: ${{ steps.meta.outputs.tags }} + DIGEST: ${{ steps.build-and-push.outputs.digest }} + # This step uses the identity token to provision an ephemeral certificate + # against the sigstore community Fulcio instance. + run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}