Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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)

about 4 years ago · Santiago Trujillo Relatório

0

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda