1

In Gitlab CI, I need to specify GITLAB_DEPLOY_TOKEN, as I have some private repositories. This works well for compile step.

But when I execute golint, it will download again all dependencies, and it will fail on private ones. I could add the same git config directive,

image: golang variables: PACKAGE_PATH: /go/src/gitlab.com/company/sam/daemon PACKAGE_API_NAME: registry.gitlab.com/company/sam/daemon REGISTRY_URL: https://registry.gitlab.com DOCKER_DRIVER: overlay GO111MODULE: "on"

.anchors: - &inject-gopath mkdir -p $(dirname ${PACKAGE_PATH}) && ln -s ${CI_PROJECT_DIR} ${PACKAGE_PATH} && cd ${PACKAGE_PATH}

compile:
  stage: build
  before_script:
    - *inject-gopath
    - git config --global url."https://oauth:${GITLAB_DEPLOY_TOKEN}@gitlab.com".insteadOf https://gitlab.com
    - go mod tidy
  script: GOOS=linux GOARCH=arm GOARM=7 go build -o release/daemon .
  artifacts:
    name: "binary-$CI_PIPELINE_ID"
    paths:
      - $GOPATH/pkg/mod/
    expire_in: 1 hour

lint:
  stage: test
  before_script:
    - apt install -y curl git
    - go get github.com/golang/lint
    - *inject-gopath
  script:
    - $GOPATH/bin/golint -set_exit_status $(go list ./...)
  allow_failure: true

I read here that go modules were cached in $GOPATH/pkg/mod but it doesn't seem to work

Any idea how should I fix it ?

Juliatzin
  • 18,455
  • 40
  • 166
  • 325

1 Answers1

0

Gitlab interprets all paths as relative to the project root, so if your project is myproject, $GOPATH/pkg/mod/ translates to myproject/$GOPATH/pkg/mod/, but the location where the go tool actually actually installs the packages is /$GOPATH/pkg/mod/ (directly under root).

You can work around this by setting the GOPATH to a location within the project directory; e.g.

export GOPATH="$CI_PROJECT_DIR/pkg/mod"

Then, you can use this path throughout your pipeline.

Isaac Kleinman
  • 3,994
  • 3
  • 31
  • 35