|
1 | 1 | name: go test |
2 | 2 |
|
3 | 3 | permissions: |
4 | | - contents: read |
5 | 4 | pull-requests: read |
| 5 | + contents: read |
6 | 6 |
|
7 | 7 | on: |
8 | 8 | push: |
9 | | - tags: |
10 | | - - v* |
11 | 9 | branches: |
12 | 10 | - master |
13 | 11 |
|
14 | 12 | pull_request: |
15 | 13 |
|
16 | 14 | jobs: |
17 | | - lint: |
18 | | - name: Go lint mono-repo |
19 | | - runs-on: ubuntu-latest |
20 | | - steps: |
21 | | - - |
22 | | - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 |
23 | | - with: |
24 | | - fetch-depth: '0' |
25 | | - - |
26 | | - uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0 |
27 | | - with: |
28 | | - go-version: stable |
29 | | - check-latest: true |
30 | | - cache: true |
31 | | - cache-dependency-path: '**/go.sum' |
32 | | - - |
33 | | - name: Install golangci-lint |
34 | | - uses: golangci/golangci-lint-action@0a35821d5c230e903fcfe077583637dea1b27b47 # v9.0.0 |
35 | | - with: |
36 | | - version: latest |
37 | | - skip-cache: true |
38 | | - install-only: true |
39 | | - - |
40 | | - name: Lint multiple modules |
41 | | - # golangci-lint doesn't support go.work to lint multiple modules in one single pass |
42 | | - run: | |
43 | | - set -euxo pipefail |
44 | | -
|
45 | | - git fetch origin master |
46 | | - git show --no-patch --oneline origin/master |
47 | | -
|
48 | | - while read module_location ; do |
49 | | - pushd "${module_location}" |
50 | | - golangci-lint run --new-from-rev origin/master |
51 | | - popd |
52 | | - done < <(go list -f '{{.Dir}}' -m) |
53 | | -
|
54 | | - module-test: |
55 | | - name: Unit tests |
56 | | - runs-on: ${{ matrix.os }} |
57 | | - needs: [ lint ] |
58 | | - |
59 | | - strategy: |
60 | | - matrix: |
61 | | - os: [ ubuntu-latest, macos-latest, windows-latest ] |
62 | | - go_version: ['oldstable', 'stable' ] |
63 | | - env: |
64 | | - TEST_REPORT: 'all_modules.report.${{ matrix.os }}.${{ matrix.go_version }}.json' |
65 | | - |
66 | | - steps: |
67 | | - - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 |
68 | | - - uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0 |
69 | | - with: |
70 | | - go-version: '${{ matrix.go_version }}' |
71 | | - check-latest: true |
72 | | - cache: true |
73 | | - cache-dependency-path: '**/go.sum' |
74 | | - |
75 | | - - name: Ensure TMP is created on windows runners |
76 | | - # On windows, tests require testing.TempDir to reside on the same drive as the code. |
77 | | - # TMP is used by os.TempDir() to determine the location of temporary files. |
78 | | - if: ${{ runner.os == 'Windows' }} |
79 | | - shell: bash |
80 | | - run: | |
81 | | - TMP="${{ github.workspace }}\..\tmp" |
82 | | - mkdir -p ${TMP} |
83 | | - echo "TMP=${TMP}" >> "${GITHUB_ENV}" |
84 | | -
|
85 | | - - name: Run unit tests on all modules in this repo |
86 | | - shell: bash |
87 | | - env: |
88 | | - # *.coverage.* pattern is automatically detected by codecov |
89 | | - COVER_PROFILE: 'all_modules.coverage.${{ matrix.os }}.${{ matrix.go_version }}.out' |
90 | | - run: | |
91 | | - # when go1.25 becomes the oldstable, we may replace this bash with "go test work" |
92 | | - declare -a ALL_MODULES |
93 | | - BASH_MAJOR=$(echo $BASH_VERSION|cut -d'.' -f1) |
94 | | - if [[ "${BASH_MAJOR}" -ge 4 ]] ; then |
95 | | - mapfile ALL_MODULES < <(go list -f '{{.Dir}}/...' -m) |
96 | | - else |
97 | | - # for older bash versions, e.g. on macOS runner. This fallback will eventually disappear. |
98 | | - while read line ; do |
99 | | - ALL_MODULES+=("${line}") |
100 | | - done < <(go list -f '{{.Dir}}/...' -m) |
101 | | - fi |
102 | | - echo "::notice title=Modules found::${ALL_MODULES[@]}" |
103 | | -
|
104 | | - # with go.work file enabled, go test recognizes sub-modules and collects all packages to be covered |
105 | | - # without specifying -coverpkg. |
106 | | - go test -race -coverprofile="${COVER_PROFILE}" -covermode=atomic -json ${ALL_MODULES[@]}|tee -a "${TEST_REPORT}" |
107 | | -
|
108 | | - - name: Upload coverage to codecov |
109 | | - if: ${{ success() }} # we do this only if all previous steps succeeded |
110 | | - uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1 |
111 | | - with: |
112 | | - name: Multi modules aggregated coverage |
113 | | - flags: '${{ matrix.go_version }}-${{ matrix.os }}' |
114 | | - fail_ci_if_error: false |
115 | | - verbose: false |
116 | | - |
117 | | - - name: Upload JSON test Results |
118 | | - if: always() |
119 | | - uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0 |
120 | | - with: |
121 | | - name: 'all_modules.report.${{ matrix.os }}.${{ matrix.go_version }}' |
122 | | - path: ${{ env.TEST_REPORT }} |
123 | | - |
124 | 15 | test: |
125 | | - needs: [ module-test ] |
126 | | - name: Test |
127 | | - runs-on: ubuntu-latest |
128 | | - steps: |
129 | | - - name: Tests complete |
130 | | - run: | |
131 | | - echo "::notice title=Success::All tests completed" |
132 | | -
|
133 | | - collect-reports: |
134 | | - if: always() |
135 | | - needs: [ module-test ] |
136 | | - name: Collect and merge test reports |
137 | | - runs-on: ubuntu-latest |
138 | | - steps: |
139 | | - - uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0 |
140 | | - with: |
141 | | - go-version: stable |
142 | | - check-latest: true |
143 | | - cache: true |
144 | | - |
145 | | - - name: Download all JSON artifacts |
146 | | - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0 |
147 | | - with: |
148 | | - run-id: "${{ github.run_id }}" |
149 | | - pattern: "all_modules.report.*" |
150 | | - # artifacts resolve as folders |
151 | | - path: reports/ |
152 | | - |
153 | | - - name: Convert test reports to a merged JUnit XML |
154 | | - # NOTE: codecov test reports only support JUnit format at this moment. See https://docs.codecov.com/docs/test-analytics. |
155 | | - # Ideally, codecov improve a bit their platform, so we may only need a single pass to CTRF format. |
156 | | - # |
157 | | - # As a contemplated alternative, we could use gotestsum above to produce the JUnit XML directly. |
158 | | - run: | |
159 | | - go install github.com/jstemmer/go-junit-report/v2@latest |
160 | | - cat reports/*/*.json | go-junit-report -parser gojson -out=reports/junit_report.xml |
161 | | -
|
162 | | - - name: Upload test results to Codecov |
163 | | - if: always() |
164 | | - uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1 |
165 | | - with: |
166 | | - files: '**/junit_report.xml' |
167 | | - report_type: 'test_results' |
168 | | - fail_ci_if_error: false |
169 | | - handle_no_reports_found: true |
170 | | - verbose: true |
171 | | - |
172 | | - - name: Convert test reports to CTRF JSON |
173 | | - run: | |
174 | | - go install github.com/ctrf-io/go-ctrf-json-reporter/cmd/go-ctrf-json-reporter@v0.0.10 |
175 | | -
|
176 | | - appName="swag" |
177 | | - buildNumber="${{ github.run_id }}" |
178 | | - appVersion="${{ github.event.pull_request.head.sha }}" |
179 | | -
|
180 | | - while read report ; do |
181 | | - echo "::notice::converting report: ${report}" |
182 | | - #TEST_REPORT: 'all_modules.report.${{ matrix.os }}.${{ matrix.go_version }}.json' |
183 | | - reformated=$(echo "${report##*/}"|sed -E 's/(go)([[:digit:]]+)\.([[:digit:]]+)/\1\2\3/') # e.g. go1.24 becomes go124 |
184 | | - mapfile -d'.' -t -s 2 -n 2 split < <(echo $reformated) # skip the first 2 parts, stop on 2 more parts |
185 | | - osPlatform="${split[0]}" |
186 | | - osRelease="${split[1]}" |
187 | | -
|
188 | | - go-ctrf-json-reporter \ |
189 | | - -verbose \ |
190 | | - -appName "${appName}" \ |
191 | | - -appVersion "${appVersion}" \ |
192 | | - -buildNumber "${buildNumber}" \ |
193 | | - -osPlatform "${osPlatform}" \ |
194 | | - -osRelease "${osRelease}" \ |
195 | | - -output "./reports/ctrf_report_${osPlatform}_${osRelease}.json" \ |
196 | | - -quiet < "${report}" |
197 | | - done < <(find reports -name \*.json) |
198 | | -
|
199 | | - # NOTE: at this moment, we don't upload CTRF reports as artifacts. |
200 | | - # Some of the CTRF reports are therefore not available (flaky tests, history, ...). |
201 | | - # |
202 | | - # See https://github.com/ctrf-io/github-test-reporter?tab=readme-ov-file#report-showcase |
203 | | - # for more reporting possibilities. At the moment, we keep it simple, as most advanced features |
204 | | - # require a github token (thus adding the complexity of a separate workflow starting on pull_request_target). |
205 | | - # |
206 | | - # For the moment, we are contented with these simple reports. This is an opportunity to compare the insight they |
207 | | - # provide as compared to what is uploaded to codecov. |
208 | | - # |
209 | | - # Codecov analytics are pretty poor at this moment. On the other hand, they manage the bot that pushes back |
210 | | - # PR comments. |
211 | | - # |
212 | | - # They also handle the storage of past test reports, so as to assess flaky tests. |
213 | | - - name: Publish Test Summary Results |
214 | | - uses: ctrf-io/github-test-reporter@024bc4b64d997ca9da86833c6b9548c55c620e40 # v1.0.26 |
215 | | - with: |
216 | | - report-path: 'reports/ctrf_report_*.json' |
217 | | - use-suite-name: true |
218 | | - summary-report: true # post a report to the github actions summary |
219 | | - github-report: true |
220 | | - failed-folded-report: true |
221 | | - |
| 16 | + uses: go-openapi/ci-workflows/.github/workflows/go-test-monorepo.yml@e77a5bc724d0ab14dd086ee6e13153129ddfe3f9 # v0.2.2 |
| 17 | + secrets: inherit |
0 commit comments