Home > Back-end >  How to mask variable in Jenkins console[Groovy]
How to mask variable in Jenkins console[Groovy]

Time:11-16

How can I make groovy on Jenkins mask My groovy code:

sh "sonar-scanner -Dsonar.host.url=http://sonarurl:9000 -Dsonar.login=squ_a1111"

Jenkins Console:

sonar-scanner -Dsonar.host.url=http://sonarurl:9000 -Dsonar.login=squ_a1111

When I run it, the token appears in the console, but I don't want it to appear. How can I do masking?

I want it this way, For example:

sonar-scanner -Dsonar.host.url=http://sonarurl:9000 -Dsonar.login=******

CodePudding user response:

I installed Mask Passwords Plugin in Jenkins and used it as follows. Masking done

def token = "squ_a1111"
wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[password: token]]]) {
    sh "sonar-scanner -Dsonar.host.url=http://sonarurl:9000 -Dsonar.login=${token}"
}

CodePudding user response:

The simplest way would be to use the Credentials Binding plugin.

There you can define different types of credentials, whether it's a single password ("secret text"), or a file, or a username/password combination.

Once defined, you can use it like so:

steps {
    withCredentials([
        string(credentialsId: "sonar-creds", variable: 'PWD_SONAR')
    ]) {
        script {
            sh "sonar-scanner -Dsonar.host.url=http://sonarurl:9000 -Dsonar.login=$PWD_SONAR"
        }
    }
}

This will make the password available in the given variable only within this block. If you attempt to print the password, it will be masked.

See also:

  • Related