publish.yml 4.6 KB

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