全部,
我有詹金斯(Jenkins)职位,该职位的选择参数称为account,例如accout-1,account-2,account-3
如果帐户是accoun-1,我想使用该帐户,然后选择account-1-id凭据。我尝试使用凭据id,也可以使用withCredentials
如果没有其他方法,该如何使用以下方法给我带来错误
'''
import jenkins.model.*
import hudson.model.*
credentialsId = 's3backup'
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
Jenkins.instance,
null,
null
).find{it.id == credentialsId};
return(creds)
'''
getting error as `[ERROR] - Problems occurs on injecting env vars defined in the build wrapper: hudson.AbortException: The evaluated Groovy script must return a Map object.. See system log for more info`
您可以具有一个INIT阶段,您可以在其中使用CredentialsID
条件将if..else
设置为特定的阶段。如下所示:
pipeline {
agent { label 'centos-docker' }
parameters {
choice choices: ['PRO', 'QA', 'DEV'], description: '', name: 'ACCOUNT'
}
stages {
stage('Set Credential ID and Run Tasks') {
steps {
script {
if ( "${params.ACCOUNT}" == 'PRO') {
env.userCredentialsID = "awsid1"
} else if ("${params.ACCOUNT}" == 'DEV') {
env.userCredentialsID = "awsid2"
} else {
env.userCredentialsID = "awsid3"
}
echo "${env.userCredentialsID}"
withCredentials([usernamePassword(credentialsId: "${env.userCredentialsID}", passwordVariable: 'PASS', usernameVariable: 'USER')]) {
echo "Credentials for ACCOUNT: \"${ACCOUNT}\" is being used"
}
}
}
}
}
}