简体   繁体   中英

How to use the credentials stored in Jenkins in a groovy script in jenkins DSL

I am using jenkins with Jobdsl to create jenkins jobs. I am trying to build a parameterized job by adding a groovy script in active choice parameter. The script uses a credential stored in jenkins credential, I am trying to fetch it in my script by using the code

import jenkins.model.*
import jenkins.*
import jenkins.model.* 
import hudson.*
import hudson.model.*

credentialsId = '1672622gjj'
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.Credentials.class,
        Jenkins.instance,
        null,
        null
).find{it.id == credentialsId};

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
  com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find{
    it.id == credentialsId}
    println(creds.username)
    println(creds.password)


This code gives me the credential name and password but the result of the branches is blank. I am using `creds.password` as the authorization token.

What I am doing wrong?

You can replace the string there the same way, you do already with your script name ( + the strings), yet this not groovy at all.

So this should work:

... "curl ... 'Authorization: token ${creds.password}' ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM