I'm running a CI/CD pipeline with Jenkins using a custom image as a docker agent. At some point, I modified the dockerfile (I simply added curl inside the container, but this should play a marginal role in the question).
Now, when I'm running the build again, curl is not found amongst the other packages, and I believe is due to the fact that Jenkins has kept the old image in memory and is not building the containers out of the new version of my dockerfile. So, I'm trying to remove the image as a post action:
pipeline {
agent {
docker {
image 'my-personal-image'
registryUrl 'my-custom-registry'
args '-v ${WORKSPACE}:/usr/jenkins --rm=true'
}
}
stages {
...
}
post{
failure{
sh "docker rmi -f my-personal-image"
}
cleanup{
cleanWs()
}
}
}
Of course, this fails because the sh is passing the command inside the docker container.
+ docker rmi -f my-personal-image
/srv/jenkins/workspace/build@tmp/durable-cfcfebd7/script.sh:
1: /srv/jenkins/workspace/build@tmp/durable-cfcfebd7/script.sh:
docker: not found
I am wondering if there is a way to remove the image at the end of the build?
PS. I found out other ways of doing it by shifting to a scripted pipeline or by using '''agent none''' at the beginning and referencing the image in a variable later. Yet, I'm still dissatisfied that I could not do without recurring to a scripted pipeline (it works smoothly!) neither specifying the docker agent later in the Jenkinsfile at some stage level