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

时间:2017-07-24 11:07:45

标签: amazon-web-services jenkins groovy credentials jenkins-job-dsl

我有以下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 SYNTAX!)

2 个答案:

答案 0 :(得分:8)

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

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

这将导致预期的结果。

答案 1 :(得分: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")
        }
    }