docker-publish.yml 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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: '24 15 * * *'
  9. push:
  10. branches: [ "main" ]
  11. # Publish semver tags as releases.
  12. tags: [ 'v*.*.*' ]
  13. pull_request:
  14. branches: [ "main" ]
  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@v4
  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@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 #v3.5.0
  37. with:
  38. cosign-release: 'v2.2.4'
  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. # Build and push Docker image with Buildx (don't push on PR)
  61. # https://github.com/docker/build-push-action
  62. - name: Build and push Docker image
  63. id: build-and-push
  64. uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
  65. with:
  66. context: .
  67. push: ${{ github.event_name != 'pull_request' }}
  68. tags: ${{ steps.meta.outputs.tags }}
  69. labels: ${{ steps.meta.outputs.labels }}
  70. cache-from: type=gha
  71. cache-to: type=gha,mode=max
  72. # Sign the resulting Docker image digest except on PRs.
  73. # This will only write to the public Rekor transparency log when the Docker
  74. # repository is public to avoid leaking data. If you would like to publish
  75. # transparency data even for private images, pass --force to cosign below.
  76. # https://github.com/sigstore/cosign
  77. - name: Sign the published Docker image
  78. if: ${{ github.event_name != 'pull_request' }}
  79. env:
  80. # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
  81. TAGS: ${{ steps.meta.outputs.tags }}
  82. DIGEST: ${{ steps.build-and-push.outputs.digest }}
  83. # This step uses the identity token to provision an ephemeral certificate
  84. # against the sigstore community Fulcio instance.
  85. run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}