publish.yml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
  57. with:
  58. ref: ${{ inputs.ref }}
  59. - name: Setup QEMU
  60. uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.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@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.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. - name: Run Trivy vulnerability scanner
  124. uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 # master
  125. with:
  126. image-ref: ${{ inputs.image-name }}:${{ steps.container_info.outputs.image-tag }}
  127. format: 'table'
  128. exit-code: '1'
  129. ignore-unfixed: true
  130. vuln-type: 'os,library'
  131. severity: 'CRITICAL,HIGH'
  132. sign:
  133. runs-on: ubuntu-latest
  134. needs: build-publish
  135. permissions:
  136. contents: read
  137. id-token: write #for keyless sign
  138. packages: write #to update packages with added SBOMs.
  139. steps:
  140. - uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
  141. with:
  142. egress-policy: audit
  143. - name: Checkout
  144. uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
  145. - name: Sign image
  146. if: env.IS_FORK != ''
  147. uses: ./.github/actions/sign
  148. with:
  149. image-name: ${{ inputs.image-name }}
  150. image-tag: ${{ needs.build-publish.outputs.image-tag }}