37

How can I teach my Jenkisfile to login via basic auth in this setup?

I'm using a custom docker image for my Jenkins build. As described in the documentation here I defined a docker agent like so:

pipeline {
agent { 
    docker {
        image 'registry.az1:5043/maven-proto'
        registryUrl 'https://registry.az1'
        args '-v /var/jenkins_home/.m2:/root/.m2'
    }
}
options {
    timeout(time: 1, unit: 'HOURS')
    buildDiscarder(logRotator(numToKeepStr:'10'))
}

stages {
    stage ('Build') {
        steps{
            sh ...
        }
    }

    stage ('Test') {
        steps {
            sh ...
        }
    } 

     stage ('Deploy') {
        steps {
            sh ...
        }
    }
}

post {
    always {
        echo 'Clean up workspace'
        deleteDir()
    }
}

}

If I use the following agent setup:

pipeline {
agent { 
    docker.withRegistry('https://registry.az1', 'registry_login'){
        image 'registry.az1:5043/maven-proto'
        registryUrl 'https://registry.az1'
        args '-v /var/jenkins_home/.m2:/root/.m2'
    }
}

The execution of the pipeline fails with the following exception:

WorkflowScript: 3: Too many arguments for map key "withRegistry" @ line 3, column 16.
       docker.withRegistry('https://registry.az1', 'registry_login'){
              ^

WorkflowScript: 3: Invalid agent type "withRegistry" specified. Must be one of [docker, dockerfile, label, any, none] @ line 3, column 16.
           docker.withRegistry('https://registry.az1', 'registry_login'){
                  ^

The problem is that the used registry requires a basic auth login. The registry runs behind a nginx reverse proxy using this configuration.

Robin Finkbeiner
  • 417
  • 1
  • 6
  • 11

1 Answers1

67

As specified in Using a custom registry, you can specify the credentials and registry url to use as such:

docker.withRegistry('https://registry.az1', 'credentials-id') {
    ...
}

You need to create a Jenkins credentials object which will contain the credentials for the repository and give it a name to replace credentials-id above.

Update:

For declarative pipelines, the syntax is as such:

agent { 
    docker {
        image 'registry.az1:5043/maven-proto'
        registryUrl 'https://registry.az1'
        registryCredentialsId 'credentials-id'
        args '-v /var/jenkins_home/.m2:/root/.m2'
    }
}
yamenk
  • 46,736
  • 10
  • 93
  • 87
  • 1
    I tried this earlier. Where in the Jenkisfile would this go exactly? Currently, I am defining the agent at the root level so that the definition is reused for each stage. – Robin Finkbeiner Feb 28 '18 at 12:27
  • 10
    Thank you, this worked for declarative pipeline. Information on specifying `registryUrl` and `registryCredentialsId` are missing in the docker pipeline documentation (https://jenkins.io/doc/book/pipeline/docker/) or the docker section under agent documentation (https://jenkins.io/doc/book/pipeline/syntax/#agent). How did you find them? Through the original Jira? https://issues.jenkins-ci.org/browse/JENKINS-39684 – haridsv Jul 30 '18 at 05:23
  • 2
    Never managed to get this to work. Chose to add the registry in Configure System --> `Pipeline Model Definition` section --> `Docker registry URL` and `Registry credentials` parameters. – Alfabravo Jan 16 '20 at 21:47
  • Thanks, that helped me a lot! For those using Google's GCR, use ```gcr:credentials-id``` and it should work. – romerorsp Jul 06 '22 at 22:32