publish.yml 4.9 KB

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