Skip to content
Snippets Groups Projects
Commit 471a77fe authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch...

Merge branch '35-golang-template-doesn-t-allow-for-go-generate-or-go-install-before-building' into 'master'

Resolve "Golang template doesn't allow for go generate or go install before building"

Closes #35

See merge request to-be-continuous/golang!68
parents 5a8bbf89 3f24f7b5
Branches
No related tags found
No related merge requests found
......@@ -49,6 +49,47 @@ The Go template uses some global configuration used throughout all jobs.
## Jobs
### go generate job
The Go template supports code generation with [go generate](https://go.dev/blog/generate).
It is disable by default and can be enabled by setting the `GO_GENERATE_MODULES` variable.
| Input / Variable | Description | Default value |
|------------------|------------------------------------------------------------------------------------------------------------|-----------------|
| `generate-modules` / `GO_GENERATE_MODULES` | Space separated list of Go code generator modules (ex: `stringer mockery`) | _none_ (disabled) |
#### Capture generated files as job artifacts
Using [go generate](https://go.dev/blog/generate) actually generates source files, that have to be captured and promoted all the way down to the build & test jobs.
In its default configuration, the template captures the following:
* any folder named `mock/` wherever in the file tree (entire content),
* any folder named `mocks/` wherever in the file tree (entire content),
* any file matching `*mock*.go` pattern wherever in the file tree.
If this default doesn't suit your needs, you'll have to override the artifact path patterns (YAML).
An example of this is given in the next chapter.
#### Example
```yaml
variables:
# list all required generate modules (including mockery)
GO_GENERATE_MODULES: >
github.com/vektra/mockery/v2@v2.38.0
github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@latest
mockery
# override the artifact path patterns
go-generate:
artifacts:
paths:
# list all the matchers to capture generated code
- "**/*mockery.go"
- "myapi/client/"
```
### build & test jobs
You can specify if you want the template to build an `application` or `modules` with the `GO_BUILD_MODE` variable. It may have the following values:
......
......@@ -90,6 +90,18 @@
}
],
"features": [
{
"id": "generate",
"name": "go generate",
"description": "generate code with [go generate](https://go.dev/blog/generate)",
"variables": [
{
"name": "GO_GENERATE_MODULES",
"description": "Space separated list of Go code generator modules (ex: `stringer mockery`)",
"mandatory": true
}
]
},
{
"id": "golangci-lint",
"name": "GolangCI-Lint",
......
......@@ -28,6 +28,9 @@ spec:
test-image:
description: Specific Docker image used to run Go tests (as a separate job)
default: ''
generate-modules:
description: "Space separated list of Go code generator modules (ex: `stringer mockery`)"
default: ''
build-flags:
description: Flags used by the [go build command](https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies)
default: -mod=readonly
......@@ -148,6 +151,8 @@ variables:
# Default Docker image (can be overridden)
GO_IMAGE: $[[ inputs.image ]]
GO_GENERATE_MODULES: $[[ inputs.generate-modules ]]
# Default flags for 'build' command
GO_BUILD_FLAGS: $[[ inputs.build-flags ]]
......@@ -476,6 +481,24 @@ stages:
- install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
- cd ${GO_PROJECT_DIR}
go-generate:
extends: .go-base
stage: .pre
script:
- go install $GO_GENERATE_MODULES
- go generate
rules:
# only if $GO_GENERATE_MODULES is set
- if: '$GO_GENERATE_MODULES != null && $GO_GENERATE_MODULES != ""'
artifacts:
name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
expire_in: 1 day
# default captured paths; otherwise has to be overwritten
paths:
- "${GO_PROJECT_DIR}/**/mock/"
- "${GO_PROJECT_DIR}/**/mocks/"
- "${GO_PROJECT_DIR}/**/*mock*.go"
go-build:
extends: .go-base
stage: build
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment