publish.yml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. - name: Checkout
  56. uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
  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@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0
  65. with:
  66. version: 'v0.4.2'
  67. install: true
  68. - name: Setup Go
  69. uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5.4.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@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
  81. if: env.IS_FORK != ''
  82. with:
  83. registry: ghcr.io
  84. username: ${{ inputs.username }}
  85. password: ${{ secrets.GHCR_TOKEN }}
  86. - name: Get docker image tag
  87. id: container_info
  88. shell: bash
  89. env:
  90. GITHUB_REF: ${{ github.ref }}
  91. run: |
  92. # rebuild-image
  93. if [ "${{ inputs.image-tag }}" != "" ]; then
  94. TAG="${{ inputs.image-tag }}${{ inputs.tag-suffix }}"
  95. # main / release-x.y
  96. elif [[ "$GITHUB_REF" == "refs/heads/main" || "$GITHUB_REF" =~ refs/heads/release-.* ]]; then
  97. TAG=${GITHUB_REF#refs/heads/}${{ inputs.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. --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: --load
  120. run: make docker.build
  121. - name: Run Trivy vulnerability scanner
  122. uses: aquasecurity/trivy-action@6c175e9c4083a92bbca2f9724c8a5e33bc2d97a5 # master
  123. with:
  124. image-ref: ${{ inputs.image-name }}:${{ steps.container_info.outputs.image-tag }}
  125. format: 'table'
  126. exit-code: '1'
  127. ignore-unfixed: true
  128. vuln-type: 'os,library'
  129. severity: 'CRITICAL,HIGH'
  130. sign:
  131. runs-on: ubuntu-latest
  132. needs: build-publish
  133. permissions:
  134. contents: read
  135. id-token: write #for keyless sign
  136. packages: write #to update packages with added SBOMs.
  137. steps:
  138. - name: Checkout
  139. uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
  140. - name: Sign image
  141. if: env.IS_FORK != ''
  142. uses: ./.github/actions/sign
  143. with:
  144. image-name: ${{ inputs.image-name }}
  145. image-tag: ${{ needs.build-publish.outputs.image-tag }}
  146. GHCR_USERNAME: ${{ inputs.username }}
  147. GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
  148. GITHUB_TOKEN: ${{ secrets.GHCR_TOKEN }}