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 {
The pipeline syntax do authorize additional parameters
You can pass additional arguments to the
docker build ...
command with theadditionalBuildArgs
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With