how to prevent gitlab ci from downloading sbt every time?

后端 未结 2 656
花落未央
花落未央 2021-02-01 08:07

We have a play2/scala application which we are building with gitlab ci.

Our .gitlab-ci.yml (at least the important part) looks as follows:

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 08:23

    If you don't want to use custom made images, the best solution is to use Gitlab CI's caching mechanism.

    It's a little hard to get it right, but this blog post describes how to do it for SBT.

    Example .gitlab-ci.yml

    Quoted from the blog post, minor errors corrected by myself:

    # some parts originally from https://github.com/randm-ch/units-of-information/blob/master/.gitlab-ci.yml
    
    image: "hseeberger/scala-sbt"
    
    variables:
      SBT_VERSION: "0.13.9"
      SBT_OPTS: "-Dsbt.global.base=sbt-cache/.sbtboot -Dsbt.boot.directory=sbt-cache/.boot -Dsbt.ivy.home=sbt-cache/.ivy"
    
    cache:
      key: "$CI_BUILD_REF_NAME" # contains either the branch or the tag, so it's caching per branch
      untracked: true
      paths:
        - "sbt-cache/.ivy/cache"
        - "sbt-cache/.boot"
        - "sbt-cache/.sbtboot"
        - "sbt-cache/target"
    
    stages:
      - test
    
    test:
      script:
        - sbt test
    

    Second example, also including apt-get caching

    This is what I used for my project, usable for more general use cases and Docker images:

    image: java:8
    
    stages:
      - test
    
    variables:
      SBT_VERSION: "0.13.9"
      SBT_OPTS: "-Dsbt.global.base=sbt-cache/.sbtboot -Dsbt.boot.directory=sbt-cache/.boot -Dsbt.ivy.home=sbt-cache/.ivy"
      SBT_CACHE_DIR: "sbt-cache/.ivy/cache"
    
    cache:
      key: "$CI_BUILD_REF_NAME" # contains either the branch or the tag, so it's caching per branch
      untracked: true
      paths:
        - "apt-cache/"
        - "sbt-cache/.ivy/cache"
        - "sbt-cache/.boot"
        - "sbt-cache/.sbtboot"
        - "sbt-cache/target"
    
    before_script:
      - export APT_CACHE_DIR=`pwd`/apt-cache
      - mkdir -pv $APT_CACHE_DIR
      - ls $APT_CACHE_DIR || echo "no apt-cache dir found"
      - apt-get -o dir::cache::archives=$APT_CACHE_DIR update -y
      - apt-get -o dir::cache::archives=$APT_CACHE_DIR install apt-transport-https -y
      # Install SBT
      - mkdir -pv $SBT_CACHE_DIR
      - ls $SBT_CACHE_DIR || echo "no ivy2 cache fir found"
      - echo "deb http://dl.bintray.com/sbt/debian /" | tee -a /etc/apt/sources.list.d/sbt.list
      - apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 642AC823
      - apt-get -o dir::cache::archives=$APT_CACHE_DIR update -y
      - apt-get -o dir::cache::archives=$APT_CACHE_DIR install sbt -y
      - sbt -v sbtVersion
    
    test:
      stage: test
      script:
         - sbt -v sbtVersion
    

提交回复
热议问题