Commit 5a78d9d8 authored by Anoop Mazhavancheri's avatar Anoop Mazhavancheri Committed by Pierre Smeyers
Browse files

feat: optional installation of Go tools

In case Go tools used in the template (govulncheck, gocover-cobertura, go-junit-report, go-mod-outdated) are already available in the container image, the template skips their installation
parent 00a69a39
Loading
Loading
Loading
Loading
+27 −15
Original line number Diff line number Diff line
@@ -342,9 +342,12 @@ stages:
      log_info "--- \\e[32mCoverage report(s) found\\e[0m (\\e[33;1m${coverage_out}\\e[0m): output"
      percent=$(go tool cover -func="$coverage_out" | tail -1 | awk -F" " '{print $NF}')
      echo "${percent} covered"

      go get github.com/boumenot/gocover-cobertura
      GOFLAGS="$GO_COBERTURA_FLAGS" go run github.com/boumenot/gocover-cobertura < "$coverage_out" > reports/go-coverage.cobertura.xml
      if ! command -v gocover-cobertura  > /dev/null
      then
        log_info "Installing gocover-cobertura.."
        go install github.com/boumenot/gocover-cobertura@latest
      fi
      GOFLAGS="$GO_COBERTURA_FLAGS" gocover-cobertura < "$coverage_out" > reports/go-coverage.cobertura.xml
    else
      log_info "--- \\e[32mCoverage report(s) not found\\e[0m: skip"
    fi
@@ -419,7 +422,7 @@ stages:

    # produce JUnit report (for GitLab)
    install_go_junit_report
    "$GOBIN/go-junit-report" < "$go_text_report" > reports/go-test.xunit.xml
    go-junit-report < "$go_text_report" > reports/go-test.xunit.xml

    # produce JSON report (for SonarQube)
    go tool test2json < "$go_text_report" > reports/go-test.native.json
@@ -429,17 +432,25 @@ stages:
  }

  function install_go_junit_report() {
    if ! command -v go-junit-report  > /dev/null
    then
      cd "$(mktemp -d)"
      go mod init go-junit-report
      log_info "Installing go-junit-report.."
      go install github.com/jstemmer/go-junit-report@latest
      cd -
    fi
  }

  function install_go_mod_outdated() {
    if ! command -v go-mod-outdated  > /dev/null
    then
      cd "$(mktemp -d)"
      go mod init go-mod-outdated
      log_info "Installing go-mod-outdated.."
      go install github.com/psampaz/go-mod-outdated@latest
      cd -
    fi  
  }

  function install_go_govulncheck() {
@@ -483,6 +494,7 @@ stages:
      fi
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - cd ${GO_PROJECT_DIR}
    - export PATH=$GOBIN:$PATH

go-generate:
  extends: .go-base
@@ -601,9 +613,9 @@ go-mod-outdated:
    - go $GO_LIST_ARGS > reports/go-list.native.json
    - install_go_mod_outdated
    # console output (no fail)
    - $GOBIN/go-mod-outdated $GO_MOD_OUTDATED_ARGS < reports/go-list.native.json
    - go-mod-outdated $GO_MOD_OUTDATED_ARGS < reports/go-list.native.json
    # text report (-ci fails)
    - $GOBIN/go-mod-outdated $GO_MOD_OUTDATED_ARGS -ci < reports/go-list.native.json > reports/go-mod-outdated.native.txt
    - go-mod-outdated $GO_MOD_OUTDATED_ARGS -ci < reports/go-list.native.json > reports/go-mod-outdated.native.txt
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    expire_in: 1 day
@@ -662,7 +674,7 @@ go-govulncheck:
  script:
    - mkdir -p -m 777 reports  
    - install_go_govulncheck
    - $GOBIN/govulncheck ${GO_VULNCHECK_ARGS}
    - govulncheck ${GO_VULNCHECK_ARGS}
  rules:
    # exclude if GO_CI_LINT_DISABLED set
    - if: '$GO_VULNCHECK_DISABLED == "true"'