publish.yml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. tag-suffix:
  9. required: true
  10. type: string
  11. dockerfile:
  12. required: true
  13. type: string
  14. secrets:
  15. GHCR_USERNAME:
  16. required: true
  17. GHCR_TOKEN:
  18. required: true
  19. env:
  20. IMAGE_NAME: ${{ inputs.image-name }}
  21. TAG_SUFFIX: ${{ inputs.tag-suffix }}
  22. DOCKERFILE: ${{ inputs.dockerfile }}
  23. IS_FORK: ${{ secrets.GHCR_USERNAME == '' && 'true' || 'false' }}
  24. jobs:
  25. build-publish:
  26. name: Build and Publish
  27. runs-on: ubuntu-latest
  28. outputs:
  29. image-tag: ${{ steps.container_info.outputs.image-tag }}
  30. steps:
  31. - name: Checkout
  32. uses: actions/checkout@v3
  33. - name: Setup QEMU
  34. uses: docker/setup-qemu-action@v2
  35. with:
  36. platforms: all
  37. - name: Setup Docker Buildx
  38. uses: docker/setup-buildx-action@v2
  39. with:
  40. version: 'v0.4.2'
  41. install: true
  42. - name: Setup Go
  43. uses: actions/setup-go@v3
  44. with:
  45. go-version-file: "go.mod"
  46. - name: Fetch History
  47. shell: bash
  48. run: git fetch --prune --unshallow
  49. - name: Find the Go Cache
  50. shell: bash
  51. id: go
  52. run: |
  53. echo "::set-output name=build-cache::$(go env GOCACHE)"
  54. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  55. - name: Cache the Go Build Cache
  56. uses: actions/cache@v3
  57. with:
  58. path: ${{ steps.go.outputs.build-cache }}
  59. key: ${{ runner.os }}-build-${{ github.sha }}-${{ hashFiles('**/go.sum') }}
  60. - name: Cache Go Dependencies
  61. uses: actions/cache@v3
  62. with:
  63. path: ${{ steps.go.outputs.mod-cache }}
  64. key: ${{ runner.os }}-mod-${{ github.sha }}-${{ hashFiles('**/go.sum') }}
  65. - name: Login to Docker
  66. uses: docker/login-action@v2
  67. if: env.IS_FORK == 'false'
  68. with:
  69. registry: ghcr.io
  70. username: ${{ secrets.GHCR_USERNAME }}
  71. password: ${{ secrets.GHCR_TOKEN }}
  72. - name: Get docker image tag
  73. id: container_info
  74. shell: bash
  75. env:
  76. GITHUB_REF: ${{ github.ref }}
  77. run: |
  78. if [ "$GITHUB_REF" == "refs/heads/main" ]; then
  79. TAG=main${{ inputs.tag-suffix }}
  80. else
  81. TAG=$(make docker.tag)
  82. fi
  83. echo "::set-output name=image-tag::${TAG}"
  84. - name: Build & Publish Artifacts
  85. if: env.IS_FORK == 'false'
  86. shell: bash
  87. env:
  88. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  89. BUILD_ARGS: >-
  90. --push
  91. --platform linux/amd64,linux/arm64
  92. run: make docker.build
  93. - name: Build & Publish Artifacts fork
  94. if: env.IS_FORK == 'true'
  95. shell: bash
  96. env:
  97. IMAGE_TAG: ${{ steps.container_info.outputs.image-tag }}
  98. BUILD_ARGS: --load
  99. run: make docker.build
  100. - name: Run Trivy vulnerability scanner
  101. uses: aquasecurity/trivy-action@master
  102. with:
  103. image-ref: ${{ inputs.image-name }}:${{ steps.container_info.outputs.image-tag }}
  104. format: 'table'
  105. exit-code: '1'
  106. ignore-unfixed: true
  107. vuln-type: 'os,library'
  108. severity: 'CRITICAL,HIGH'
  109. sign:
  110. runs-on: ubuntu-latest
  111. needs: build-publish
  112. steps:
  113. - name: Checkout
  114. uses: actions/checkout@v3
  115. - name: Sign image
  116. if: env.IS_FORK == 'false'
  117. uses: ./.github/actions/sign
  118. with:
  119. image-name: ${{ inputs.image-name }}
  120. image-tag: ${{ needs.build-publish.outputs.image-tag }}
  121. GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
  122. GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
  123. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}