Docker allows passing the --pull
flag to docker build
, e.g. docker build --pull -t myimage .
. How can I enforce pulling the base image with a pipeline script in my Jenkinsfile
? This way I want to ensure that the build always uses the latest container image despite of the version available locally.
node('docker') {
def app
stage('Checkout') {
checkout scm
}
stage('Build image') {
docker.withRegistry('https://myregistry.company.com', 'dcr-jenkins') {
app = docker.build "myimage"
}
}
stage('Publish image') {
docker.withRegistry('https://myregistry.company.com', 'dcr-jenkins') {
app.push("latest")
}
}
}
The most straight-forward answer is to use the second argument to docker.build
stage('Build image') {
docker.withRegistry('https://myregistry.company.com', 'dcr-jenkins') {
app = docker.build("myimage", "--pull .")
}
}
If you don't supply it then it just defaults to .
, so if you pass in anything you must also include the context yourself.
You can find this in the "Pipeline Syntax - Global Variable Reference". Just add /pipeline-syntax/globals
to the end of any Jenkins URL (i.e. http://localhost:8080/job/myjob/pipeline-syntax/globals)
additionalBuildArgs does the job.
Example:
pipeline {
agent {
label "docker"
}
stages {
[…]
stage('Build image') {
agent {
dockerfile {
reuseNode true
registryUrl "https://registry.comapny.com"
registryCredentialsId "dcr-jenkins"
additionalBuildArgs "--pull --build-arg APP_VERSION=${params.APP_VERSION}"
dir "installation/app"
}
}
steps {
script {
docker {
app = docker.build "company/app"
}
}
}
}
[…]
}
}
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