ci.yml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.23.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@v3.4.1
  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@v2
  42. - name: Setup Go
  43. uses: actions/setup-go@v2
  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@v2.1.7
  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@v2.1.7
  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@v2
  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@v2
  81. - name: Setup Go
  82. uses: actions/setup-go@v2
  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@v2.1.7
  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@v2.1.7
  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@v2
  115. - name: Fetch History
  116. run: git fetch --prune --unshallow
  117. - name: Setup Go
  118. uses: actions/setup-go@v2
  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@v2.1.7
  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@v2.1.7
  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@v2.1.7
  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. steps:
  158. - name: Setup QEMU
  159. uses: docker/setup-qemu-action@v1
  160. with:
  161. platforms: all
  162. - name: Setup Docker Buildx
  163. uses: docker/setup-buildx-action@v1
  164. with:
  165. version: ${{ env.DOCKER_BUILDX_VERSION }}
  166. install: true
  167. - name: Checkout
  168. uses: actions/checkout@v2
  169. - name: Fetch History
  170. run: git fetch --prune --unshallow
  171. - name: Setup Go
  172. uses: actions/setup-go@v2
  173. with:
  174. go-version: ${{ env.GO_VERSION }}
  175. - name: Find the Go Cache
  176. id: go
  177. run: |
  178. echo "::set-output name=build-cache::$(go env GOCACHE)"
  179. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  180. - name: Cache the Go Build Cache
  181. uses: actions/cache@v2.1.7
  182. with:
  183. path: ${{ steps.go.outputs.build-cache }}
  184. key: ${{ runner.os }}-build-publish-artifacts-${{ hashFiles('**/go.sum') }}
  185. restore-keys: ${{ runner.os }}-build-publish-artifacts-
  186. - name: Cache Go Dependencies
  187. uses: actions/cache@v2.1.7
  188. with:
  189. path: ${{ steps.go.outputs.mod-cache }}
  190. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  191. restore-keys: ${{ runner.os }}-pkg-
  192. - name: Login to Docker
  193. uses: docker/login-action@v1
  194. if: env.GHCR_USERNAME != ''
  195. with:
  196. registry: ghcr.io
  197. username: ${{ secrets.GHCR_USERNAME }}
  198. password: ${{ secrets.GHCR_TOKEN }}
  199. - name: Build & Publish Artifacts
  200. if: env.GHCR_USERNAME != ''
  201. env:
  202. BUILD_ARGS: "--push --platform linux/amd64,linux/arm64"
  203. run: make docker.build
  204. - name: Promote Artifacts to main release channel
  205. if: github.ref == 'refs/heads/main' && env.GHCR_USERNAME != ''
  206. run: make docker.promote
  207. env:
  208. RELEASE_TAG: main