2

A very typical scenario with GitLab CI is to install a few packages you need for your jobs (linters, code coverage tools, deployment-specific helpers and so on) and to then run your actual stages/steps of a building, testing and deploying your software.

The Docker runner is a very neat and clean solution, but it seems very wasteful to always run the steps that install the base software. Normally, Docker is able to cache such layers, but with the way the GitLab Docker runner works, that doesn't happen.

Do we realize that setting up another project to produce pre-configured Docker images would be one solution, but are there any better ones? Basically, what we want to say is: "If the before section hasn't changed, you can reuse the image from last time, no need to reinstall wget or whatever".

Any solution like that out there?

Vishal Parmar
  • 524
  • 7
  • 27
Torque
  • 3,319
  • 2
  • 27
  • 39
  • I don't think it's supported. The prepared docker image solution looks pretty neat to me. Might be related - https://gitlab.com/gitlab-org/gitlab-foss/issues/17861 – shakhawat Jan 29 '20 at 10:10
  • Looks like this has been asked before: https://stackoverflow.com/questions/34820755/gitlab-docker-executor-cache-image-after-before-script?rq=1 – Sebastian Heikamp Jan 29 '20 at 11:44

1 Answers1

1

You can use the registry of your gitlab project.

eg.

images:
    stage: build
    image: docker
    services:
        - docker:dind
    script:
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY # login
        # pull the current image or in case the image does not exit, do not stop the script:
        - docker pull $CI_REGISTRY_IMAGE:latest || true
        # build with the pulled image as cache:
        - docker build --pull --cache-from $CI_REGISTRY_IMAGE:latest -t "$CI_REGISTRY_IMAGE:latest" .
        # push the final image:
        - docker push "$CI_REGISTRY_IMAGE:latest"

This way docker build will profit from the work done by the last run of the job. See the docs. Maybe you want to avoid unnecessary runs by some rules.

RiWe
  • 367
  • 3
  • 9