From 0356dbf7715396a12c6d03a329f5795192f28086 Mon Sep 17 00:00:00 2001 From: per1234 Date: Thu, 12 Aug 2021 01:21:43 -0700 Subject: [PATCH 1/2] Remove redundant variable from asset taskfile The installation instructions for all templates that require the `DEFAULT_GO_PACKAGES` taskfile variable point to the "go-task" asset taskfile as its source. However, there was a redundant copy of this variable in the "check-go-task" asset taskfile. Maintaining two copies of this fairly complex asset is difficult, for no benefit in return, so it must be removed. --- workflow-templates/assets/check-go-task/Taskfile.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/workflow-templates/assets/check-go-task/Taskfile.yml b/workflow-templates/assets/check-go-task/Taskfile.yml index df268489..b7a7d2af 100644 --- a/workflow-templates/assets/check-go-task/Taskfile.yml +++ b/workflow-templates/assets/check-go-task/Taskfile.yml @@ -1,10 +1,6 @@ # See: https://taskfile.dev/#/usage version: "3" -vars: - DEFAULT_GO_PACKAGES: - sh: echo $(go list ./... | tr '\n' ' ') - tasks: # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml go:check: From e5b2bf73925a94b93030042c8b0a4b69aec902ca Mon Sep 17 00:00:00 2001 From: per1234 Date: Thu, 12 Aug 2021 01:51:09 -0700 Subject: [PATCH 2/2] 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. --- .../assets/check-go-task/Taskfile.yml | 4 ++ .../assets/go-task/Taskfile.yml | 3 +- .../assets/test-go-task/Taskfile.yml | 1 + workflow-templates/check-go-task.md | 8 +++ workflow-templates/check-go-task.yml | 52 +++++++++++++++++-- .../.github/workflows/check-go-task.yml | 52 +++++++++++++++++-- .../.github/workflows/test-go-task.yml | 22 +++++--- workflow-templates/test-go-task.md | 2 + workflow-templates/test-go-task.yml | 22 +++++--- 9 files changed, 145 insertions(+), 21 deletions(-) diff --git a/workflow-templates/assets/check-go-task/Taskfile.yml b/workflow-templates/assets/check-go-task/Taskfile.yml index b7a7d2af..c9f41319 100644 --- a/workflow-templates/assets/check-go-task/Taskfile.yml +++ b/workflow-templates/assets/check-go-task/Taskfile.yml @@ -12,18 +12,21 @@ tasks: # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml go:vet: desc: Check for errors in Go code + dir: '{{default "./" .GO_MODULE_PATH}}' cmds: - go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}} # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml go:fix: desc: Modernize usages of outdated APIs + dir: '{{default "./" .GO_MODULE_PATH}}' cmds: - go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}} # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml go:lint: desc: Lint Go code + dir: '{{default "./" .GO_MODULE_PATH}}' cmds: - | if ! which golint &>/dev/null; then @@ -38,5 +41,6 @@ tasks: # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml go:format: desc: Format Go code + dir: '{{default "./" .GO_MODULE_PATH}}' cmds: - go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}} diff --git a/workflow-templates/assets/go-task/Taskfile.yml b/workflow-templates/assets/go-task/Taskfile.yml index af611416..8a3dfe3d 100644 --- a/workflow-templates/assets/go-task/Taskfile.yml +++ b/workflow-templates/assets/go-task/Taskfile.yml @@ -3,7 +3,8 @@ version: "3" vars: DEFAULT_GO_PACKAGES: - sh: echo $(go list ./... | tr '\n' ' ') + sh: | + echo $(cd {{default "./" .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"') LDFLAGS: tasks: diff --git a/workflow-templates/assets/test-go-task/Taskfile.yml b/workflow-templates/assets/test-go-task/Taskfile.yml index d26080fc..edc4ab83 100644 --- a/workflow-templates/assets/test-go-task/Taskfile.yml +++ b/workflow-templates/assets/test-go-task/Taskfile.yml @@ -5,6 +5,7 @@ tasks: # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-go-task/Taskfile.yml go:test: desc: Run unit tests + dir: '{{default "./" .GO_MODULE_PATH}}' cmds: - | go test \ diff --git a/workflow-templates/check-go-task.md b/workflow-templates/check-go-task.md index a788e687..15f20d86 100644 --- a/workflow-templates/check-go-task.md +++ b/workflow-templates/check-go-task.md @@ -19,6 +19,14 @@ Install the [`check-go-task.yml`](check-go-task.yml) GitHub Actions workflow to Configure the version of Go used for development of the project in the `env.GO_VERSION` field of `check-go-task.yml`. +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: + +- `jobs.check-errors.strategy.matrix.module[].path` +- `jobs.check-outdated.strategy.matrix.module[].path` +- `jobs.check-style.strategy.matrix.module[].path` +- `jobs.check-formatting.strategy.matrix.module[].path` +- `jobs.check-config.strategy.matrix.module[].path` + ### Readme badge Markdown badge: diff --git a/workflow-templates/check-go-task.yml b/workflow-templates/check-go-task.yml index 8dce4973..2eb4f8a8 100644 --- a/workflow-templates/check-go-task.yml +++ b/workflow-templates/check-go-task.yml @@ -11,23 +11,32 @@ on: paths: - ".github/workflows/check-go-task.ya?ml" - "Taskfile.ya?ml" - - "go.mod" - - "go.sum" + - "**/go.mod" + - "**/go.sum" - "**.go" pull_request: paths: - ".github/workflows/check-go-task.ya?ml" - "Taskfile.ya?ml" - - "go.mod" - - "go.sum" + - "**/go.mod" + - "**/go.sum" - "**.go" workflow_dispatch: repository_dispatch: jobs: check-errors: + name: check-errors (${{ matrix.module.path }}) runs-on: ubuntu-latest + strategy: + fail-fast: false + + matrix: + module: + # TODO: add paths of all Go modules here + - path: ./ + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -44,11 +53,22 @@ jobs: version: 3.x - name: Check for errors + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task go:vet check-outdated: + name: check-outdated (${{ matrix.module.path }}) runs-on: ubuntu-latest + strategy: + fail-fast: false + + matrix: + module: + # TODO: add paths of all Go modules here + - path: ./ + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -65,14 +85,25 @@ jobs: version: 3.x - name: Modernize usages of outdated APIs + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task go:fix - name: Check if any fixes were needed run: git diff --color --exit-code check-style: + name: check-style (${{ matrix.module.path }}) runs-on: ubuntu-latest + strategy: + fail-fast: false + + matrix: + module: + # TODO: add paths of all Go modules here + - path: ./ + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -92,11 +123,22 @@ jobs: run: go install golang.org/x/lint/golint@latest - name: Check style + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task --silent go:lint check-formatting: + name: check-formatting (${{ matrix.module.path }}) runs-on: ubuntu-latest + strategy: + fail-fast: false + + matrix: + module: + # TODO: add paths of all Go modules here + - path: ./ + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -113,6 +155,8 @@ jobs: version: 3.x - name: Format code + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task go:format - name: Check formatting diff --git a/workflow-templates/dependabot/workflow-template-copies/.github/workflows/check-go-task.yml b/workflow-templates/dependabot/workflow-template-copies/.github/workflows/check-go-task.yml index 8dce4973..2eb4f8a8 100644 --- a/workflow-templates/dependabot/workflow-template-copies/.github/workflows/check-go-task.yml +++ b/workflow-templates/dependabot/workflow-template-copies/.github/workflows/check-go-task.yml @@ -11,23 +11,32 @@ on: paths: - ".github/workflows/check-go-task.ya?ml" - "Taskfile.ya?ml" - - "go.mod" - - "go.sum" + - "**/go.mod" + - "**/go.sum" - "**.go" pull_request: paths: - ".github/workflows/check-go-task.ya?ml" - "Taskfile.ya?ml" - - "go.mod" - - "go.sum" + - "**/go.mod" + - "**/go.sum" - "**.go" workflow_dispatch: repository_dispatch: jobs: check-errors: + name: check-errors (${{ matrix.module.path }}) runs-on: ubuntu-latest + strategy: + fail-fast: false + + matrix: + module: + # TODO: add paths of all Go modules here + - path: ./ + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -44,11 +53,22 @@ jobs: version: 3.x - name: Check for errors + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task go:vet check-outdated: + name: check-outdated (${{ matrix.module.path }}) runs-on: ubuntu-latest + strategy: + fail-fast: false + + matrix: + module: + # TODO: add paths of all Go modules here + - path: ./ + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -65,14 +85,25 @@ jobs: version: 3.x - name: Modernize usages of outdated APIs + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task go:fix - name: Check if any fixes were needed run: git diff --color --exit-code check-style: + name: check-style (${{ matrix.module.path }}) runs-on: ubuntu-latest + strategy: + fail-fast: false + + matrix: + module: + # TODO: add paths of all Go modules here + - path: ./ + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -92,11 +123,22 @@ jobs: run: go install golang.org/x/lint/golint@latest - name: Check style + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task --silent go:lint check-formatting: + name: check-formatting (${{ matrix.module.path }}) runs-on: ubuntu-latest + strategy: + fail-fast: false + + matrix: + module: + # TODO: add paths of all Go modules here + - path: ./ + steps: - name: Checkout repository uses: actions/checkout@v2 @@ -113,6 +155,8 @@ jobs: version: 3.x - name: Format code + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task go:format - name: Check formatting diff --git a/workflow-templates/dependabot/workflow-template-copies/.github/workflows/test-go-task.yml b/workflow-templates/dependabot/workflow-template-copies/.github/workflows/test-go-task.yml index a17fa795..79fa2f74 100644 --- a/workflow-templates/dependabot/workflow-template-copies/.github/workflows/test-go-task.yml +++ b/workflow-templates/dependabot/workflow-template-copies/.github/workflows/test-go-task.yml @@ -11,8 +11,8 @@ on: paths: - ".github/workflows/test-go-task.ya?ml" - "codecov.ya?ml" - - "go.mod" - - "go.sum" + - "**/go.mod" + - "**/go.sum" - "Taskfile.ya?ml" - "**.go" - "**/testdata/**" @@ -20,8 +20,8 @@ on: paths: - ".github/workflows/test-go-task.ya?ml" - "codecov.ya?ml" - - "go.mod" - - "go.sum" + - "**/go.mod" + - "**/go.sum" - "Taskfile.ya?ml" - "**.go" - "**/testdata/**" @@ -30,12 +30,20 @@ on: jobs: test: + name: test (${{ matrix.module.path }} - ${{ matrix.operating-system }}) + strategy: + fail-fast: false + matrix: operating-system: - ubuntu-latest - windows-latest - macos-latest + module: + # TODO: add paths of all Go modules here + - path: ./ + codecov-flags: unit runs-on: ${{ matrix.operating-system }} @@ -55,12 +63,14 @@ jobs: version: 3.x - name: Run tests + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task go:test - name: Send unit tests coverage to Codecov if: matrix.operating-system == 'ubuntu-latest' uses: codecov/codecov-action@v2 with: - file: ./coverage_unit.txt - flags: unit + file: ${{ matrix.module.path }}coverage_unit.txt + flags: ${{ matrix.module.codecov-flags }} fail_ci_if_error: true diff --git a/workflow-templates/test-go-task.md b/workflow-templates/test-go-task.md index 60c1ed16..210feb1e 100644 --- a/workflow-templates/test-go-task.md +++ b/workflow-templates/test-go-task.md @@ -23,6 +23,8 @@ Install the [`test-go-task.yml`](test-go-task.yml) GitHub Actions workflow to `. Configure the version of Go used for development of the project in the `env.GO_VERSION` field of `test-go-task.yml`. +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` + #### `.gitignore` Add the following to `.gitignore`: diff --git a/workflow-templates/test-go-task.yml b/workflow-templates/test-go-task.yml index a17fa795..79fa2f74 100644 --- a/workflow-templates/test-go-task.yml +++ b/workflow-templates/test-go-task.yml @@ -11,8 +11,8 @@ on: paths: - ".github/workflows/test-go-task.ya?ml" - "codecov.ya?ml" - - "go.mod" - - "go.sum" + - "**/go.mod" + - "**/go.sum" - "Taskfile.ya?ml" - "**.go" - "**/testdata/**" @@ -20,8 +20,8 @@ on: paths: - ".github/workflows/test-go-task.ya?ml" - "codecov.ya?ml" - - "go.mod" - - "go.sum" + - "**/go.mod" + - "**/go.sum" - "Taskfile.ya?ml" - "**.go" - "**/testdata/**" @@ -30,12 +30,20 @@ on: jobs: test: + name: test (${{ matrix.module.path }} - ${{ matrix.operating-system }}) + strategy: + fail-fast: false + matrix: operating-system: - ubuntu-latest - windows-latest - macos-latest + module: + # TODO: add paths of all Go modules here + - path: ./ + codecov-flags: unit runs-on: ${{ matrix.operating-system }} @@ -55,12 +63,14 @@ jobs: version: 3.x - name: Run tests + env: + GO_MODULE_PATH: ${{ matrix.module.path }} run: task go:test - name: Send unit tests coverage to Codecov if: matrix.operating-system == 'ubuntu-latest' uses: codecov/codecov-action@v2 with: - file: ./coverage_unit.txt - flags: unit + file: ${{ matrix.module.path }}coverage_unit.txt + flags: ${{ matrix.module.codecov-flags }} fail_ci_if_error: true