-
Notifications
You must be signed in to change notification settings - Fork 0
199 lines (157 loc) · 5.06 KB
/
check-commit.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
name: "🤖 Check: Commit"
on:
pull_request:
branches:
- main
push:
branches:
- main
permissions:
contents: write
defaults:
run:
shell: bash
jobs:
changes:
runs-on: ubuntu-latest
timeout-minutes: 4
outputs:
has_change: "${{ steps.check-non-source.outputs.any_modified == 'true' || steps.check-source.outputs.any_modified == 'true' }}"
has_source_change: "${{ steps.check-source.outputs.any_modified == 'true' }}"
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check for any non-source code changes
id: check-non-source
uses: tj-actions/changed-files@v45
with:
files: |
.github/actions/**
.github/workflows/**
go.mod
go.sum
- name: Check for any source code changes
id: check-source
uses: tj-actions/changed-files@v45
with:
files: |
**/*.go
format:
needs: ["changes"]
if: |
needs.changes.outputs.has_source_change == 'true' &&
github.actor != 'dependabot[bot]'
runs-on: ubuntu-latest
timeout-minutes: 4
steps:
- uses: actions/checkout@v4
with:
# Checkout the "head_ref" (i.e. PR branch HEAD) in case a commit is
# later needed. See https://github.com/stefanzweifel/git-auto-commit-action
# for more details.
ref: ${{ github.head_ref }}
# Use a PAT so that GitHub Actions will trigger on the resulting commit.
token: ${{ secrets.ACTIONS_BOT }}
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
- name: Check formatting of source code
id: format
continue-on-error: true
run: |
go install golang.org/x/tools/cmd/goimports@latest
test -z $(gofmt -l .)
test -z $(goimports -local github.com/coffeebeats/gdenv -l .)
- name: Fix formatting of source code
if: steps.format.outcome == 'failure'
run: |
gofmt -w .
goimports -local github.com/coffeebeats/gdenv -w .
# See https://github.com/orgs/community/discussions/26560#discussioncomment-3531273
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
git add --all '*.go'
git commit -m "chore: fix formatting (on behalf of '${{ github.triggering_actor }}')"
git push
- name: Terminate CI run early
if: steps.format.outcome == 'failure'
run: exit 1
build:
needs: ["changes"]
if: needs.changes.outputs.has_change == 'true'
runs-on: ubuntu-latest
timeout-minutes: 4
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
- name: Build source code
run: go build -v ./...
lint:
needs: ["changes", "build", "format"]
if: needs.changes.outputs.has_source_change == 'true'
runs-on: ubuntu-latest
timeout-minutes: 4
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
cache: false # See https://github.com/golangci/golangci-lint-action/issues/135.
go-version-file: "go.mod"
- name: Lint source code
uses: golangci/golangci-lint-action@v6
with:
version: latest
test:
needs: ["changes", "build"]
if: needs.changes.outputs.has_change == 'true'
runs-on: ubuntu-latest
timeout-minutes: 4
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
- name: Test source code
run: go test -race -covermode=atomic -coverprofile=coverage.out ./...
- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v5
if: needs.changes.outputs.has_source_change == 'true'
with:
fail_ci_if_error: true
flags: gdenv
token: ${{ secrets.CODECOV_TOKEN }}
# Used to ensure all branch protection requirements are met. This is a workaround until
# https://github.com/github-community/community/discussions/4324 is addressed.
branch_protection:
needs: ["format", "lint", "build", "test"]
if: ${{ always() }}
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- name: Verify 'format' status
if: |
always() &&
needs.format.result == 'failure' ||
needs.format.result == 'cancelled'
run: exit 1
- name: Verify 'lint' status
if: |
always() &&
needs.lint.result == 'failure' ||
needs.lint.result == 'cancelled'
run: exit 1
- name: Verify 'build' status
if: |
always() &&
needs.build.result == 'failure' ||
needs.build.result == 'cancelled'
run: exit 1
- name: Verify 'test' status
if: |
always() &&
needs.test.result == 'failure' ||
needs.test.result == 'cancelled'
run: exit 1