Jenkins声明性管道 - 与AWS的使用一起使用

时间:2018-04-22 08:16:02

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline

在我以前的Jenkins文件中,我曾经有过这个:

withCredentials([[ $class: 'AmazonWebServicesCredentialsBinding',
                   credentialsId : 'aws',                                                 
                   accessKeyVariable: 'KEY_1',
                   secretKeyVariable: 'KEY_2'
                                     ]]) {
    node(..) { .. } }

e.g。我需要使用AWS信用卡的所有阶段都包含在withCreds中,但是使用声明性语法,我必须使用withCredentials阻止每个阶段这是不可接受的。我读到credentials('aws')块中有一些environments {}选项,但我无法弄清楚如何在那里指定我的信用等级,看来,看看它是错误的

pipeline {
   stages {
       stage('A') {
           steps {
              withCredentials([[ $class: 'AmazonWebServicesCredentialsBinding',
                   credentialsId : 'aws',                                                 
                   accessKeyVariable: 'KEY_1',
                   secretKeyVariable: 'KEY_2'
                                     ]]) { .. }
           }
       }
       stage('B') {
           steps {
              withCredentials([[ $class: 'AmazonWebServicesCredentialsBinding',
                   credentialsId : 'aws',                                                 
                   accessKeyVariable: 'KEY_1',
                   secretKeyVariable: 'KEY_2'
                                     ]]) { .. }
           }
       }
   }

}

请让我知道如何用Jenkins声明语法包装我的几个阶段withCredentials。感谢

1 个答案:

答案 0 :(得分:0)

您所要做的就是使用导出命令。

bindings

    Array/List
    Nested Choice of Objects
    $class: 'AmazonWebServicesCredentialsBinding'
    Sets one variable to the AWS access key and another one to the secret key given in the credentials.
        accessKeyVariable
        Environment variable name for the AWS Access Key Id. If empty, AWS_ACCESS_KEY_ID will be used.
            Type: String
        secretKeyVariable
        Environment variable name for the AWS Secret Access Key. If empty, AWS_SECRET_ACCESS_KEY will be used.
            Type: String
        credentialsId
        Credentials of an appropriate type to be set to the variable.
            Type: String

只需输入"导出accessKeyVariable"和"导出secretKeyVariable"在您的代码中,这应该在其他阶段可用。