ci.yml 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.15'
  12. GOLANGCI_VERSION: 'v1.33'
  13. KUBEBUILDER_VERSION: '2.3.1'
  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. GCP_SM_SA_JSON: ${{ secrets.GCP_SM_SA_JSON}}
  20. jobs:
  21. detect-noop:
  22. runs-on: ubuntu-18.04
  23. outputs:
  24. noop: ${{ steps.noop.outputs.should_skip }}
  25. steps:
  26. - name: Detect No-op Changes
  27. id: noop
  28. uses: fkirc/skip-duplicate-actions@v3.4.0
  29. with:
  30. github_token: ${{ secrets.GITHUB_TOKEN }}
  31. paths_ignore: '["**.md", "**.png", "**.jpg"]'
  32. do_not_skip: '["workflow_dispatch", "schedule", "push"]'
  33. concurrent_skipping: false
  34. lint:
  35. runs-on: ubuntu-18.04
  36. needs: detect-noop
  37. if: needs.detect-noop.outputs.noop != 'true'
  38. steps:
  39. - name: Checkout
  40. uses: actions/checkout@v2
  41. - name: Find the Go Cache
  42. id: go
  43. run: |
  44. echo "::set-output name=build-cache::$(go env GOCACHE)"
  45. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  46. - name: Cache the Go Build Cache
  47. uses: actions/cache@v2.1.6
  48. with:
  49. path: ${{ steps.go.outputs.build-cache }}
  50. key: ${{ runner.os }}-build-lint-${{ hashFiles('**/go.sum') }}
  51. restore-keys: ${{ runner.os }}-build-lint-
  52. - name: Cache Go Dependencies
  53. uses: actions/cache@v2.1.6
  54. with:
  55. path: ${{ steps.go.outputs.mod-cache }}
  56. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  57. restore-keys: ${{ runner.os }}-pkg-
  58. # This action uses its own setup-go, which always seems to use the latest
  59. # stable version of Go. We could run 'make lint' to ensure our desired Go
  60. # version, but we prefer this action because it leaves 'annotations' (i.e.
  61. # it comments on PRs to point out linter violations).
  62. - name: Lint
  63. uses: golangci/golangci-lint-action@v2
  64. with:
  65. version: ${{ env.GOLANGCI_VERSION }}
  66. skip-pkg-cache: true
  67. skip-build-cache: true
  68. check-diff:
  69. runs-on: ubuntu-18.04
  70. needs: detect-noop
  71. if: needs.detect-noop.outputs.noop != 'true'
  72. steps:
  73. - name: Checkout
  74. uses: actions/checkout@v2
  75. - name: Setup Go
  76. uses: actions/setup-go@v2
  77. with:
  78. go-version: ${{ env.GO_VERSION }}
  79. - name: Find the Go Cache
  80. id: go
  81. run: |
  82. echo "::set-output name=build-cache::$(go env GOCACHE)"
  83. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  84. - name: Cache the Go Build Cache
  85. uses: actions/cache@v2.1.6
  86. with:
  87. path: ${{ steps.go.outputs.build-cache }}
  88. key: ${{ runner.os }}-build-check-diff-${{ hashFiles('**/go.sum') }}
  89. restore-keys: ${{ runner.os }}-build-check-diff-
  90. - name: Cache Go Dependencies
  91. uses: actions/cache@v2.1.6
  92. with:
  93. path: ${{ steps.go.outputs.mod-cache }}
  94. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  95. restore-keys: ${{ runner.os }}-pkg-
  96. - name: Check Diff
  97. run: make check-diff
  98. unit-tests:
  99. runs-on: ubuntu-18.04
  100. needs: detect-noop
  101. if: needs.detect-noop.outputs.noop != 'true'
  102. steps:
  103. - name: Checkout
  104. uses: actions/checkout@v2
  105. - name: Fetch History
  106. run: git fetch --prune --unshallow
  107. - name: Setup Go
  108. uses: actions/setup-go@v2
  109. with:
  110. go-version: ${{ env.GO_VERSION }}
  111. - name: Find the Go Cache
  112. id: go
  113. run: |
  114. echo "::set-output name=build-cache::$(go env GOCACHE)"
  115. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  116. - name: Cache the Go Build Cache
  117. uses: actions/cache@v2.1.6
  118. with:
  119. path: ${{ steps.go.outputs.build-cache }}
  120. key: ${{ runner.os }}-build-unit-tests-${{ hashFiles('**/go.sum') }}
  121. restore-keys: ${{ runner.os }}-build-unit-tests-
  122. - name: Cache Go Dependencies
  123. uses: actions/cache@v2.1.6
  124. with:
  125. path: ${{ steps.go.outputs.mod-cache }}
  126. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  127. restore-keys: ${{ runner.os }}-pkg-
  128. - name: Add kubebuilder
  129. run: |
  130. curl -L https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${{env.KUBEBUILDER_VERSION}}/kubebuilder_${{env.KUBEBUILDER_VERSION}}_linux_amd64.tar.gz > kubebuilder_${{env.KUBEBUILDER_VERSION}}_linux_amd64.tar.gz
  131. tar -xvf kubebuilder_${{env.KUBEBUILDER_VERSION}}_linux_amd64.tar.gz
  132. sudo mv kubebuilder_${{env.KUBEBUILDER_VERSION}}_linux_amd64 /usr/local/kubebuilder
  133. - name: Cache kubebuilder
  134. uses: actions/cache@v2.1.6
  135. with:
  136. path: /usr/local/kubebuilder
  137. key: ${{ runner.os }}-kubebuilder-${{env.KUBEBUILDER_VERSION}}
  138. restore-keys: ${{ runner.os }}-kubebuilder-
  139. - name: Run Unit Tests
  140. run: make test
  141. - name: Publish Unit Test Coverage
  142. uses: codecov/codecov-action@v1
  143. with:
  144. flags: unittests
  145. file: ./cover.out
  146. publish-artifacts:
  147. runs-on: ubuntu-18.04
  148. needs: detect-noop
  149. if: needs.detect-noop.outputs.noop != 'true'
  150. steps:
  151. - name: Setup QEMU
  152. uses: docker/setup-qemu-action@v1
  153. with:
  154. platforms: all
  155. - name: Setup Docker Buildx
  156. uses: docker/setup-buildx-action@v1
  157. with:
  158. version: ${{ env.DOCKER_BUILDX_VERSION }}
  159. install: true
  160. - name: Checkout
  161. uses: actions/checkout@v2
  162. - name: Fetch History
  163. run: git fetch --prune --unshallow
  164. - name: Setup Go
  165. uses: actions/setup-go@v2
  166. with:
  167. go-version: ${{ env.GO_VERSION }}
  168. - name: Find the Go Cache
  169. id: go
  170. run: |
  171. echo "::set-output name=build-cache::$(go env GOCACHE)"
  172. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  173. - name: Cache the Go Build Cache
  174. uses: actions/cache@v2.1.6
  175. with:
  176. path: ${{ steps.go.outputs.build-cache }}
  177. key: ${{ runner.os }}-build-publish-artifacts-${{ hashFiles('**/go.sum') }}
  178. restore-keys: ${{ runner.os }}-build-publish-artifacts-
  179. - name: Cache Go Dependencies
  180. uses: actions/cache@v2.1.6
  181. with:
  182. path: ${{ steps.go.outputs.mod-cache }}
  183. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  184. restore-keys: ${{ runner.os }}-pkg-
  185. - name: Build Artifacts
  186. env:
  187. # We're using docker buildx, which doesn't actually load the images it
  188. # builds by default. Specifying --load does so.
  189. BUILD_ARGS: "--load"
  190. run: make docker.build
  191. - name: Login to Docker
  192. uses: docker/login-action@v1
  193. if: env.GHCR_USERNAME != ''
  194. with:
  195. registry: ghcr.io
  196. username: ${{ secrets.GHCR_USERNAME }}
  197. password: ${{ secrets.GHCR_TOKEN }}
  198. - name: Publish Artifacts
  199. if: env.GHCR_USERNAME != ''
  200. run: make docker.push
  201. - name: Promote Artifacts to main release channel
  202. if: github.ref == 'refs/heads/main' && env.GHCR_USERNAME != ''
  203. run: make docker.promote
  204. env:
  205. RELEASE_TAG: main