Skip to content

Commit 712a469

Browse files
committed
github actions: update to match conn
Improve checks based on what I learned in github.com/maruel/pat.
1 parent c77f5cb commit 712a469

File tree

1 file changed

+126
-75
lines changed

1 file changed

+126
-75
lines changed

.github/workflows/test.yml

Lines changed: 126 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@
33
# that can be found in the LICENSE file.
44

55
# References:
6-
# https://developer.github.com/webhooks/event-payloads/
7-
# https://github.com/actions/cache
86
# https://github.com/actions/checkout
97
# https://github.com/actions/setup-go
108
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#using-the-github_token-in-a-workflow
119
# https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions/
1210
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
11+
# https://docs.github.com/en/rest/commits/comments#create-a-commit-comment
12+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
13+
# https://docs.github.com/en/actions/learn-github-actions/contexts
1314

1415
on: [push, pull_request]
1516
name: Run tests
1617
jobs:
17-
test_all:
18+
# Runs go test both with code coverage sent to codecov, race detector and
19+
# benchmarks. At the end do a quick check to ensure the tests to not leave
20+
# files in the tree.
21+
test:
22+
name: "test: go${{matrix.gover}}.x/${{matrix.os}}"
23+
runs-on: "${{matrix.os}}"
1824
continue-on-error: true
1925
defaults:
2026
run:
@@ -25,19 +31,88 @@ jobs:
2531
os: [ubuntu-latest, macos-latest, windows-latest]
2632
# Do not forget to bump every 6 months!
2733
gover: ["1.18"]
28-
runs-on: "${{matrix.os}}"
29-
name: "go${{matrix.gover}}.x on ${{matrix.os}}"
3034
env:
3135
PYTHONDONTWRITEBYTECODE: x
3236
steps:
37+
- name: Turn off git core.autocrlf
38+
if: matrix.os == 'windows-latest'
39+
run: git config --global core.autocrlf false
40+
- uses: actions/checkout@v3
41+
with:
42+
fetch-depth: 2
3343
- uses: actions/setup-go@v3
3444
with:
35-
go-version: "${{matrix.gover}}"
45+
go-version: "~${{matrix.gover}}.0"
46+
cache: true
47+
- name: 'go install necessary tools'
48+
if: always()
49+
run: |
50+
go install github.com/maruel/pat/cmd/ba@latest
51+
- name: 'Check: go test -cover'
52+
if: always()
53+
run: go test -timeout=120s -covermode=count -coverprofile coverage.txt -bench=. -benchtime=1x ./...
54+
# Don't send code coverage if anything failed to reduce spam.
55+
- uses: codecov/codecov-action@v2
56+
- name: 'Cleanup'
57+
if: always()
58+
run: rm coverage.txt
59+
- name: 'Check: go test -race'
60+
run: go test -timeout=120s -race -bench=. -benchtime=1x ./...
61+
- name: 'Check: benchmark 📈'
62+
run: ba -against HEAD~1
63+
- name: 'Check: go test -short (CGO_ENABLED=0)'
64+
env:
65+
CGO_ENABLED: 0
66+
run: go test -timeout=120s -short -bench=. -benchtime=1x ./...
67+
- name: 'Check: go test -short (32 bits)'
68+
if: matrix.os != 'macos-latest'
69+
env:
70+
GOARCH: 386
71+
run: go test -timeout=120s -short -bench=. -benchtime=1x ./...
72+
- name: "Check: tree is clean"
73+
if: always()
74+
run: |
75+
# Nothing should have changed in the tree up to that point and no
76+
# unsuspected file was created.
77+
TOUCHED=$(git status --porcelain --ignored)
78+
if ! test -z "$TOUCHED"; then
79+
echo "Oops, something touched these files, please cleanup:"
80+
echo "$TOUCHED"
81+
git diff
82+
false
83+
fi
3684
37-
# Checkout and print debugging information.
85+
86+
# Run linters. This workflow can be merged with the test_all one if desired
87+
# to cut on runtime, at the cost of latency. I dislike waiting for results
88+
# so I prefer to run them in parallel.
89+
lint:
90+
name: "lint: go${{matrix.gover}}.x/${{matrix.os}}"
91+
runs-on: "${{matrix.os}}"
92+
continue-on-error: true
93+
defaults:
94+
run:
95+
shell: bash
96+
strategy:
97+
fail-fast: false
98+
matrix:
99+
# You may want to run only on linux to save on cost. Projects with
100+
# OS-specific code benefits from explicitly linting on macOS and
101+
# Windows.
102+
os: [ubuntu-latest, macos-latest, windows-latest]
103+
# Do not forget to bump every 6 months!
104+
gover: ["1.18"]
105+
env:
106+
PYTHONDONTWRITEBYTECODE: x
107+
steps:
38108
- name: Turn off git core.autocrlf
109+
if: matrix.os == 'windows-latest'
39110
run: git config --global core.autocrlf false
40111
- uses: actions/checkout@v3
112+
- uses: actions/setup-go@v3
113+
with:
114+
go-version: "~${{matrix.gover}}.0"
115+
cache: true
41116
- name: "Debug"
42117
run: |
43118
echo HOME = $HOME
@@ -46,14 +121,6 @@ jobs:
46121
echo ""
47122
echo $ ls -l $HOME/go/bin
48123
ls -la $HOME/go/bin
49-
50-
- name: 'Cache: ~/go'
51-
uses: actions/cache@v2
52-
with:
53-
path: ~/go
54-
key: "${{runner.os}}-gopkg-${{hashFiles('go.sum', '.github/workflows/*.yml')}}"
55-
56-
# Fetch the tools.
57124
- name: 'go install necessary tools'
58125
if: always()
59126
run: |
@@ -66,12 +133,11 @@ jobs:
66133
run: |
67134
go install github.com/client9/misspell/cmd/misspell@latest
68135
go install github.com/google/addlicense@latest
69-
70-
# Now run proper checks.
71136
- name: 'Check: go vet'
72137
if: always()
73-
run: go vet ./...
138+
run: go vet -unsafeptr=false ./...
74139
- name: 'Check: go vet shadow; shadowed variables'
140+
if: always()
75141
run: |
76142
SHADOW_TOOL="$(which shadow)"
77143
if [ -f "${SHADOW_TOOL}.exe" ]; then
@@ -88,8 +154,8 @@ jobs:
88154
# SA5002: loop condition never changes or has a race condition
89155
run: staticcheck -checks inherit,-U1000,-SA5002,-SA1019 ./...
90156
- name: 'Check: gosec'
157+
if: always()
91158
run: gosec -fmt=golint -quiet -exclude=G103,G304,G404 ./...
92-
93159
# The following checks are not dependent on the OS or go build tags. Only
94160
# run them on ubuntu-latest since it's the fastest one.
95161
- name: 'Check: no executable was committed (ubuntu)'
@@ -99,6 +165,9 @@ jobs:
99165
echo 'Do not commit executables beside shell scripts'
100166
false
101167
fi
168+
- name: 'Check: addlicense; all sources have a license header (ubuntu)'
169+
if: always() && matrix.os == 'ubuntu-latest'
170+
run: addlicense -check .
102171
- name: 'Check: gofmt; code is well formatted (ubuntu)'
103172
if: always() && matrix.os == 'ubuntu-latest'
104173
run: |
@@ -116,9 +185,6 @@ jobs:
116185
echo "" >> _comments.txt
117186
false
118187
fi
119-
- name: 'Check: addlicense; all sources have a license header (ubuntu)'
120-
if: always() && matrix.os == 'ubuntu-latest'
121-
run: addlicense -check .
122188
- name: "Check: misspelling; code doesn't contain misspelling (ubuntu)"
123189
if: always() && matrix.os == 'ubuntu-latest'
124190
run: |
@@ -131,49 +197,34 @@ jobs:
131197
echo "" >> _comments.txt
132198
false
133199
fi
134-
135-
# Run tests last since it's potentially the slowest step.
136-
- name: 'Check: go test -cover'
137-
if: always()
138-
run: go test -timeout=40s -covermode=count -coverprofile coverage.txt ./...
139-
# Don't send code coverage if anything failed to reduce spam.
140-
- uses: codecov/codecov-action@v2
141-
- name: 'Cleanup'
142-
if: always()
143-
run: rm coverage.txt
144-
# Don't run go test -race if anything failed, to speed up the results.
145-
- name: 'Check: go test -race'
146-
run: go test -timeout=60s -race ./...
147-
- name: 'Check: go test -bench=.'
148-
run: go test -timeout=40s -bench=. -benchtime=100ms -cpu=1 -run NONE ./...
149-
- name: 'Check: CGO_ENABLED=0 go test -short'
150-
run: CGO_ENABLED=0 go test -timeout=40s -short ./...
151-
152-
- name: "Check: tree is clean"
153-
if: always()
200+
- name: 'Send comments'
201+
if: failure()
154202
run: |
155-
# Nothing should have changed in the tree up to that point and no
156-
# unsuspected file was created.
157-
TOUCHED=$(git status --porcelain --ignored)
158-
if ! test -z "$TOUCHED"; then
159-
echo "Oops, something touched these files, please cleanup:"
160-
echo "$TOUCHED"
161-
git diff
162-
false
203+
if [ -f ../_comments.txt ]; then
204+
URL="${{github.event.issue.pull_request.url}}"
205+
if test -z "$URL"; then
206+
URL="${{github.api_url}}/repos/${{github.repository}}/commits/${{github.sha}}/comments"
207+
fi
208+
echo "Sending $(cat ../_comments.txt|wc -l) lines of comments to ${URL}"
209+
curl -sS --request POST \
210+
--header "Authorization: Bearer ${{secrets.GITHUB_TOKEN}}" \
211+
--header "Content-Type: application/json" \
212+
--data "$(cat ../_comments.txt | jq -R --slurp '{body: .}')" \
213+
"${URL}" > /dev/null
214+
rm ../_comments.txt
163215
fi
164-
165216
- name: "Check: go generate doesn't modify files"
166217
if: always()
167218
run: |
168219
go generate ./...
169-
# Also test for untracked files.
220+
# Also test for untracked files. go generate should not generate ignored
221+
# files either.
170222
TOUCHED=$(git status --porcelain --ignored)
171223
if ! test -z "$TOUCHED"; then
172224
echo "go generate created these files, please fix:"
173225
echo "$TOUCHED"
174226
false
175227
fi
176-
177228
- name: "Check: go mod tidy doesn't modify files"
178229
if: always()
179230
run: |
@@ -205,52 +256,52 @@ jobs:
205256
go get -t ./...
206257
go test -short ./...
207258
208-
- name: 'Send comments'
209-
if: failure() && github.event_name == 'pull_request'
210-
run: |
211-
if [ -f _comments.txt ]; then
212-
URL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
213-
echo "Sending $(cat _comments.txt|wc -l) lines of comments to ${URL}"
214-
PAYLOAD=$(echo '{}' | jq --arg body "$(cat _comments.txt)" '.body = $body')
215-
curl -sS --request POST \
216-
--header "Authorization: Bearer ${{secrets.GITHUB_TOKEN}}" \
217-
--header "Content-Type: application/json" \
218-
--data "${PAYLOAD}" "${URL}" > /dev/null
219-
fi
220259
221-
test_short:
260+
# Ensure tests pass on oldest supported Go version.
261+
old:
262+
name: "test: go${{matrix.gover}}/${{matrix.os}}"
263+
runs-on: "${{matrix.os}}"
222264
continue-on-error: true
265+
defaults:
266+
run:
267+
shell: bash
223268
strategy:
224269
fail-fast: false
225270
matrix:
226271
os: [ubuntu-latest]
227272
gover: ['1.13.15']
228-
runs-on: "${{matrix.os}}"
229-
name: "go${{matrix.gover}} on ${{matrix.os}} (quick)"
273+
env:
274+
PYTHONDONTWRITEBYTECODE: x
230275
steps:
276+
- name: Turn off git core.autocrlf
277+
if: matrix.os == 'windows-latest'
278+
run: git config --global core.autocrlf false
279+
- uses: actions/checkout@v3
231280
- uses: actions/setup-go@v3
232281
with:
233-
go-version: "${{matrix.gover}}"
234-
- uses: actions/checkout@v3
282+
go-version: "=${{matrix.gover}}"
235283
- name: 'Check: go test'
236-
run: go test -timeout=40s ./...
284+
run: go test -timeout=120s -bench=. -benchtime=1x ./...
285+
237286

238287
codeql:
288+
name: "codeql: go${{matrix.gover}}.x/${{matrix.os}}"
289+
runs-on: "${{matrix.os}}"
239290
continue-on-error: true
240291
strategy:
241292
fail-fast: false
242293
matrix:
294+
os: [ubuntu-latest]
243295
# Do not forget to bump every 6 months!
244296
gover: ["1.18"]
245297
permissions:
246298
security-events: write
247-
runs-on: ubuntu-latest
248-
name: "go${{matrix.gover}}.x on ubuntu-latest (codeql)"
249299
steps:
300+
- uses: actions/checkout@v3
250301
- uses: actions/setup-go@v3
251302
with:
252-
go-version: "${{matrix.gover}}"
253-
- uses: actions/checkout@v3
303+
go-version: "~${{matrix.gover}}.0"
304+
cache: true
254305
- name: Initialize CodeQL
255306
uses: github/codeql-action/init@v2
256307
with:

0 commit comments

Comments
 (0)