0

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=******
zoroglur
  • 395
  • 2
  • 18

2 Answers2

3

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}"
}
zoroglur
  • 395
  • 2
  • 18
0

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:

Pexers
  • 953
  • 1
  • 7
  • 20
  • Thanks for help, I tried before but It didnt work and I dont have Username and Password, I just have only token. I solved the problem by installing the Mask Passwords plugin. I'll put it as a comment. – zoroglur Nov 15 '22 at 14:30
  • That works as well. I've edited my answer afterwards to only use a _string_ instead of a username-password pair. Both answers are valid :) – Pexers Nov 15 '22 at 15:18