14

I use gitlab-ci to test, compile and deploy a small golang application but the problem is that the stages take longer than necessary because they have to fetch all of the dependencies every time.

How can I keep the golang dependencies between two stages (test and build)?

This is part of my current gitlab-ci config:

test:
    stage: test
    script:
        # get dependencies
        - go get github.com/foobar/...
        - go get github.com/foobar2/...
        # ...
        - go tool vet -composites=false -shadow=true *.go
        - go test -race $(go list ./... | grep -v /vendor/)

compile:
    stage: build
    script:
        # getting the same dependencies again
        - go get github.com/foobar/...
        - go get github.com/foobar2/...
        # ...
        - go build -race -ldflags "-extldflags '-static'" -o foobar
    artifacts:
        paths:
            - foobar
irgendwr
  • 537
  • 5
  • 18

2 Answers2

15

As mentioned by Yan Foto, you can only use paths that are within the project workspace. But you can move the $GOPATH to be inside your project, as suggested by extrawurst blog.

test:
  image: golang:1.11
  cache:
    paths:
      - .cache
  script:
    - mkdir -p .cache
    - export GOPATH="$CI_PROJECT_DIR/.cache"
    - make test
voiski
  • 437
  • 5
  • 10
  • 2
    Thanks! Both your and Yan Foto's answer are useful workarounds, I upvoted both. What you're suggesting here seems a bit less hacky so I'll accept it :) – irgendwr Jul 25 '19 at 13:04
7

This is a pretty tricky task, as GitLab does not allow caching outside the project directory. A quick and dirty task would be to copy the contents of $GOPATH under some directory inside the project (say _GO), cache it and copy it upon each stage start back to $GOPATH:

after_script:
  - cp -R $GOPATH ./_GO || :

before_script:
  - cp -R _GO $GOPATH

cache:
  untracked: true
  key: "$CI_BUILD_REF_NAME"
  paths:
    - _GO/

WARNING: This is just a (rather ugly) workaround and I haven't tested it myself. It should only exhibit a possible solution.

Peter
  • 29,454
  • 5
  • 48
  • 60
Yan Foto
  • 10,850
  • 6
  • 57
  • 88