publish.yml 4.9 KB

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