1

In a simple jenkinsfile as seen bellow:

pipeline {
agent {
    label 'my-agent'
}

stages {
    stage ('Docker version') {
        steps {
            sh 'docker --version'
        }
    }

    stage ('Docker Login Test') {
        steps {
            script {
                withCredentials([usernamePassword(credentialsId: 'mycredentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASSWORD')]) {

                    echo "docker login naked"
                    sh "docker login myAzureRepo.azurecr.io -u admin -p 1234"

                    echo "docker login protected"
                    sh "docker login myAzureRepo.azurecr.io -u $DOCKER_USER -p $DOCKER_PASSWORD" 
                }
            }
        }
    }
}

When naked credentials are used, I get successfull login, and have even tried to push images, and works fine.

But when i get the password from credentials store, I get the following error from jenkins.

docker login myAzureRepo.azurecr.io -u ****Error: Cannot perform an interactive login from a non TTY device

Vergil C.
  • 1,046
  • 2
  • 15
  • 28
  • The variables you stored the username and password are the environment variables. You should set them in the shell script or the Jenkins node. – Charles Xu Sep 19 '18 at 15:48

1 Answers1

2

After trying out many different ways, one worked. The username must be provided, only the password can be passed as variable.

So instead of

sh "docker login myAzureRepo.azurecr.io -u $DOCKER_USER -p $DOCKER_PASSWORD"

I used

sh "docker login myAzureRepo.azurecr.io -u admin -p $DOCKER_PASSWORD"

And worked fine. At least the password is hidden.

The registry in the examples is a made one one, the registry I am working on has different name and credentials.

But if you know better ways, please spread the love. I am just starting working with Jenkins, docker and microservices and am loving it.

Vergil C.
  • 1,046
  • 2
  • 15
  • 28
  • Note, that the password might be printed to the console, as all commands are printed to the console. This might help: https://stackoverflow.com/questions/47523909/hide-command-executed-only-show-output/51607994 – Chris Jul 20 '20 at 09:32