publish.yml 4.6 KB

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