6

I'm using the jib Gradle plugin to create a docker image and push it to the Azure Container Registry. I've added username/password credentials to Jenkins so far and need to pass them to Gradle. Accessing or passing the credentials to Gradle, they get masked. Hope you can help me. Here're the code snippets:

build.gradle (jib configuration):

jib {
    to {
        image = "myacr.azurecr.io/" + project.name
        tags = ["latest"]
        auth {
            // retrieve from Jenkins
            username System.properties['ACR_CREDENTIALS_USR']
            password System.properties['ACR_CREDENTIALS_PSW']
        }
    }
    container {
        jvmFlags = ["-Xms512M",  "-Xmx1G"]
        ports = ["5000/tcp", "8080/tcp"]
    }    
}

Jenkinsfile:

pipeline {
...
    environment {
        ACR_CREDENTIALS = credentials('myproject-acr') 
    }

    stages {
        ...
        stage('Push Docker Image to Registry') {
            steps {
                sh "./gradlew jib -PACR_CREDENTIALS_USR=${env.ACR_CREDENTIALS_USR} -PACR_CREDENTIALS_PSW=${env.ACR_CREDENTIALS_PSW}"
            }
        }
...

EDIT: I had a typo in my username

Chanseok Oh
  • 3,920
  • 4
  • 23
  • 63
ndueck
  • 713
  • 1
  • 8
  • 27
  • Well, correct me if I am wrong, but this is exactly how the `credentials()` helper method is intended to behave - mask the credentials when echoed in the logs but still pass the real values to the environment and any utilities requesting them. E.g., if you write them to a file, you see the real values and not *****. – Dibakar Aditya Oct 25 '19 at 17:02
  • So, how do I pass the credentials to the environment? The environment-block in the Jeninsfile doesn't save these values as environment variables. I couldn't get them in gradle (using System.env['ACR_CREDENTIALS_USR']) – ndueck Oct 28 '19 at 08:33
  • I had a typo in my username :/ Accessing the environment variable in gradle works. – ndueck Oct 28 '19 at 09:11
  • Good to know that it worked. You might close the question with a note or an answer with regard to the same. – Dibakar Aditya Oct 28 '19 at 09:53

1 Answers1

6

I had a typo in the username. Passing Jenkins credentials as environment variables works as expected. Here's my code: build.gradle (jib configuration):

jib {
    to {
        image = "myacr.azurecr.io/" + project.name
        tags = ["latest"]
        auth {
            // retrieve from Jenkins
            username "${System.env.ACR_CREDENTIALS_USR}"
            password "${System.env.ACR_CREDENTIALS_PSW}"
        }
    }
    container {
        jvmFlags = ["-Xms512M",  "-Xmx1G"]
        ports = ["5000/tcp", "8080/tcp"]
    }    
}

Jenkinsfile:

pipeline {
...
    environment {
        ACR_CREDENTIALS = credentials('myproject-acr') 
    }

    stages {
        ...
        stage('Push Docker Image to Registry') {
            steps {
                sh "./gradlew jib"
            }
        }
...
ndueck
  • 713
  • 1
  • 8
  • 27