如何在Jenkins作业DSL中加载AWS凭据?

问题描述 投票:8回答:2

我有以下DSL结构:

freeStyleJob {
  wrappers {
    credentialsBinding {
      [
         $class:"AmazonWebServicesCredentialsBinding",
         accessKeyVariable: "AWS_ACCESS_KEY_ID",
         credentialsId: "your-credential-id",
         secretKeyVariable: "AWS_SECRET_ACCESS_KEY"
      ]
     }
   }
   steps {
      // ACCESS AWS ENVIRONMENT VARIABLES HERE!
   }
}

但是,这不起作用。这样做的正确语法是什么?对于Jenkins管道,您可以:

withCredentials([[
$class: "AmazonWebServicesCredentialsBinding",
accessKeyVariable: "AWS_ACCESS_KEY_ID",
credentialsId: "your-credential-id",
secretKeyVariable: "AWS_SECRET_ACCESS_KEY"]]) {
  // ACCESS AWS ENVIRONMENT VARIABLES HERE!
}

但是这种语法在正常的DSL作业groovy中不起作用。

tl; dr如何将AmazonWebServicesCredentialsBinding插件定义的AWS凭证导出到Groovy作业DSL的环境变量中? (不是PIPELINE PLUGIN语法!)

amazon-web-services jenkins groovy credentials jenkins-job-dsl
2个回答
8
投票

我找到了解决这个问题的解决方案:

wrappers {
  credentialsBinding {
    amazonWebServicesCredentialsBinding {
      accessKeyVariable("AWS_ACCESS_KEY_ID")
      secretKeyVariable("AWS_SECRET_ACCESS_KEY")
      credentialsId("your-credentials-id")
    }
  }
}

这将导致期望的结果。


0
投票

我无法重复使用Miguel的解决方案(即使安装了aws-credentials插件),所以这是DSL配置块的另一种方法

    configure { project ->
        def bindings = project / 'buildWrappers' / 'org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper' / 'bindings'
        bindings << 'com.cloudbees.jenkins.plugins.awscredentials.AmazonWebServicesCredentialsBinding' {
            accessKeyVariable("AWS_ACCESS_KEY_ID")
            secretKeyVariable("AWS_SECRET_ACCESS_KEY")
            credentialsId("credentials-id")
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.