gitlab ci cache/keep golang packages between stages

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

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.