Commit d33b1bcb authored by Yann D'Isanto's avatar Yann D'Isanto Committed by Pierre Smeyers
Browse files

fix: go-build failing for library without main.go

parent 197443a9
Loading
Loading
Loading
Loading
+40 −34
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ include:
The Go template uses some global configuration used throughout all jobs.

| Name             | description                                                                                                | default value   |
| --------------------- | ---------------------------------------------- | ----------------- |
|------------------|------------------------------------------------------------------------------------------------------------|-----------------|
| `GO_IMAGE`       | The Docker image used to run Go for `go-build` <br/>:warning: **set the version required by your project** | `golang:buster` |
| `GO_TEST_IMAGE`  | The Docker image used to run Go for `go-test` <br/>:warning: **set the version required by your project**  | _none_          |
| `GO_PROJECT_DIR` | Go project root directory                                                                                  | `.`             |
@@ -30,6 +30,11 @@ The Go template uses some global configuration used throughout all jobs.

### 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: 
 * `application` will make the build output the binaries (use `-o` build option, won't work if there is no `main.go` file)
 * `modules` won't output the binaries (no use of the `-o` option)
 * `auto` the template will rely on the presence of a `main.go` file to detect if it should output the binaries.

The build target platform is the one defined by the docker image but it can be overriden using the `GO_TARGET_OS` and `GO_TARGET_ARCH` variables.

```yaml
@@ -65,20 +70,21 @@ go-build:
These jobs use the following variable:

| Name                    | description                                                                                                             | default value                                 |
| ----------------------- | ---------------------------------------- | ----------------- |
|-------------------------|-------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| `GO_BUILD_MODE`         | The template build mode (accepted values are `application`, `modules` and `auto`)                                       | `auto`                                        |
| `GO_BUILD_FLAGS`        | Flags used by the [go build command](https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies)                   | `-mod=readonly`                               |
| `GO_BUILD_LINKER_FLAGS` | Linker flags used by the [go build command](https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies) `-ldflags` | `-s -w`                                       |
| `GO_BUILD_PACKAGES`     | Packages to build with the [go build command](https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies)          | `./...`                                       |
| `GO_TEST_FLAGS`         | Flags used by the [go test command](https://pkg.go.dev/cmd/go#hdr-Test_packages)                                        | `-mod=readonly -v -race`                      |
| `GO_TEST_PACKAGES`      | Packages to test with the [go test command](https://pkg.go.dev/cmd/go#hdr-Test_packages)                                | `./...`                                       |
| `GO_LIST_ARGS`          | Arguments used by the list command                                                                                      | `list -u -m -mod=readonly -json all`          |
| `GO_TARGET_OS`          | The `GOOS` target [see available values](https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63)         | _none_ (fallback to go docker image `GOOS`)
| `GO_TARGET_ARCH`          | The `GOARCH` target [see available values](https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63)         | _none_ (fallback to go docker image `GOARCH`)
| `GO_TARGET_OS`          | The `GOOS` target [see available values](https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63)           | _none_ (fallback to go docker image `GOOS`)   |
| `GO_TARGET_ARCH`        | The `GOARCH` target [see available values](https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63)         | _none_ (fallback to go docker image `GOARCH`) |

In addition to a textual report in the console, the test jobs produce the following reports, kept for one day:

| Report                                              | Format                                                                             | Usage                                                                                                                 |
| -------------- | ---------------------------------------------------------------------------- | ----------------- |
|-----------------------------------------------------|------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|
| `$GO_PROJECT_DIR/reports/go-test.native.txt`        | native Go test report (text)                                                       | N/A                                                                                                                   |
| `$GO_PROJECT_DIR/reports/go-test.native.json`       | native Go test report (json)                                                       | [SonarQube integration](https://docs.sonarqube.org/latest/analysis/test-coverage/test-execution-parameters/#header-8) |
| `$GO_PROJECT_DIR/reports/go-test.xunit.xml`         | [xUnit](https://en.wikipedia.org/wiki/XUnit) test report(s)                        | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportsjunit)                 |
@@ -92,7 +98,7 @@ This job enables a manual [GolangCI-Lint](https://github.com/golangci/golangci-l
It is bound to the `build` stage, and uses the following variables:

| Name                  | description                                                                                              | default value                          |
| --------------------- | -------------------------------------------- | ----------------- |
|-----------------------|----------------------------------------------------------------------------------------------------------|----------------------------------------|
| `GO_CI_LINT_IMAGE`    | The Docker image used to run `golangci-lint`                                                             | `golangci/golangci-lint:latest-alpine` |
| `GO_CI_LINT_ARGS`     | `golangci-lint` [command line arguments](https://github.com/golangci/golangci-lint#command-line-options) | `-E gosec,goimports ./...`             |
| `GO_CI_LINT_DISABLED` | Set to `true` to disable this job                                                                        | _none_(enabled)                        |
@@ -100,7 +106,7 @@ It is bound to the `build` stage, and uses the following variables:
In addition to a textual report in the console, this job produces the following reports, kept for one day:

| Report                                                | Format                                                   | Usage                                                                                                       |
| -------------- | ---------------------------------------------------------------------------- | ----------------- |
|-------------------------------------------------------|----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------|
| `$GO_PROJECT_DIR/reports/go-ci-lint.codeclimate.json` | [Code Climate](https://docs.codeclimate.com/docs/pylint) | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportscodequality) |
| `$GO_PROJECT_DIR/reports/go-ci-lint.checkstyle.xml`   | Checkstyle                                               | [SonarQube integration](https://docs.sonarqube.org/latest/analysis/external-issues/)                        |

@@ -111,7 +117,7 @@ This job enables a manual [Go-mod-outdated](https://github.com/psampaz/go-mod-ou
It is bound to the `test` stage, and uses the following variables:

| Name                   | description                                                                                   | default value     |
| --------------------- | -------------------------------------------- | ----------------- |
|------------------------|-----------------------------------------------------------------------------------------------|-------------------|
| `GO_MOD_OUTDATED_ARGS` | `god-mod-outdated` [command line arguments](https://github.com/psampaz/go-mod-outdated#usage) | `-update -direct` |

Checking outdated modules can be a long operation and therefore the job is configured to be ran **manually** by default (overridable).
+12 −0
Original line number Diff line number Diff line
@@ -31,6 +31,18 @@
      "default": "-mod=readonly",
      "advanced": true
    },
    {
      "name": "GO_BUILD_MODE",
      "description": "The template build mode (accepted values are `application`, `modules` and `auto`)",
      "type": "enum",
      "values": [
        "auto",
        "application",
        "modules"
      ],
      "default": "auto",
      "advanced": true
    },
    {
      "name": "GO_BUILD_LINKER_FLAGS",
      "description": "Linker flags used by the [go build command](https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies) `-ldflags`",
+38 −0
Original line number Diff line number Diff line
@@ -61,6 +61,9 @@ variables:
  GO_BUILD_PACKAGES: >-
    ./...

  # Default build mode (application/modules/auto)
  GO_BUILD_MODE: auto

  # Default flags for 'test' command
  GO_TEST_FLAGS: >-
    -mod=readonly
@@ -130,6 +133,35 @@ stages:
  }

  function go_build() {
    case "${GO_BUILD_MODE}" in
      application)
        go_build_application
        ;;
      modules)
        go_build_modules
        ;;
      auto)
        go_build_auto
        ;;
      *)
        log_error "invalid \\e[94;1mGO_BUILD_MODE\\e[0m (expected values are \\e[96;1mapplication\\e[0m, \\e[96;1mmodules\\e[0m, \\e[96;1mauto\\e[0m)"
        exit 1
        ;;
    esac
  }

  function go_build_auto() {
    log_info "auto build mode, looking up for a \\e[33;1mmain.go\\e[0m file..."
    found_main_go=$(find ./ -name 'main.go' | wc -l)
    if [ "${found_main_go}" -eq 0 ]; then
      go_build_modules
    else
      go_build_application
    fi
  }

  function go_build_application() {
    log_info "building go application"
    GO_TARGET_OS="${GO_TARGET_OS:-$GOOS}"
    GO_TARGET_ARCH="${GO_TARGET_ARCH:-$GOARCH}"
    target_dir="$GOBIN/$GO_TARGET_OS/$GO_TARGET_ARCH"
@@ -138,6 +170,12 @@ stages:
    GOOS="$GO_TARGET_OS" GOARCH="$GO_TARGET_ARCH" go build -ldflags="$GO_BUILD_LINKER_FLAGS" $GO_BUILD_FLAGS -o "$target_dir" $GO_BUILD_PACKAGES
  }

  function go_build_modules() {
    log_info "building go modules"
    # shellcheck disable=SC2086
    go build -ldflags="$GO_BUILD_LINKER_FLAGS" $GO_BUILD_FLAGS $GO_BUILD_PACKAGES
  }

  function go_test() {
    mkdir -p -m 777 reports
    local go_text_report="reports/go-test.native.txt"