Skip to content

Commit e5b2bf7

Browse files
committed
Add multi-module support to Go templates
The templates for testing, linting, and formatting Go code work from a list of packages of the Go module which is output from a `go list` command. Previously, it was only possible to get the packages of the module in the root of the repository. Projects may contain multiple Go modules in subfolders of the repository. In order to support checks on these modules, it's necessary to configure the commands to run from their path. This is passed to the task via the `GO_MODULE_PATH` environment variable. If this variable is not defined, the default root module path is used as default, preserving the previous task behavior. The workflows use a job matrix to allow easy configuration for any number of module paths and a dedicated parallel job for each module. Because it may not be desirable to combine the code coverage data for all modules, a Codecov flag value can be defined for each matrix job in the "Test Go" workflow. This workflow produces a two dimensional "operating system * module" job matrix, running the tests for each module using all operating systems.
1 parent 0356dbf commit e5b2bf7

File tree

9 files changed

+145
-21
lines changed

9 files changed

+145
-21
lines changed

workflow-templates/assets/check-go-task/Taskfile.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ tasks:
1212
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
1313
go:vet:
1414
desc: Check for errors in Go code
15+
dir: '{{default "./" .GO_MODULE_PATH}}'
1516
cmds:
1617
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
1718

1819
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
1920
go:fix:
2021
desc: Modernize usages of outdated APIs
22+
dir: '{{default "./" .GO_MODULE_PATH}}'
2123
cmds:
2224
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
2325

2426
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
2527
go:lint:
2628
desc: Lint Go code
29+
dir: '{{default "./" .GO_MODULE_PATH}}'
2730
cmds:
2831
- |
2932
if ! which golint &>/dev/null; then
@@ -38,5 +41,6 @@ tasks:
3841
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
3942
go:format:
4043
desc: Format Go code
44+
dir: '{{default "./" .GO_MODULE_PATH}}'
4145
cmds:
4246
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

workflow-templates/assets/go-task/Taskfile.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ version: "3"
33

44
vars:
55
DEFAULT_GO_PACKAGES:
6-
sh: echo $(go list ./... | tr '\n' ' ')
6+
sh: |
7+
echo $(cd {{default "./" .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"')
78
LDFLAGS:
89

910
tasks:

workflow-templates/assets/test-go-task/Taskfile.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ tasks:
55
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-go-task/Taskfile.yml
66
go:test:
77
desc: Run unit tests
8+
dir: '{{default "./" .GO_MODULE_PATH}}'
89
cmds:
910
- |
1011
go test \

workflow-templates/check-go-task.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ Install the [`check-go-task.yml`](check-go-task.yml) GitHub Actions workflow to
1919

2020
Configure the version of Go used for development of the project in the `env.GO_VERSION` field of `check-go-task.yml`.
2121

22+
If the project contains Go modules in paths other than the root of the repository, add their paths to the [job matrices](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix) of `check-go-task.yml` at:
23+
24+
- `jobs.check-errors.strategy.matrix.module[].path`
25+
- `jobs.check-outdated.strategy.matrix.module[].path`
26+
- `jobs.check-style.strategy.matrix.module[].path`
27+
- `jobs.check-formatting.strategy.matrix.module[].path`
28+
- `jobs.check-config.strategy.matrix.module[].path`
29+
2230
### Readme badge
2331

2432
Markdown badge:

workflow-templates/check-go-task.yml

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@ on:
1111
paths:
1212
- ".github/workflows/check-go-task.ya?ml"
1313
- "Taskfile.ya?ml"
14-
- "go.mod"
15-
- "go.sum"
14+
- "**/go.mod"
15+
- "**/go.sum"
1616
- "**.go"
1717
pull_request:
1818
paths:
1919
- ".github/workflows/check-go-task.ya?ml"
2020
- "Taskfile.ya?ml"
21-
- "go.mod"
22-
- "go.sum"
21+
- "**/go.mod"
22+
- "**/go.sum"
2323
- "**.go"
2424
workflow_dispatch:
2525
repository_dispatch:
2626

2727
jobs:
2828
check-errors:
29+
name: check-errors (${{ matrix.module.path }})
2930
runs-on: ubuntu-latest
3031

32+
strategy:
33+
fail-fast: false
34+
35+
matrix:
36+
module:
37+
# TODO: add paths of all Go modules here
38+
- path: ./
39+
3140
steps:
3241
- name: Checkout repository
3342
uses: actions/checkout@v2
@@ -44,11 +53,22 @@ jobs:
4453
version: 3.x
4554

4655
- name: Check for errors
56+
env:
57+
GO_MODULE_PATH: ${{ matrix.module.path }}
4758
run: task go:vet
4859

4960
check-outdated:
61+
name: check-outdated (${{ matrix.module.path }})
5062
runs-on: ubuntu-latest
5163

64+
strategy:
65+
fail-fast: false
66+
67+
matrix:
68+
module:
69+
# TODO: add paths of all Go modules here
70+
- path: ./
71+
5272
steps:
5373
- name: Checkout repository
5474
uses: actions/checkout@v2
@@ -65,14 +85,25 @@ jobs:
6585
version: 3.x
6686

6787
- name: Modernize usages of outdated APIs
88+
env:
89+
GO_MODULE_PATH: ${{ matrix.module.path }}
6890
run: task go:fix
6991

7092
- name: Check if any fixes were needed
7193
run: git diff --color --exit-code
7294

7395
check-style:
96+
name: check-style (${{ matrix.module.path }})
7497
runs-on: ubuntu-latest
7598

99+
strategy:
100+
fail-fast: false
101+
102+
matrix:
103+
module:
104+
# TODO: add paths of all Go modules here
105+
- path: ./
106+
76107
steps:
77108
- name: Checkout repository
78109
uses: actions/checkout@v2
@@ -92,11 +123,22 @@ jobs:
92123
run: go install golang.org/x/lint/golint@latest
93124

94125
- name: Check style
126+
env:
127+
GO_MODULE_PATH: ${{ matrix.module.path }}
95128
run: task --silent go:lint
96129

97130
check-formatting:
131+
name: check-formatting (${{ matrix.module.path }})
98132
runs-on: ubuntu-latest
99133

134+
strategy:
135+
fail-fast: false
136+
137+
matrix:
138+
module:
139+
# TODO: add paths of all Go modules here
140+
- path: ./
141+
100142
steps:
101143
- name: Checkout repository
102144
uses: actions/checkout@v2
@@ -113,6 +155,8 @@ jobs:
113155
version: 3.x
114156

115157
- name: Format code
158+
env:
159+
GO_MODULE_PATH: ${{ matrix.module.path }}
116160
run: task go:format
117161

118162
- name: Check formatting

workflow-templates/dependabot/workflow-template-copies/.github/workflows/check-go-task.yml

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@ on:
1111
paths:
1212
- ".github/workflows/check-go-task.ya?ml"
1313
- "Taskfile.ya?ml"
14-
- "go.mod"
15-
- "go.sum"
14+
- "**/go.mod"
15+
- "**/go.sum"
1616
- "**.go"
1717
pull_request:
1818
paths:
1919
- ".github/workflows/check-go-task.ya?ml"
2020
- "Taskfile.ya?ml"
21-
- "go.mod"
22-
- "go.sum"
21+
- "**/go.mod"
22+
- "**/go.sum"
2323
- "**.go"
2424
workflow_dispatch:
2525
repository_dispatch:
2626

2727
jobs:
2828
check-errors:
29+
name: check-errors (${{ matrix.module.path }})
2930
runs-on: ubuntu-latest
3031

32+
strategy:
33+
fail-fast: false
34+
35+
matrix:
36+
module:
37+
# TODO: add paths of all Go modules here
38+
- path: ./
39+
3140
steps:
3241
- name: Checkout repository
3342
uses: actions/checkout@v2
@@ -44,11 +53,22 @@ jobs:
4453
version: 3.x
4554

4655
- name: Check for errors
56+
env:
57+
GO_MODULE_PATH: ${{ matrix.module.path }}
4758
run: task go:vet
4859

4960
check-outdated:
61+
name: check-outdated (${{ matrix.module.path }})
5062
runs-on: ubuntu-latest
5163

64+
strategy:
65+
fail-fast: false
66+
67+
matrix:
68+
module:
69+
# TODO: add paths of all Go modules here
70+
- path: ./
71+
5272
steps:
5373
- name: Checkout repository
5474
uses: actions/checkout@v2
@@ -65,14 +85,25 @@ jobs:
6585
version: 3.x
6686

6787
- name: Modernize usages of outdated APIs
88+
env:
89+
GO_MODULE_PATH: ${{ matrix.module.path }}
6890
run: task go:fix
6991

7092
- name: Check if any fixes were needed
7193
run: git diff --color --exit-code
7294

7395
check-style:
96+
name: check-style (${{ matrix.module.path }})
7497
runs-on: ubuntu-latest
7598

99+
strategy:
100+
fail-fast: false
101+
102+
matrix:
103+
module:
104+
# TODO: add paths of all Go modules here
105+
- path: ./
106+
76107
steps:
77108
- name: Checkout repository
78109
uses: actions/checkout@v2
@@ -92,11 +123,22 @@ jobs:
92123
run: go install golang.org/x/lint/golint@latest
93124

94125
- name: Check style
126+
env:
127+
GO_MODULE_PATH: ${{ matrix.module.path }}
95128
run: task --silent go:lint
96129

97130
check-formatting:
131+
name: check-formatting (${{ matrix.module.path }})
98132
runs-on: ubuntu-latest
99133

134+
strategy:
135+
fail-fast: false
136+
137+
matrix:
138+
module:
139+
# TODO: add paths of all Go modules here
140+
- path: ./
141+
100142
steps:
101143
- name: Checkout repository
102144
uses: actions/checkout@v2
@@ -113,6 +155,8 @@ jobs:
113155
version: 3.x
114156

115157
- name: Format code
158+
env:
159+
GO_MODULE_PATH: ${{ matrix.module.path }}
116160
run: task go:format
117161

118162
- name: Check formatting

workflow-templates/dependabot/workflow-template-copies/.github/workflows/test-go-task.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ on:
1111
paths:
1212
- ".github/workflows/test-go-task.ya?ml"
1313
- "codecov.ya?ml"
14-
- "go.mod"
15-
- "go.sum"
14+
- "**/go.mod"
15+
- "**/go.sum"
1616
- "Taskfile.ya?ml"
1717
- "**.go"
1818
- "**/testdata/**"
1919
pull_request:
2020
paths:
2121
- ".github/workflows/test-go-task.ya?ml"
2222
- "codecov.ya?ml"
23-
- "go.mod"
24-
- "go.sum"
23+
- "**/go.mod"
24+
- "**/go.sum"
2525
- "Taskfile.ya?ml"
2626
- "**.go"
2727
- "**/testdata/**"
@@ -30,12 +30,20 @@ on:
3030

3131
jobs:
3232
test:
33+
name: test (${{ matrix.module.path }} - ${{ matrix.operating-system }})
34+
3335
strategy:
36+
fail-fast: false
37+
3438
matrix:
3539
operating-system:
3640
- ubuntu-latest
3741
- windows-latest
3842
- macos-latest
43+
module:
44+
# TODO: add paths of all Go modules here
45+
- path: ./
46+
codecov-flags: unit
3947

4048
runs-on: ${{ matrix.operating-system }}
4149

@@ -55,12 +63,14 @@ jobs:
5563
version: 3.x
5664

5765
- name: Run tests
66+
env:
67+
GO_MODULE_PATH: ${{ matrix.module.path }}
5868
run: task go:test
5969

6070
- name: Send unit tests coverage to Codecov
6171
if: matrix.operating-system == 'ubuntu-latest'
6272
uses: codecov/codecov-action@v2
6373
with:
64-
file: ./coverage_unit.txt
65-
flags: unit
74+
file: ${{ matrix.module.path }}coverage_unit.txt
75+
flags: ${{ matrix.module.codecov-flags }}
6676
fail_ci_if_error: true

workflow-templates/test-go-task.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Install the [`test-go-task.yml`](test-go-task.yml) GitHub Actions workflow to `.
2323

2424
Configure the version of Go used for development of the project in the `env.GO_VERSION` field of `test-go-task.yml`.
2525

26+
If the project contains Go modules in paths other than the root of the repository, add their paths to the [job matrix](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix) in `check-go-task.yml` at `jobs.test.strategy.matrix.module[].path` and the [Codecov flag](https://docs.codecov.com/docs/flags) to group their data under at `jobs.test.strategy.matrix.module[].codecov-flags`
27+
2628
#### `.gitignore`
2729

2830
Add the following to `.gitignore`:

0 commit comments

Comments
 (0)