publish.yml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. name: Reusable workflow to run trivy scan
  2. on:
  3. workflow_call:
  4. inputs:
  5. image-name:
  6. required: true
  7. type: string
  8. image-tag:
  9. required: false
  10. type: string
  11. tag-suffix:
  12. required: true
  13. type: string
  14. dockerfile:
  15. required: true
  16. type: string
  17. ref:
  18. required: false
  19. default: main
  20. type: string
  21. build-args:
  22. required: true
  23. type: string
  24. build-arch:
  25. required: true
  26. type: string
  27. build-platform:
  28. required: true
  29. type: string
  30. secrets:
  31. IS_FORK:
  32. required: false
  33. env:
  34. IMAGE_NAME: ${{ inputs.image-name }}
  35. TAG_SUFFIX: ${{ inputs.tag-suffix }}
  36. ARCH: ${{ inputs.build-arch }}
  37. DOCKERFILE: ${{ inputs.dockerfile }}
  38. IS_FORK: ${{ secrets.IS_FORK }}
  39. permissions:
  40. contents: read
  41. jobs:
  42. build-publish:
  43. name: Build and Publish
  44. runs-on: ubuntu-latest
  45. permissions:
  46. contents: read
  47. packages: write
  48. id-token: write
  49. outputs:
  50. image-tag: ${{ steps.container_info.outputs.image-tag }}
  51. steps:
  52. - uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
  53. with:
  54. egress-policy: audit
  55. - name: Checkout
  56. uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
  57. with:
  58. ref: ${{ inputs.ref }}
  59. - name: Setup QEMU
  60. uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
  61. with:
  62. platforms: all
  63. - name: Setup Docker Buildx
  64. uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
  65. with:
  66. version: 'v0.4.2'
  67. install: true
  68. - name: Setup Go
  69. uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
  70. id: setup-go
  71. with:
  72. go-version-file: "go.mod"
  73. - name: Download Go modules
  74. if: ${{ steps.setup-go.outputs.cache-hit != 'true' }}
  75. run: go mod download
  76. - name: Fetch History
  77. shell: bash
  78. run: git fetch --prune --unshallow
  79. - name: Login to Docker
  80. uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
  81. if: env.IS_FORK != ''
  82. with:
  83. registry: ghcr.io
  84. username: ${{ github.actor }}
  85. password: ${{ github.token }}
  86. - name: Get docker image tag
  87. id: container_info
  88. shell: bash
  89. env:
  90. GITHUB_REF: ${{ github.ref }}
  91. INPUT_IMAGE_TAG: ${{ inputs.image-tag }}
  92. INPUT_TAG_SUFFIX: ${{ inputs.tag-suffix }}
  93. run: |
  94. # rebuild-image
  95. if [ "$INPUT_IMAGE_TAG" != "" ]; then
  96. TAG="${INPUT_IMAGE_TAG}${INPUT_TAG_SUFFIX}"
  97. # main
  98. elif [[ "$GITHUB_REF" == "refs/heads/main" ]]; then
  99. TAG=${GITHUB_REF#refs/heads/}${INPUT_TAG_SUFFIX}
  100. # Pull Request
  101. else
  102. TAG=$(make docker.tag)
  103. fi
  104. echo "image-tag=${TAG}" >> $GITHUB_OUTPUT
  105. - name: Build & Publish Artifacts
  106. if: env.IS_FORK != ''
  107. shell: bash
  108. env:
  109. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  110. BUILD_ARGS: ${{ inputs.build-args }}
  111. DOCKER_BUILD_ARGS: >-
  112. --push
  113. --platform ${{ inputs.build-platform }}
  114. run: make docker.build
  115. - name: Build & Publish Artifacts fork
  116. if: env.IS_FORK == ''
  117. shell: bash
  118. env:
  119. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  120. BUILD_ARGS: ${{ inputs.build-args }}
  121. DOCKER_BUILD_ARGS: --load
  122. run: make docker.build
  123. # images are large to the point trivy fails due to no space on disk left
  124. # This is a silly attempt to clean up space for trivy to run more
  125. # consistently
  126. - name: Cleanup unused cache
  127. shell: bash
  128. run: |
  129. docker system prune --force
  130. go clean -cache
  131. go clean -modcache
  132. - name: Run Trivy vulnerability scanner
  133. uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 # master
  134. with:
  135. image-ref: ${{ inputs.image-name }}:${{ steps.container_info.outputs.image-tag }}
  136. format: 'table'
  137. exit-code: '1'
  138. ignore-unfixed: true
  139. vuln-type: 'os,library'
  140. severity: 'CRITICAL,HIGH'
  141. sign:
  142. runs-on: ubuntu-latest
  143. needs: build-publish
  144. permissions:
  145. contents: read
  146. id-token: write #for keyless sign
  147. packages: write #to update packages with added SBOMs.
  148. steps:
  149. - uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
  150. with:
  151. egress-policy: audit
  152. - name: Checkout
  153. uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
  154. - name: Sign image
  155. if: env.IS_FORK != ''
  156. uses: ./.github/actions/sign
  157. with:
  158. image-name: ${{ inputs.image-name }}
  159. image-tag: ${{ needs.build-publish.outputs.image-tag }}