publish.yml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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@v5
  63. id: setup-go
  64. with:
  65. go-version-file: "go.mod"
  66. - name: Download Go modules
  67. if: ${{ steps.setup-go.outputs.cache-hit != 'true' }}
  68. run: go mod download
  69. - name: Fetch History
  70. shell: bash
  71. run: git fetch --prune --unshallow
  72. - name: Login to Docker
  73. uses: docker/login-action@v3
  74. if: env.IS_FORK == 'false'
  75. with:
  76. registry: ghcr.io
  77. username: ${{ secrets.GHCR_USERNAME }}
  78. password: ${{ secrets.GHCR_TOKEN }}
  79. - name: Get docker image tag
  80. id: container_info
  81. shell: bash
  82. env:
  83. GITHUB_REF: ${{ github.ref }}
  84. run: |
  85. # rebuild-image
  86. if [ "${{ inputs.image-tag }}" != "" ]; then
  87. TAG="${{ inputs.image-tag }}${{ inputs.tag-suffix }}"
  88. # main / release-x.y
  89. elif [[ "$GITHUB_REF" == "refs/heads/main" || "$GITHUB_REF" =~ refs/heads/release-.* ]]; then
  90. TAG=${GITHUB_REF#refs/heads/}${{ inputs.tag-suffix }}
  91. # Pull Request
  92. else
  93. TAG=$(make docker.tag)
  94. fi
  95. echo "::set-output name=image-tag::${TAG}"
  96. - name: Build & Publish Artifacts
  97. if: env.IS_FORK == 'false'
  98. shell: bash
  99. env:
  100. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  101. BUILD_ARGS: ${{ inputs.build-args }}
  102. DOCKER_BUILD_ARGS: >-
  103. --push
  104. --platform ${{ inputs.build-platform }}
  105. run: make docker.build
  106. - name: Build & Publish Artifacts fork
  107. if: env.IS_FORK == 'true'
  108. shell: bash
  109. env:
  110. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  111. BUILD_ARGS: ${{ inputs.build-args }}
  112. DOCKER_BUILD_ARGS: --load
  113. run: make docker.build
  114. - name: Run Trivy vulnerability scanner
  115. uses: aquasecurity/trivy-action@master
  116. with:
  117. image-ref: ${{ inputs.image-name }}:${{ steps.container_info.outputs.image-tag }}
  118. format: 'table'
  119. exit-code: '1'
  120. ignore-unfixed: true
  121. vuln-type: 'os,library'
  122. severity: 'CRITICAL,HIGH'
  123. sign:
  124. runs-on: ubuntu-latest
  125. needs: build-publish
  126. steps:
  127. - name: Checkout
  128. uses: actions/checkout@v4
  129. - name: Sign image
  130. if: env.IS_FORK == 'false'
  131. uses: ./.github/actions/sign
  132. with:
  133. image-name: ${{ inputs.image-name }}
  134. image-tag: ${{ needs.build-publish.outputs.image-tag }}
  135. GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
  136. GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
  137. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}