publish.yml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. username:
  31. required: true
  32. type: string
  33. secrets:
  34. GHCR_TOKEN:
  35. required: true
  36. IS_FORK:
  37. required: false
  38. env:
  39. IMAGE_NAME: ${{ inputs.image-name }}
  40. TAG_SUFFIX: ${{ inputs.tag-suffix }}
  41. ARCH: ${{ inputs.build-arch }}
  42. DOCKERFILE: ${{ inputs.dockerfile }}
  43. IS_FORK: ${{ secrets.IS_FORK }}
  44. jobs:
  45. build-publish:
  46. name: Build and Publish
  47. runs-on: ubuntu-latest
  48. permissions:
  49. contents: read
  50. packages: write
  51. id-token: write
  52. outputs:
  53. image-tag: ${{ steps.container_info.outputs.image-tag }}
  54. steps:
  55. - uses: step-security/harden-runner@c6295a65d1254861815972266d5933fd6e532bdf # v2.11.1
  56. with:
  57. egress-policy: audit
  58. - name: Checkout
  59. uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
  60. with:
  61. ref: ${{ inputs.ref }}
  62. - name: Setup QEMU
  63. uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0
  64. with:
  65. platforms: all
  66. - name: Setup Docker Buildx
  67. uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0
  68. with:
  69. version: 'v0.4.2'
  70. install: true
  71. - name: Setup Go
  72. uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5.4.0
  73. id: setup-go
  74. with:
  75. go-version-file: "go.mod"
  76. - name: Download Go modules
  77. if: ${{ steps.setup-go.outputs.cache-hit != 'true' }}
  78. run: go mod download
  79. - name: Fetch History
  80. shell: bash
  81. run: git fetch --prune --unshallow
  82. - name: Login to Docker
  83. uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
  84. if: env.IS_FORK != ''
  85. with:
  86. registry: ghcr.io
  87. username: ${{ inputs.username }}
  88. password: ${{ secrets.GHCR_TOKEN }}
  89. - name: Get docker image tag
  90. id: container_info
  91. shell: bash
  92. env:
  93. GITHUB_REF: ${{ github.ref }}
  94. run: |
  95. # rebuild-image
  96. if [ "${{ inputs.image-tag }}" != "" ]; then
  97. TAG="${{ inputs.image-tag }}${{ inputs.tag-suffix }}"
  98. # main / release-x.y
  99. elif [[ "$GITHUB_REF" == "refs/heads/main" || "$GITHUB_REF" =~ refs/heads/release-.* ]]; then
  100. TAG=${GITHUB_REF#refs/heads/}${{ inputs.tag-suffix }}
  101. # Pull Request
  102. else
  103. TAG=$(make docker.tag)
  104. fi
  105. echo "image-tag=${TAG}" >> $GITHUB_OUTPUT
  106. - name: Build & Publish Artifacts
  107. if: env.IS_FORK != ''
  108. shell: bash
  109. env:
  110. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  111. BUILD_ARGS: ${{ inputs.build-args }}
  112. DOCKER_BUILD_ARGS: >-
  113. --push
  114. --platform ${{ inputs.build-platform }}
  115. run: make docker.build
  116. - name: Build & Publish Artifacts fork
  117. if: env.IS_FORK == ''
  118. shell: bash
  119. env:
  120. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  121. BUILD_ARGS: ${{ inputs.build-args }}
  122. DOCKER_BUILD_ARGS: --load
  123. run: make docker.build
  124. - name: Run Trivy vulnerability scanner
  125. uses: aquasecurity/trivy-action@6c175e9c4083a92bbca2f9724c8a5e33bc2d97a5 # master
  126. with:
  127. image-ref: ${{ inputs.image-name }}:${{ steps.container_info.outputs.image-tag }}
  128. format: 'table'
  129. exit-code: '1'
  130. ignore-unfixed: true
  131. vuln-type: 'os,library'
  132. severity: 'CRITICAL,HIGH'
  133. sign:
  134. runs-on: ubuntu-latest
  135. needs: build-publish
  136. permissions:
  137. contents: read
  138. id-token: write #for keyless sign
  139. packages: write #to update packages with added SBOMs.
  140. steps:
  141. - uses: step-security/harden-runner@c6295a65d1254861815972266d5933fd6e532bdf # v2.11.1
  142. with:
  143. egress-policy: audit
  144. - name: Checkout
  145. uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
  146. - name: Sign image
  147. if: env.IS_FORK != ''
  148. uses: ./.github/actions/sign
  149. with:
  150. image-name: ${{ inputs.image-name }}
  151. image-tag: ${{ needs.build-publish.outputs.image-tag }}
  152. GHCR_USERNAME: ${{ inputs.username }}
  153. GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
  154. GITHUB_TOKEN: ${{ secrets.GHCR_TOKEN }}