docker-publish.yml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. name: Docker
  2. # This workflow uses actions that are not certified by GitHub.
  3. # They are provided by a third-party and are governed by
  4. # separate terms of service, privacy policy, and support
  5. # documentation.
  6. on:
  7. # schedule:
  8. # - cron: '30 20 * * *'
  9. push:
  10. branches: ["*"]
  11. # Publish semver tags as releases.
  12. tags: ["v*.*.*"]
  13. pull_request:
  14. branches: ["{{is_default_branch}}"]
  15. env:
  16. # Use docker.io for Docker Hub if empty
  17. REGISTRY: ghcr.io
  18. # github.repository as <account>/<repo>
  19. IMAGE_NAME: ${{ github.repository }}
  20. jobs:
  21. build:
  22. runs-on: ubuntu-latest
  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. steps:
  30. - name: Checkout repository
  31. uses: actions/checkout@v3
  32. # Install the cosign tool except on PR
  33. # https://github.com/sigstore/cosign-installer
  34. - name: Install cosign
  35. if: github.event_name != 'pull_request'
  36. uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1
  37. with:
  38. cosign-release: "v2.1.1"
  39. # Set up BuildKit Docker container builder to be able to build
  40. # multi-platform images and export cache
  41. # https://github.com/docker/setup-buildx-action
  42. - name: Set up Docker Buildx
  43. uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
  44. # Login against a Docker registry except on PR
  45. # https://github.com/docker/login-action
  46. - name: Log into registry ${{ env.REGISTRY }}
  47. if: github.event_name != 'pull_request'
  48. uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
  49. with:
  50. registry: ${{ env.REGISTRY }}
  51. username: ${{ github.actor }}
  52. password: ${{ secrets.GITHUB_TOKEN }}
  53. # Extract metadata (tags, labels) for Docker
  54. # https://github.com/docker/metadata-action
  55. - name: Extract Docker metadata
  56. id: meta
  57. uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
  58. with:
  59. images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
  60. tags: |
  61. type=ref,event=branch
  62. type=ref,event=pr
  63. # set latest tag for default branch
  64. type=raw,value=latest,enable={{is_default_branch}}
  65. type=semver,pattern={{version}}
  66. type=semver,pattern={{major}}.{{minor}}
  67. # Build and push Docker image with Buildx (don't push on PR)
  68. # https://github.com/docker/build-push-action
  69. - name: Build and push Docker image
  70. id: build-and-push
  71. uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
  72. with:
  73. context: .
  74. push: ${{ github.event_name != 'pull_request' }}
  75. tags: ${{ steps.meta.outputs.tags }}
  76. labels: ${{ steps.meta.outputs.labels }}
  77. cache-from: type=gha
  78. cache-to: type=gha,mode=max
  79. # Sign the resulting Docker image digest except on PRs.
  80. # This will only write to the public Rekor transparency log when the Docker
  81. # repository is public to avoid leaking data. If you would like to publish
  82. # transparency data even for private images, pass --force to cosign below.
  83. # https://github.com/sigstore/cosign
  84. - name: Sign the published Docker image
  85. if: ${{ github.event_name != 'pull_request' }}
  86. env:
  87. # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
  88. TAGS: ${{ steps.meta.outputs.tags }}
  89. DIGEST: ${{ steps.build-and-push.outputs.digest }}
  90. # This step uses the identity token to provision an ephemeral certificate
  91. # against the sigstore community Fulcio instance.
  92. run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}