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