Reputation: 528
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")
}
}
}
Upvotes: 7
Views: 14234
Reputation: 627
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)
Upvotes: 11
Reputation: 528
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"
}
}
}
}
[…]
}
}
Upvotes: 5
Reputation: 4677
docker rmi <image>
at the beginning of your script, before docker build --pull
. The image will not exist locally when docker build --pull
executes, so it will be downloaded fresh each time.
Upvotes: 0