Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I push and pull from docker hub in Jenkins declarative pipeline

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?

like image 526
akc42 Avatar asked Mar 28 '17 08:03

akc42


People also ask

How do I push an image to Docker Hub from Jenkins?

Manage Jenkins → Manage Plugins In Jenkins you have to add a new credential with your Docker Hub account details. Go to Credentials → Global → Add credentials and fill out the form with your username and password. Create your Jenkins pipeline.


2 Answers

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)

like image 182
herm Avatar answered Sep 19 '22 16:09

herm


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.

https://gist.github.com/jglick/0aa389c053196e38e2a1

like image 43
Swagrid Avatar answered Sep 18 '22 16:09

Swagrid