publish.yml 4.4 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. 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@v3
  50. with:
  51. ref: ${{ inputs.ref }}
  52. - name: Setup QEMU
  53. uses: docker/setup-qemu-action@v2
  54. with:
  55. platforms: all
  56. - name: Setup Docker Buildx
  57. uses: docker/setup-buildx-action@v2
  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@v2
  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. if [ "${{ inputs.image-tag }}" != "" ]; then
  98. TAG="${{ inputs.image-tag }}${{ inputs.tag-suffix }}"
  99. elif [ "$GITHUB_REF" == "refs/heads/main" ]; then
  100. TAG=main${{ inputs.tag-suffix }}
  101. else
  102. TAG=$(make docker.tag)
  103. fi
  104. echo "::set-output name=image-tag::${TAG}"
  105. - name: Build & Publish Artifacts
  106. if: env.IS_FORK == 'false'
  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 == 'true'
  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@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. steps:
  136. - name: Checkout
  137. uses: actions/checkout@v3
  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: ${{ secrets.GHCR_USERNAME }}
  145. GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
  146. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}