How to cache in gitlab-ci

3.7k Views Asked by At

How should i cache the coursier dependencies for the scala project in gitlab-ci. I am using docker executor.

*Gitlab - 8.4.2

Gitlab runner - 1.0.4*

my /etc/gitlab-runner/config.toml

  [runners.docker]
    image = "docker.**.com/docker:latest"
    privileged = false
    disable_cache = false
    volumes = [
      "/cache",
      "/var/run/docker.sock:/var/run/docker.sock",
      "/data/gitlab-runner/ansible_vault_password.txt:/etc/ansible_vault_password.txt:ro",
    ]

gitlab-ci.yml file contains

stages:
  - build
  - test
  - publish
  - cleanup

create:
  type: build
  script:
    - docker build --rm --pull -t docker.**.com/sbt:latest -f Dockerfile .
    - docker images

lint:
  type: test
  image: docker.***.com/sbt
  script:
    - sbt scalastyle
    - sbt scalafmtTest

unit tests:
  type: test
  image: docker.**.com/sbt
  script:
    - sbt test

publish jar:
  type: publish
  image: docker.**.com/sbt
  script:
    - sbt assembly
    - sbt publish
  only:
    - master

remove containers:
  type: cleanup
  script:
    - docker rmi -f docker.**.com/sbt:latest
  when: always

The plugins file contains

addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "0.4.8")
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.8.0")
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-M14")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.1")

Any suggestions or hints please?

1

There are 1 best solutions below

2
On BEST ANSWER

You'll need cache enabled on your Gitlab Runner (which it looks like you've got). From there, you'll need to add in a cache key to your .gitlab-ci.yml file. I'm not sure where sbt puts its cache directory by default, but you'll almost certainly have to change it so that it's inside the workspace directory.

See https://docs.gitlab.com/ce/ci/yaml/#cache for a complete guide, but I'd imagine you'd do something like:

stages:
  - build
  - test
  - publish
  - cleanup

cache:
  paths:
  - cache/sbt

create:
  type: build
  script:
    - docker build --rm --pull -t docker.xxx.com/sbt:latest -f Dockerfile .
    - docker images
...

Where cache/sbt is a path relative to your source folder that contains your sbt cache. You can only use paths that are within the project workspace for caching.