Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass Jenkins credentials to gradle?

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

like image 412
ndueck Avatar asked Oct 25 '19 16:10

ndueck


People also ask

How do you pass parameters from Jenkins to gradle?

If you use the Gradle Jenkins plugin, Jenkins build parameter are passed to Gradle as System properties. You can then convert it to a boolean (if needed) with the toBoolean() method. Small tip: Declare aGradle property for the Systen properties map if you need to access to several properties.

How do I use Jenkins Gradle wrapper?

Create a Jenkins jobFrom the left navigation bar select "New Item > Freestyle project". Enter a new name for the project. We'll pick "gradle-site-plugin" for the project. Select the radio button "Git" in the section "Source Code Management".


1 Answers

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"
            }
        }
...
like image 198
ndueck Avatar answered Sep 18 '22 16:09

ndueck