Skip to content

Commit 7d7bd22

Browse files
authored
refactor: update go-validate workflow (#442)
Simplifies the workflow by removing the separate get-go-version job and using go.mod for Go version management. Adds concurrency control, improves go mod tidy and gofmt checks with clearer error messages, and standardizes job names and steps for better maintainability. Signed-off-by: Ryan Johnson <ryan.johnson@broadcom.com>
1 parent 23d7f05 commit 7d7bd22

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

.github/workflows/go-validate.yml

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,92 +11,97 @@ on:
1111
branches:
1212
- main
1313

14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.ref }}
16+
cancel-in-progress: true
17+
1418
permissions:
1519
contents: read
1620

1721
jobs:
18-
get-go-version:
19-
runs-on: ubuntu-latest
20-
outputs:
21-
go-version: ${{ steps.get-go-version.outputs.go-version }}
22-
steps:
23-
- name: Checkout Repository
24-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
25-
- name: Get Go Version
26-
id: get-go-version
27-
run: |
28-
echo "Found Go $(cat .go-version)"
29-
echo "go-version=$(cat .go-version)" >> $GITHUB_OUTPUT
3022
check-mod-tidy:
31-
needs:
32-
- get-go-version
3323
runs-on: ubuntu-latest
34-
name: Go Mod Tidy
24+
name: Go Mod Tidy Check
3525
steps:
3626
- name: Checkout Repository
3727
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
3828
- name: Setup Go
3929
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
4030
with:
41-
go-version: ${{ needs.get-go-version.outputs.go-version }}
42-
- run: go mod tidy
31+
go-version-file: go.mod
32+
cache: true
33+
- name: Run go mod tidy
34+
run: go mod tidy
35+
- name: Verify go.mod/go.sum are tidy
36+
shell: bash
37+
run: |
38+
if ! git diff --exit-code; then
39+
echo "go.mod/go.sum are not tidy."
40+
echo "Run: go mod tidy"
41+
exit 1
42+
fi
43+
4344
check-lint:
44-
needs:
45-
- get-go-version
4645
runs-on: ubuntu-latest
47-
name: Lint Check
46+
name: Go Lint Check
4847
steps:
4948
- name: Checkout Repository
5049
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
5150
- name: Setup Go
5251
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
5352
with:
54-
go-version: ${{ needs.get-go-version.outputs.go-version }}
55-
- uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0
53+
go-version-file: go.mod
54+
cache: true
55+
- name: Run golangci-lint
56+
uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0
5657
with:
5758
version: latest
5859
only-new-issues: true
60+
5961
check-fmt:
60-
needs:
61-
- get-go-version
6262
runs-on: ubuntu-latest
63-
name: Gofmt Check
63+
name: Go Format Check
6464
steps:
6565
- name: Checkout Repository
6666
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
6767
- name: Setup Go
6868
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
6969
with:
70-
go-version: ${{ needs.get-go-version.outputs.go-version }}
71-
- run: |
72-
go fmt ./...
73-
echo "==> Checking that code complies with go fmt requirements..."
74-
git diff --exit-code; if [ $$? -eq 1 ]; then \
75-
echo "Found files that are not fmt'ed."; \
76-
echo "You can use the command: \`go fmt ./...\` to reformat code."; \
77-
exit 1; \
70+
go-version-file: go.mod
71+
cache: true
72+
- name: Run gofmt
73+
shell: bash
74+
run: |
75+
files="$(gofmt -l .)"
76+
if [[ -n "$files" ]]; then
77+
echo "Found files that are not gofmt'ed:"
78+
echo "$files"
79+
echo
80+
echo "Run: gofmt -w ."
81+
exit 1
7882
fi
83+
7984
check-generate:
80-
needs:
81-
- get-go-version
8285
runs-on: ubuntu-latest
83-
name: Generate Check
86+
name: Go Generate Check
8487
steps:
8588
- name: Checkout Repository
8689
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
8790
- name: Setup Go
8891
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
8992
with:
90-
go-version: ${{ needs.get-go-version.outputs.go-version }}
91-
- run: |
93+
go-version-file: go.mod
94+
cache: true
95+
- name: Run make generate
96+
run: |
9297
export PATH=$PATH:$(go env GOPATH)/bin
9398
make generate
9499
uncommitted="$(git status -s)"
95100
if [[ -z "$uncommitted" ]]; then
96101
echo "OK"
97102
else
98-
echo "Docs have been updated, but the compiled docs have not been committed."
99-
echo "Run 'make generate', and commit the result to resolve this error."
103+
echo "Generated files have changed but are not committed."
104+
echo "Run: make generate"
100105
echo "Generated but uncommitted files:"
101106
echo "$uncommitted"
102107
exit 1

0 commit comments

Comments
 (0)