web-dev-qa-db-fra.com

Utiliser un registre de docker privé avec authentification dans Jenkinsfile

Comment puis-je apprendre à mon Jenkisfile à se connecter via une authentification de base dans cette configuration?

J'utilise une image de menu fixe personnalisée pour ma construction Jenkins. Comme décrit dans la documentation ici J'ai défini un agent de menu fixe comme suit:

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()
    }
}

}

Si j'utilise la configuration d'agent suivante:

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'
    }
}

L'exécution du pipeline échoue avec l'exception suivante:

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'){
                  ^

Le problème est que le registre utilisé nécessite une connexion d'authentification de base. Le registre s'exécute derrière un proxy inverse nginx à l'aide de this configuration.

15
Robin Finkbeiner

Comme spécifié dans tilisation d'un registre personnalisé , vous pouvez spécifier les informations d'identification et l'URL du registre à utiliser en tant que telles:

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

Vous devez créer un objet d'informations d'identification Jenkins qui contiendra les informations d'identification pour le référentiel et lui donner un nom pour remplacer credentials-id au dessus de.

Mise à jour:

Pour les pipelines déclaratifs, la syntaxe est la suivante:

agent { 
    docker {
        image 'registry.az1:5043/maven-proto'
        registryUrl 'https://registry.az1'
        registryCredentialsId 'credentials-id'
        args '-v /var/jenkins_home/.m2:/root/.m2'
    }
}
28
yamenk