publish.yml 4.6 KB

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