我如何基于詹金斯中的参数选择AWS凭证

问题描述 投票:0回答:1

全部,

我有詹金斯(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`
jenkins groovy jenkins-groovy
1个回答
0
投票

您可以具有一个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"
                    }
                }
            }
        }        
    }     
}
© www.soinside.com 2019 - 2024. All rights reserved.