1

I'm trying to push a Docker image from Jenkins to DockerHub using a declarative pipeline. The DockerHub's credentials are stored in Vault. And, I wish to use the Docker plugin in my pipeline's syntax.

My following tries were successful:

  1. If I store Dockerhub's credentials in Jenkins, the pipeline works fine with the following code snippet:
stage('Publish the Docker Image on DockerHub')
{
    steps {
        script {
            docker.withRegistry('', 'dockerhub-credentials'){
                dockerImage.push()
            }
        }
    }
}
  1. If I store Dockerhub's credentials in Vault and use shell commands to login, then too the pipeline works successful with the code snippet as below:
stage('Publish the Docker Image on DockerHub')
{
    steps 
    {
        withVault(
            configuration: \
            [
                timeout: 60,
                vaultCredentialId: 'vault-jenkins-approle-creds',
                vaultUrl: 'http://172.31.32.203:8200'
            ],
            vaultSecrets: 
            [[
                engineVersion: 2,
                path: 'secret/credentials/dockerhub',
                secretValues:
                [
                    [envVar: 'DOCKERHUB_USERNAME', vaultKey: 'username'],
                    [envVar: 'DOCKERHUB_PASSWORD', vaultKey: 'password']
                ]
            ]]
        )
        {
            script 
            {
                sh "docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD"
                sh "docker push <docker-hub-repo>"
            }
        }
    }
}

Now, my query is how to parse the Username+Password credentials (obtained in 2) inside the docker.withRegistry() method (used in 1)?

matak8s
  • 497
  • 4
  • 7
  • The `withRegistry` method would need to support the Jenkins Vault plugin in addition to the Credentials Binding plugin, which it does not. You could retrieve the credentials and publish them to the a credentials plugin ID within the pipeline, but that would not be super great from a security perspective. https://www.jenkins.io/doc/pipeline/steps/docker-workflow/#withdockerregistry-sets-up-docker-registry-endpoint – Matthew Schuchard Jan 05 '23 at 18:28

0 Answers0