I am successfully building docker images from my jenkinsfile pipeline on a slave, just using standard "sh" commands.
I want to be able to try and pull an image from docker/hub, and if it fails (because it hasn't been saved there yet) build it and push it to docker hub.
Obviously I somehow need to store credentials for docker hub and provide them to a docker login command.
My problem is I am confused. There are docker pipeline plugins, but I think they are for running jenkins steps inside a docker container - not what I am trying to do. Also many examples seem to be scripted pipeline rather than declarative.
In simple "steps" terms I think I want do do something like
agent {
label 'pi'
}
steps {
sh 'docker login -u my-account -p xxxx'
sh 'docker pull my-account/image:version || (docker build -f dockerfile -t my-account/image:version && docker push my-account/image:version)'
....
}
but I also suspect I maybe need to do something like
steps {
script {
withDockerRegistry([credentialsId: 'something', url: 'docker.io/my-account']) {
sh 'docker pull my-account/image:version || (docker build -f dockerfile -t my-account/image:version && docker push my-account/image:version)'
....
}
}
but I don't want to make any mistakes and screw a pipeline that is currently working fine except for this little bit.
Am I along the right lines?
If you have the Docker pipeline plugin you can create and push images like this.
dir(config.buildFolder){
newImage = docker.build(${imageTag})
docker.withRegistry("https://${registryAddress}", '${credentialsId}'){
newImage.push("${variables.version}")
}
You can pull and execute some code inside a container like this:
testbed = docker.image('${imageName}')
testbed.inside("-u root:root"){
echo 'executing build inside container'
sh 'dotnet restore'
}
This will automatically mount the folder from which you created the container as root directory of your container so that you can work with that data (like build an application)
I'm also working on the same issue. I wish to pull Docker images from my Dockerhub repo and perform some tests with them. I have managed to get my pipeline working by following the steps shown here.