Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Docker's --cache-from build flag in a declarative Jenkins pipeline?

I'm using a declarative Jenkinsfile to run some stages inside a Docker container. The process works okay but the build times are often very slow as our CI has quite a few slaves and if the build occurs on a slave without the layer cache, the entire build takes a while.

I've read that Docker can speed up builds if a --cache-from flag is specified. How do I specify the cache-from flag and the external registry's URL and credentials?

pipeline {

  agent { dockerfile true }
  environment {
    REPO = credentials('supersecret')
  }

  stages {
    stage('Prepare environment') {
      steps {
like image 259
Mridang Agarwalla Avatar asked Oct 29 '22 01:10

Mridang Agarwalla


1 Answers

The pipeline syntax do authorize additional parameters

You can pass additional arguments to the docker build ... command with the additionalBuildArgs option, like agent

{ dockerfile { additionalBuildArgs '--build-arg foo=bar' } }

But cache-from refer to an image which might be in a dedicated external registry with its own credentials.
Maybe you can setup an first step just in charge of docker login in that registry.


Another approach entirely would be to reuse the same node for that specific build.
See "Reusing node/workspace with per-stage Docker agents"

pipeline {
  agent {
    label 'whatever'
  }
  stages {
    stage('build') {
      steps {
        sh "./build-artifact.sh"
      }
    }
    stage('test in docker') {
      agent {
        docker {
          image 'ubuntu:16.04'
          reuseNode true
        }
      }
      steps {
        sh "./run-tests-in-docker.sh"
      }
    }
  }
}

Then any docker build would benefit from the current local image cache.

like image 182
VonC Avatar answered Nov 15 '22 07:11

VonC