|
| 1 | +name: ci |
| 2 | + |
| 3 | +'on': |
| 4 | + pull_request: {} |
| 5 | + merge_group: {} |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.merge_group.head_ref || github.ref }} |
| 9 | + cancel-in-progress: true |
| 10 | + |
| 11 | +defaults: |
| 12 | + run: |
| 13 | + shell: bash |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + |
| 18 | +jobs: |
| 19 | + changes: |
| 20 | + name: categorize changes |
| 21 | + runs-on: ubuntu-latest |
| 22 | + outputs: |
| 23 | + non-docs: ${{ steps.detect.outputs.non-docs }} |
| 24 | + yaml: ${{ steps.detect.outputs.yaml }} |
| 25 | + steps: |
| 26 | + - name: Get base depth |
| 27 | + id: base-depth |
| 28 | + run: echo "base-depth=$(expr ${{ github.event.pull_request.commits }} + 1)" >> $GITHUB_OUTPUT |
| 29 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 30 | + with: |
| 31 | + ref: ${{ github.event.pull_request.head.sha }} |
| 32 | + fetch-depth: ${{ steps.base-depth.outputs.base-depth }} |
| 33 | + persist-credentials: false |
| 34 | + - name: detect |
| 35 | + id: detect |
| 36 | + run: | |
| 37 | + git fetch origin ${GITHUB_BASE_REF} |
| 38 | + CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }} | tr ' ' '\n') |
| 39 | +
|
| 40 | + echo -e "Changed files:\n${CHANGED_FILES}" |
| 41 | +
|
| 42 | + # If no files are changed at all, then `grep -v` will match even though no change outputs |
| 43 | + # should be true. Skipping output on an empty set of changes eliminates the false positive |
| 44 | + if [[ -n "${CHANGED_FILES}" ]]; then |
| 45 | + NON_DOCS=$(echo "${CHANGED_FILES}" | grep -Eqv '\.md$' && echo 'true' || echo 'false') |
| 46 | + YAML=$(echo "${CHANGED_FILES}" | grep -Eq '\.ya?ml$' && echo 'true' || echo 'false') |
| 47 | + echo "non-docs=${NON_DOCS}" | tee -a $GITHUB_OUTPUT |
| 48 | + echo "yaml=${YAML}" | tee -a $GITHUB_OUTPUT |
| 49 | + fi |
| 50 | +
|
| 51 | + build: |
| 52 | + name: build |
| 53 | + runs-on: ubuntu-latest |
| 54 | + needs: [changes] |
| 55 | + if: ${{ needs.changes.outputs.non-docs == 'true' }} |
| 56 | + steps: |
| 57 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 58 | + with: |
| 59 | + persist-credentials: false |
| 60 | + - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 |
| 61 | + with: |
| 62 | + go-version-file: "go.mod" |
| 63 | + - name: build |
| 64 | + run: | |
| 65 | + go build -v ./... |
| 66 | + linting: |
| 67 | + name: lint |
| 68 | + runs-on: ubuntu-latest |
| 69 | + permissions: |
| 70 | + contents: read |
| 71 | + checks: write # Used by golangci-lint to annotate code in the PR |
| 72 | + needs: [changes] |
| 73 | + steps: |
| 74 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 75 | + with: |
| 76 | + fetch-depth: 0 |
| 77 | + persist-credentials: false |
| 78 | + - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 |
| 79 | + with: |
| 80 | + go-version-file: "go.mod" |
| 81 | + - name: gofmt |
| 82 | + if: ${{ needs.changes.outputs.non-docs == 'true' }} |
| 83 | + run: | |
| 84 | + gofmt_out=$(gofmt -d $(find * -name '*.go' ! -path 'vendor/*' ! -path 'third_party/*')) |
| 85 | + if [[ -n "$gofmt_out" ]]; then |
| 86 | + failed=1 |
| 87 | + fi |
| 88 | + echo "$gofmt_out" |
| 89 | + - name: golangci-lint |
| 90 | + uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0 |
| 91 | + if: ${{ needs.changes.outputs.non-docs == 'true' }} |
| 92 | + with: |
| 93 | + version: v2.7.2 |
| 94 | + args: --new-from-merge-base=origin/${{ github.base_ref }} --timeout=10m |
| 95 | + - name: yamllint |
| 96 | + if: ${{ needs.changes.outputs.yaml == 'true' }} |
| 97 | + run: | |
| 98 | + apt-get update && apt-get install -y yamllint |
| 99 | + make yamllint |
| 100 | + - name: check-license |
| 101 | + if: ${{ needs.changes.outputs.non-docs == 'true' }} |
| 102 | + run: | |
| 103 | + go install github.com/google/go-licenses@v1.0.0 |
| 104 | + go-licenses check ./... |
| 105 | + tests: |
| 106 | + needs: [build] |
| 107 | + name: test |
| 108 | + runs-on: ubuntu-latest |
| 109 | + steps: |
| 110 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 111 | + with: |
| 112 | + persist-credentials: false |
| 113 | + - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 |
| 114 | + with: |
| 115 | + go-version-file: "go.mod" |
| 116 | + - name: unit-test |
| 117 | + run: | |
| 118 | + make test-unit-verbose-and-race |
| 119 | + e2e-tests: |
| 120 | + needs: [build] |
| 121 | + uses: ./.github/workflows/kind-e2e.yaml |
| 122 | + |
| 123 | + ci-summary: |
| 124 | + name: CI summary |
| 125 | + needs: [build, linting, tests, e2e-tests] |
| 126 | + runs-on: ubuntu-latest |
| 127 | + if: always() |
| 128 | + steps: |
| 129 | + - name: Check CI results |
| 130 | + run: | |
| 131 | + results=( |
| 132 | + "build=${NEEDS_BUILD_RESULT}" |
| 133 | + "linting=${NEEDS_LINTING_RESULT}" |
| 134 | + "tests=${NEEDS_TESTS_RESULT}" |
| 135 | + "e2e-tests=${NEEDS_E2E_TESTS_RESULT}" |
| 136 | + ) |
| 137 | + failed=0 |
| 138 | + for r in "${results[@]}"; do |
| 139 | + name="${r%%=*}" |
| 140 | + result="${r#*=}" |
| 141 | + echo "${name}: ${result}" |
| 142 | + if [ "$result" != "success" ] && [ "$result" != "skipped" ]; then |
| 143 | + failed=1 |
| 144 | + fi |
| 145 | + done |
| 146 | + if [ "$failed" -eq 1 ]; then |
| 147 | + echo "" |
| 148 | + echo "Some CI jobs failed or were cancelled" |
| 149 | + exit 1 |
| 150 | + fi |
| 151 | + echo "" |
| 152 | + echo "All CI checks passed" |
| 153 | + env: |
| 154 | + NEEDS_BUILD_RESULT: ${{ needs.build.result }} |
| 155 | + NEEDS_LINTING_RESULT: ${{ needs.linting.result }} |
| 156 | + NEEDS_TESTS_RESULT: ${{ needs.tests.result }} |
| 157 | + NEEDS_E2E_TESTS_RESULT: ${{ needs.e2e-tests.result }} |
0 commit comments