ci.yml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. name: CI
  2. on:
  3. push:
  4. branches:
  5. - main
  6. - release-*
  7. pull_request: {}
  8. workflow_dispatch: {}
  9. env:
  10. # Common versions
  11. GO_VERSION: '1.17'
  12. GOLANGCI_VERSION: 'v1.42.1'
  13. KUBERNETES_VERSION: '1.24.x'
  14. DOCKER_BUILDX_VERSION: 'v0.4.2'
  15. # Common users. We can't run a step 'if secrets.GHCR_USERNAME != ""' but we can run
  16. # a step 'if env.GHCR_USERNAME' != ""', so we copy these to succinctly test whether
  17. # credentials have been provided before trying to run steps that need them.
  18. GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
  19. # Sonar
  20. SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  21. jobs:
  22. detect-noop:
  23. runs-on: ubuntu-18.04
  24. outputs:
  25. noop: ${{ steps.noop.outputs.should_skip }}
  26. steps:
  27. - name: Detect No-op Changes
  28. id: noop
  29. uses: fkirc/skip-duplicate-actions@v4.0.0
  30. with:
  31. github_token: ${{ secrets.GITHUB_TOKEN }}
  32. paths_ignore: '["**.md", "**.png", "**.jpg"]'
  33. do_not_skip: '["workflow_dispatch", "schedule", "push"]'
  34. concurrent_skipping: false
  35. lint:
  36. runs-on: ubuntu-18.04
  37. needs: detect-noop
  38. if: needs.detect-noop.outputs.noop != 'true'
  39. steps:
  40. - name: Checkout
  41. uses: actions/checkout@v3
  42. - name: Setup Go
  43. uses: actions/setup-go@v3
  44. with:
  45. go-version: ${{ env.GO_VERSION }}
  46. - name: Find the Go Cache
  47. id: go
  48. run: |
  49. echo "::set-output name=build-cache::$(go env GOCACHE)"
  50. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  51. - name: Cache the Go Build Cache
  52. uses: actions/cache@v3
  53. with:
  54. path: ${{ steps.go.outputs.build-cache }}
  55. key: ${{ runner.os }}-build-lint-${{ hashFiles('**/go.sum') }}
  56. restore-keys: ${{ runner.os }}-build-lint-
  57. - name: Cache Go Dependencies
  58. uses: actions/cache@v3
  59. with:
  60. path: ${{ steps.go.outputs.mod-cache }}
  61. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  62. restore-keys: ${{ runner.os }}-pkg-
  63. # This action uses its own setup-go, which always seems to use the latest
  64. # stable version of Go. We could run 'make lint' to ensure our desired Go
  65. # version, but we prefer this action because it leaves 'annotations' (i.e.
  66. # it comments on PRs to point out linter violations).
  67. - name: Lint
  68. uses: golangci/golangci-lint-action@v3.2.0
  69. with:
  70. version: ${{ env.GOLANGCI_VERSION }}
  71. skip-pkg-cache: true
  72. skip-build-cache: true
  73. skip-go-installation: true
  74. check-diff:
  75. runs-on: ubuntu-18.04
  76. needs: detect-noop
  77. if: needs.detect-noop.outputs.noop != 'true'
  78. steps:
  79. - name: Checkout
  80. uses: actions/checkout@v3
  81. - name: Setup Go
  82. uses: actions/setup-go@v3
  83. with:
  84. go-version: ${{ env.GO_VERSION }}
  85. - name: Find the Go Cache
  86. id: go
  87. run: |
  88. echo "::set-output name=build-cache::$(go env GOCACHE)"
  89. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  90. - name: Cache the Go Build Cache
  91. uses: actions/cache@v3
  92. with:
  93. path: ${{ steps.go.outputs.build-cache }}
  94. key: ${{ runner.os }}-build-check-diff-${{ hashFiles('**/go.sum') }}
  95. restore-keys: ${{ runner.os }}-build-check-diff-
  96. - name: Cache Go Dependencies
  97. uses: actions/cache@v3
  98. with:
  99. path: ${{ steps.go.outputs.mod-cache }}
  100. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  101. restore-keys: ${{ runner.os }}-pkg-
  102. # Check DIff also runs Reviewable which needs golangci-lint installed
  103. - name: Check Diff
  104. run: |
  105. wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.42.1
  106. export PATH=$PATH:./bin
  107. make check-diff
  108. unit-tests:
  109. runs-on: ubuntu-18.04
  110. needs: detect-noop
  111. if: needs.detect-noop.outputs.noop != 'true'
  112. steps:
  113. - name: Checkout
  114. uses: actions/checkout@v3
  115. - name: Fetch History
  116. run: git fetch --prune --unshallow
  117. - name: Setup Go
  118. uses: actions/setup-go@v3
  119. with:
  120. go-version: ${{ env.GO_VERSION }}
  121. - name: Find the Go Cache
  122. id: go
  123. run: |
  124. echo "::set-output name=build-cache::$(go env GOCACHE)"
  125. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  126. - name: Cache the Go Build Cache
  127. uses: actions/cache@v3
  128. with:
  129. path: ${{ steps.go.outputs.build-cache }}
  130. key: ${{ runner.os }}-build-unit-tests-${{ hashFiles('**/go.sum') }}
  131. restore-keys: ${{ runner.os }}-build-unit-tests-
  132. - name: Cache Go Dependencies
  133. uses: actions/cache@v3
  134. with:
  135. path: ${{ steps.go.outputs.mod-cache }}
  136. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  137. restore-keys: ${{ runner.os }}-pkg-
  138. - name: Add setup-envtest
  139. run: |
  140. go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
  141. setup-envtest use ${{env.KUBERNETES_VERSION}} -p env --os $(go env GOOS) --arch $(go env GOARCH)
  142. - name: Cache envtest binaries
  143. uses: actions/cache@v3
  144. with:
  145. path: /home/runner/.local/share/kubebuilder-envtest/
  146. key: ${{ runner.os }}-kubebuilder-${{env.KUBERNETES_VERSION}}
  147. restore-keys: ${{ runner.os }}-kubebuilder-
  148. - name: Run Unit Tests
  149. run: |
  150. export KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT=true
  151. source <(setup-envtest use ${{env.KUBERNETES_VERSION}} -p env --os $(go env GOOS) --arch $(go env GOARCH))
  152. make test
  153. publish-artifacts:
  154. runs-on: ubuntu-18.04
  155. needs: detect-noop
  156. if: needs.detect-noop.outputs.noop != 'true'
  157. permissions:
  158. id-token: write
  159. contents: read
  160. steps:
  161. - name: Setup QEMU
  162. uses: docker/setup-qemu-action@v2
  163. with:
  164. platforms: all
  165. - name: Setup Docker Buildx
  166. uses: docker/setup-buildx-action@v2
  167. with:
  168. version: ${{ env.DOCKER_BUILDX_VERSION }}
  169. install: true
  170. - name: Checkout
  171. uses: actions/checkout@v3
  172. - name: Fetch History
  173. run: git fetch --prune --unshallow
  174. - name: Setup Go
  175. uses: actions/setup-go@v3
  176. with:
  177. go-version: ${{ env.GO_VERSION }}
  178. - name: Find the Go Cache
  179. id: go
  180. run: |
  181. echo "::set-output name=build-cache::$(go env GOCACHE)"
  182. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  183. - name: Cache the Go Build Cache
  184. uses: actions/cache@v3
  185. with:
  186. path: ${{ steps.go.outputs.build-cache }}
  187. key: ${{ runner.os }}-build-publish-artifacts-${{ hashFiles('**/go.sum') }}
  188. restore-keys: ${{ runner.os }}-build-publish-artifacts-
  189. - name: Cache Go Dependencies
  190. uses: actions/cache@v3
  191. with:
  192. path: ${{ steps.go.outputs.mod-cache }}
  193. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  194. restore-keys: ${{ runner.os }}-pkg-
  195. - name: Login to Docker
  196. uses: docker/login-action@v2
  197. if: env.GHCR_USERNAME != ''
  198. with:
  199. registry: ghcr.io
  200. username: ${{ secrets.GHCR_USERNAME }}
  201. password: ${{ secrets.GHCR_TOKEN }}
  202. - name: Build & Publish Artifacts
  203. if: env.GHCR_USERNAME != ''
  204. env:
  205. BUILD_ARGS: "--push --platform linux/amd64,linux/arm64"
  206. run: make docker.build
  207. - name: Promote Artifacts to main release channel
  208. if: github.ref == 'refs/heads/main' && env.GHCR_USERNAME != ''
  209. run: make docker.promote
  210. env:
  211. RELEASE_TAG: main
  212. - name: Set up crane
  213. if: github.ref == 'refs/heads/main' && env.GHCR_USERNAME != ''
  214. run: go install github.com/google/go-containerregistry/cmd/crane@v0.8.0
  215. - name: Install cosign
  216. if: github.ref == 'refs/heads/main' && env.GHCR_USERNAME != ''
  217. uses: sigstore/cosign-installer@v2.5.1
  218. - name: Sign Artifacts to main release channel
  219. if: github.ref == 'refs/heads/main' && env.GHCR_USERNAME != ''
  220. run: make docker.sign
  221. env:
  222. RELEASE_TAG: main
  223. COSIGN_EXPERIMENTAL: true