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@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
  53. with:
  54. egress-policy: audit
  55. - name: Checkout
  56. uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
  57. with:
  58. ref: ${{ inputs.ref }}
  59. persist-credentials: false
  60. - name: Setup QEMU
  61. uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
  62. with:
  63. platforms: all
  64. - name: Setup Docker Buildx
  65. uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
  66. - name: Setup Go
  67. uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
  68. id: setup-go
  69. with:
  70. go-version-file: "go.mod"
  71. - name: Download Go modules
  72. run: go mod download
  73. - name: Fetch History
  74. shell: bash
  75. run: git fetch --prune --unshallow
  76. - name: Login to Docker
  77. uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
  78. if: env.IS_FORK != ''
  79. with:
  80. registry: ghcr.io
  81. username: ${{ github.actor }}
  82. password: ${{ github.token }}
  83. - name: Get docker image tag
  84. id: container_info
  85. shell: bash
  86. env:
  87. GITHUB_REF: ${{ github.ref }}
  88. INPUT_IMAGE_TAG: ${{ inputs.image-tag }}
  89. INPUT_TAG_SUFFIX: ${{ inputs.tag-suffix }}
  90. run: |
  91. # rebuild-image
  92. if [ "$INPUT_IMAGE_TAG" != "" ]; then
  93. TAG="${INPUT_IMAGE_TAG}${INPUT_TAG_SUFFIX}"
  94. # main
  95. elif [[ "$GITHUB_REF" == "refs/heads/main" ]]; then
  96. TAG=${GITHUB_REF#refs/heads/}${INPUT_TAG_SUFFIX}
  97. # Pull Request
  98. else
  99. TAG=$(make docker.tag)
  100. fi
  101. echo "image-tag=${TAG}" >> $GITHUB_OUTPUT
  102. - name: Build & Publish Artifacts
  103. if: env.IS_FORK != ''
  104. shell: bash
  105. env:
  106. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  107. BUILD_ARGS: ${{ inputs.build-args }}
  108. DOCKER_BUILD_ARGS: >-
  109. --no-cache
  110. --push
  111. --platform ${{ inputs.build-platform }}
  112. run: make docker.build
  113. - name: Build & Publish Artifacts fork
  114. if: env.IS_FORK == ''
  115. shell: bash
  116. env:
  117. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  118. BUILD_ARGS: ${{ inputs.build-args }}
  119. DOCKER_BUILD_ARGS: --no-cache --load
  120. run: make docker.build
  121. # images are large to the point trivy fails due to no space on disk left
  122. # This is a silly attempt to clean up space for trivy to run more
  123. # consistently
  124. - name: Cleanup unused cache
  125. shell: bash
  126. run: |
  127. docker system prune --force
  128. go clean -cache
  129. go clean -modcache
  130. - name: Run Trivy vulnerability scanner
  131. uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1 # master
  132. with:
  133. image-ref: ${{ inputs.image-name }}:${{ steps.container_info.outputs.image-tag }}
  134. format: 'table'
  135. exit-code: '1'
  136. ignore-unfixed: true
  137. vuln-type: 'os,library'
  138. severity: 'CRITICAL,HIGH'
  139. sign:
  140. runs-on: ubuntu-latest
  141. needs: build-publish
  142. permissions:
  143. contents: read
  144. id-token: write #for keyless sign
  145. packages: write #to update packages with added SBOMs.
  146. steps:
  147. - uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
  148. with:
  149. egress-policy: audit
  150. - name: Checkout
  151. uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
  152. with:
  153. persist-credentials: false
  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 }}