Pushing builds to DockerHub via Jenkins Pipelines

We use Jenkins extensively within Brightbox to manage our Continuous Integration/Continuous Deployment(CI/CD) architecture and keep Brightbox Cloud bang up to date with the latest images and facilities. In common with many other Jenkins users, we’ve started using the pipeline facilities to drive Jenkins directly from the software repositories.

For those looking how to build and push docker images to DockerHub from a declarative Jenkins pipeline, look no further. All you have to do is wrap your docker push in the correct withDockerRegistry call and run the pipeline on your docker nodes. Like this, from our Docker Images repo:

pipeline {
  agent { label 'docker' }
  options {
    buildDiscarder(logRotator(numToKeepStr: '5'))
  }
  triggers {
    cron('@daily')
  }
  stages {
    stage('Build') {
      steps {
        sh 'docker build -f "Dockerfile-terraform" -t brightbox/terraform:latest .'
        sh 'docker build -f "Dockerfile-cli" -t brightbox/cli:latest .'
      }
    }
    stage('Publish') {
      when {
        branch 'master'
      }
      steps {
        withDockerRegistry([ credentialsId: "6544de7e-17a4-4576-9b9b-e86bc1e4f903", url: "" ]) {
          sh 'docker push brightbox/terraform:latest'
          sh 'docker push brightbox/cli:latest'
        }
      }
    }
  }
}

The key is the blank url parameter, which DockerRegistry translates into the appropriate DockerHub reference.

The credentials are a username/password set containing the DockerHubID and the password for it. Create a new one on DockerHub and add it into your docker repository on DockerHub as a write enabled collaborator.

We use Jenkins pipelines extensively as the CI/CD of choice to maintain and improve Brightbox Cloud. You can sign up for Brightbox Cloud in just a couple of minutes and get a £20 free credit

Get started with Brightbox Sign up takes just two minutes...