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

I successfully create docker images from my jenkinsfile pipeline on a slave, simply using the standard "sh" commands.

I want to try to pull the image from the docker / hub, and if it does not work (because it has not been saved there yet), create it and click on it in the docker hub.

Obviously, I somehow need to store the credentials for the docker hub and provide them with a docker login command.

My problem is that I am confused. There are plugins for connecting dockers, but I think they are designed to run jenkins steps inside a docker container, not what I'm trying to do. Also, many examples are most likely a script, not declarative.

In simple terms, “steps” I think I want to 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 that I may 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 spin the pipeline, which currently works fine, except for this small amount.

Am I on the right?

+10
source share
2 answers

If you have a Docker plug-in for the pipeline , you can create and send images as follows.

dir(config.buildFolder){
                newImage = docker.build(${imageTag})
                docker.withRegistry("https://${registryAddress}", '${credentialsId}'){
                     newImage.push("${variables.version}")

            }

You can pull and execute some code inside the container as follows:

testbed = docker.image('${imageName}')
testbed.inside("-u root:root"){
                echo 'executing build inside container'
                sh 'dotnet restore'
            }

, , , (, ).

+7
0

Source: https://habr.com/ru/post/1673390/


All Articles