4

在我上一个 Jenkinsfile 中,我曾经有这个:

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

例如,我需要使用 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'
                                     ]]) { .. }
           }
       }
   }

}

请让我知道如何将我的几个阶段包装withCredentials在 Jenkins 声明式语法中。谢谢

4

1 回答 1

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

只需在您的代码中添加“export accessKeyVariable”和“export secretKeyVariable”,这应该可以在其他阶段使用。

于 2018-05-16T00:39:11.667 回答