|
| 1 | +name: Test |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened] |
| 6 | + push: |
| 7 | + branches-ignore: [ main ] |
| 8 | + |
| 9 | +env: |
| 10 | + GO_VERSION: '1.24' |
| 11 | + GOLANGCI_LINT_VERSION: 'v2.1' |
| 12 | + |
| 13 | +jobs: |
| 14 | + # Check if there is any dirty change for go mod tidy |
| 15 | + go-mod: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + - uses: actions/setup-go@v5 |
| 20 | + with: |
| 21 | + go-version: ${{ env.GO_VERSION }} |
| 22 | + - name: Check go mod |
| 23 | + run: | |
| 24 | + go mod tidy |
| 25 | + git diff --exit-code go.mod |
| 26 | + git diff --exit-code go.sum |
| 27 | +
|
| 28 | + go-test: |
| 29 | + needs: go-mod |
| 30 | + runs-on: ubuntu-latest |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + - uses: actions/setup-go@v5 |
| 34 | + with: |
| 35 | + go-version: ${{ env.GO_VERSION }} |
| 36 | + - run: go test ./... -race |
| 37 | + |
| 38 | + golangci-lint: |
| 39 | + needs: go-test |
| 40 | + runs-on: ubuntu-latest |
| 41 | + steps: |
| 42 | + - uses: actions/checkout@v4 |
| 43 | + - uses: actions/setup-go@v5 |
| 44 | + with: |
| 45 | + go-version: ${{ env.GO_VERSION }} |
| 46 | + - uses: golangci/golangci-lint-action@v8 |
| 47 | + with: |
| 48 | + version: ${{ env.GOLANGCI_LINT_VERSION }} |
| 49 | + |
| 50 | + detect-modules: # ref: https://github.com/golangci/golangci-lint-action/tree/main |
| 51 | + needs: [golangci-lint, go-test, go-mod] |
| 52 | + runs-on: ubuntu-latest |
| 53 | + outputs: |
| 54 | + modules: ${{ steps.set-modules.outputs.modules }} |
| 55 | + steps: |
| 56 | + - uses: actions/checkout@v4 |
| 57 | + - uses: actions/setup-go@v5 |
| 58 | + with: |
| 59 | + go-version: ${{ env.GO_VERSION }} |
| 60 | + - name: Create go.work # Required for Go workspace mode to enable submodule checks and linting support (ref: https://go.dev/doc/tutorial/workspaces) |
| 61 | + run: go work init && find . -mindepth 2 -name go.mod|sed -r 's/(.*)(go.mod)/use \1/g' >> go.work |
| 62 | + - name: Upload go.work |
| 63 | + uses: actions/upload-artifact@v4 |
| 64 | + with: |
| 65 | + name: go_work |
| 66 | + path: go.work |
| 67 | + - id: set-modules |
| 68 | + run: echo "modules=$(go list -m -json | jq -s '.' | jq -c '[.[].Dir]')" >> $GITHUB_OUTPUT |
| 69 | + |
| 70 | + go-mod-examples: |
| 71 | + needs: [detect-modules] |
| 72 | + runs-on: ubuntu-latest |
| 73 | + strategy: |
| 74 | + matrix: |
| 75 | + modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }} |
| 76 | + fail-fast: false |
| 77 | + steps: |
| 78 | + - uses: actions/checkout@v4 |
| 79 | + - uses: actions/setup-go@v5 |
| 80 | + with: |
| 81 | + go-version: ${{ env.GO_VERSION }} |
| 82 | + - name: Check go mod |
| 83 | + run: | |
| 84 | + pwd |
| 85 | + go mod tidy |
| 86 | + git diff --exit-code go.mod |
| 87 | + git diff --exit-code go.sum |
| 88 | + working-directory: ${{ matrix.modules }} |
| 89 | + |
| 90 | + go-test-examples: |
| 91 | + needs: [detect-modules, go-mod-examples] |
| 92 | + runs-on: ubuntu-latest |
| 93 | + strategy: |
| 94 | + matrix: |
| 95 | + modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }} |
| 96 | + fail-fast: false |
| 97 | + steps: |
| 98 | + - uses: actions/checkout@v4 |
| 99 | + - uses: actions/setup-go@v5 |
| 100 | + with: |
| 101 | + go-version: ${{ env.GO_VERSION }} |
| 102 | + - name: Download go.work from detect-modules |
| 103 | + uses: actions/download-artifact@v4 |
| 104 | + with: |
| 105 | + name: go_work |
| 106 | + - run: go test ./... |
| 107 | + working-directory: ${{ matrix.modules }} |
| 108 | + |
| 109 | + golangci-lint-examples: |
| 110 | + needs: [detect-modules, go-test-examples] |
| 111 | + runs-on: ubuntu-latest |
| 112 | + strategy: |
| 113 | + matrix: |
| 114 | + modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }} |
| 115 | + fail-fast: false |
| 116 | + steps: |
| 117 | + - uses: actions/checkout@v4 |
| 118 | + - uses: actions/setup-go@v5 |
| 119 | + with: |
| 120 | + go-version: ${{ env.GO_VERSION }} |
| 121 | + - name: Download go.work from detect-modules |
| 122 | + uses: actions/download-artifact@v4 |
| 123 | + with: |
| 124 | + name: go_work |
| 125 | + - name: golangci-lint ${{ matrix.modules }} |
| 126 | + uses: golangci/golangci-lint-action@v8 |
| 127 | + with: |
| 128 | + version: ${{ env.GOLANGCI_LINT_VERSION }} |
| 129 | + working-directory: ${{ matrix.modules }} |
0 commit comments