Jenkinsfile中的Parametricze withCredentials块

时间:2018-09-12 17:11:58

标签: jenkins groovy jenkins-pipeline

我试图在我的Jenkinsfile中参数化凭据块,但无法这样做。

这是我目前拥有的

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

String inputString = "00:01:30.500";

Date date = sdf.parse("1970-01-01 " + inputString);

我遇到错误了

stage("Deploy") {

  if ("${ENVIRONMENT}"=='dev') {
      wrap([$class: 'VaultBuildWrapper', configuration: configuration, vaultSecrets: secrets]) {
        withCredentials([string(credentialsId: 'secret1_dev', variable: 'SECRET1'), string(credentialsId: 'secret2_dev', variable: 'SECRET2'),string(credentialsId: 'secret3_dev', variable: 'SECRET3'), string(credentialsId: 'secret4_dev', variable: 'SECRET4'),string(credentialsId: 'secret5_dev', variable: 'SECRET5'), string(credentialsId: 'secret6_dev', variable: 'SECRET6')]){
  } else {
      wrap([$class: 'VaultBuildWrapper', configuration: configuration, vaultSecrets: secrets]) {
        withCredentials([string(credentialsId: 'secret1_qa', variable: 'SECRET1'), string(credentialsId: 'secret2_qa', variable: 'SECRET2'),string(credentialsId: 'secret3_qa', variable: 'SECRET3'), string(credentialsId: 'secret4_qa', variable: 'SECRET4'),string(credentialsId: 'secret5_qa', variable: 'SECRET5'), string(credentialsId: 'secret6_qa', variable: 'SECRET6')]){
  }
        sh """
        export DEPLOYMENT_ENVIRONMENT=${ENVIRONMENT}
        source ~/.bashrc
        echo 'parametrized credentials!'
        """
        }
      }
}

我要执行的操作取决于通过参数为作业提供的org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 58: expecting '}', found 'else' @ line 58, column 17. } else { ^ ,它选择要传递给工作的秘密变量的ENVIRONMENT

有人知道该怎么做吗?

谢谢

2 个答案:

答案 0 :(得分:2)

您的错误仅是由于您的块嵌套不正确。在使用大括号表示块的Groovy这样的编程语言中(在这种情况下),请从最外层到最内层打开块,然后从最内层到最外层封闭。例如

while(outer) {
    if (inner1) {
        echo "inner1"
    } else {
        echo "inner2"
    }
}

因此,对于您的情况,它看起来像这样(为了便于阅读,将长行换行):

stage("Deploy") {
    if ("${ENVIRONMENT}"=='dev') {
        wrap(
            [
                $class: 'VaultBuildWrapper',
                configuration: configuration,
                vaultSecrets: secrets
            ]
        ) {
            withCredentials(
                [
                    string(credentialsId: 'secret1_dev', variable: 'SECRET1'),
                    string(credentialsId: 'secret2_dev', variable: 'SECRET2'),
                    string(credentialsId: 'secret3_dev', variable: 'SECRET3'),
                    string(credentialsId: 'secret4_dev', variable: 'SECRET4'),
                    string(credentialsId: 'secret5_dev', variable: 'SECRET5'),
                    string(credentialsId: 'secret6_dev', variable: 'SECRET6')
                ]
            ) {
                sh """
                    export DEPLOYMENT_ENVIRONMENT=${ENVIRONMENT}
                    source ~/.bashrc
                    echo 'parametrized credentials!'
                """
            }
        }
    } else {
        wrap(
            [
                $class: 'VaultBuildWrapper',
                configuration: configuration,
                vaultSecrets: secrets
            ]
        ) {
            withCredentials(
                [
                    string(credentialsId: 'secret1_qa', variable: 'SECRET1'),
                    string(credentialsId: 'secret2_qa', variable: 'SECRET2'),
                    string(credentialsId: 'secret3_qa', variable: 'SECRET3'),
                    string(credentialsId: 'secret4_qa', variable: 'SECRET4'),
                    string(credentialsId: 'secret5_qa', variable: 'SECRET5'),
                    string(credentialsId: 'secret6_qa', variable: 'SECRET6')
                ]
            ) {
                sh """
                    export DEPLOYMENT_ENVIRONMENT=${ENVIRONMENT}
                    source ~/.bashrc
                    echo 'parametrized credentials!'
                """
            }
        }
    }
}

现在,这似乎不必要地重复了,对吧?您只希望拥有sh步骤的一个副本,并在其中包含正确的凭据,具体取决于${ENVIRONMENT}变量。您可以尝试将凭据的后缀存储在变量中,然后在每种情况下都使用GString作为名称。这样的事情应该可以解决问题(请注意:我还没有尝试过,但是我相对有信心它应该可以工作):

stage("Deploy") {
    final String credentialSuffix = ENVIRONMENT == 'dev' ? 'dev' : 'qa'
    wrap(
        [
            $class: 'VaultBuildWrapper',
            configuration: configuration,
            vaultSecrets: secrets
        ]
    ) {
        withCredentials(
            [
                string(credentialsId: "secret1_${credentialSuffix}", variable: 'SECRET1'),
                string(credentialsId: "secret2_${credentialSuffix}", variable: 'SECRET2'),
                string(credentialsId: "secret3_${credentialSuffix}", variable: 'SECRET3'),
                string(credentialsId: "secret4_${credentialSuffix}", variable: 'SECRET4'),
                string(credentialsId: "secret5_${credentialSuffix}", variable: 'SECRET5'),
                string(credentialsId: "secret6_${credentialSuffix}", variable: 'SECRET6')
            ]
        ) {
            sh """
                export DEPLOYMENT_ENVIRONMENT=${ENVIRONMENT}
                source ~/.bashrc
                echo 'parametrized credentials!'
            """
        }
    }
}

答案 1 :(得分:0)

我会接受@ gerard-ryan的答案,但这简化了我所需要的 从他的答案中得出

stage("Deploy") {
    wrap(
        [
            $class: 'VaultBuildWrapper',
            configuration: configuration,
            vaultSecrets: secrets
        ]
    ) {
        withCredentials(
            [
                string(credentialsId: "secret1_${ENVIRONMENT}", variable: 'SECRET1'),
                string(credentialsId: "secret2_${ENVIRONMENT}", variable: 'SECRET2'),
                string(credentialsId: "secret3_${ENVIRONMENT}", variable: 'SECRET3'),
                string(credentialsId: "secret4_${ENVIRONMENT}", variable: 'SECRET4'),
                string(credentialsId: "secret5_${ENVIRONMENT}", variable: 'SECRET5'),
                string(credentialsId: "secret6_${ENVIRONMENT}", variable: 'SECRET6')
            ]
        ) {
            sh """
                export DEPLOYMENT_ENVIRONMENT=${ENVIRONMENT}
                source ~/.bashrc
                echo 'parametrized credentials!'
            """
        }
    }
}

我没有最后一个字符串部分,因为我已经将ENVIRONMENT参数化了