ci.yml 7.5 KB

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