Gitlab: continuous delivery setup for Clojure/Java with Docker

Recently I was doing a microservice with Server Sent Events and Pedestal in Clojure and I though it should be a good idea to implement automatic build and deployment to docker registry container. I already have Gitlab installed and started to play around.

Long story short, here my .gitlab-ci.yml which took me a couple of days to figure out what is the “artifact” in Gitlab and how is it suppose to survive between artifact builds and docker builds.

My current setup has two stages, java build and then docker build. At the first stage we use clojure:lein-2.7.1-alpine, which is quite small, to build jar file from the sources. Then we try to assemble docker container and reuse artifact from the previous build. I was lucky enough to discover that artifact could be saved with help of “cache” option in YML file which preserves folder in “path” for the next build.

So here is the working Gitlab CI configuration which builds JAR as artifact and uploads to the Pipeline page and then creates docker container and publishes it to the internal Gitlab registry.

stages:
  - jar
  - docker
cache:
  paths:
    - target
jar:
  image: clojure:lein-2.7.1-alpine
  stage: jar
  script:
    - lein deps
    - lein uberjar
  artifacts:
    paths:
      - target/it-service-sse-0.0.1-SNAPSHOT-standalone.jar
    expire_in: 1 week

build-master:
  stage: docker
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"

I hope this will save you a couple of days for debug.

Happy Continuous Delivery!