正在努力了解如何从Jenkins验证到AWS

m4pnthwp  于 2023-04-29  发布在  Jenkins
关注(0)|答案(1)|浏览(164)

我试图使用DefaultAWSCredentialsProvider将AWS认证到Jenkins中,但由于某种原因,它从未起作用!

AWSSecurityTokenService awsSecurityTokenService =       AWSSecurityTokenServiceClientBuilder.standard()
                 .withCredentials(new DefaultAWSCredentialsProviderChain())
                .withRegion(region)
                .build();

我试过用这个

pipeline {
   agent {
    // Define agent details here
}
environment {
    AWS_ACCESS_KEY_ID     = credentials('aws_access_key_id')
    AWS_SECRET_ACCESS_KEY = credentials('aws_secret_access_key')
}

当我尝试输出它时,我看到了这个。我不太熟悉Groovy和Jenkins。

  • aws_access_key_id = XXXXXXXXX /workspace/_qa-new-gih_feature_jenkinstests@tmp/durable-49d31937/ www.example.com :1:/workspace/_qa-new-gih_feature_jenkinstests@tmp/durable-49d31937/script.sh:aws_access_key_id:未找到

我肯定我错过了一些东西,有人可以帮忙吗?

vlurs2pr

vlurs2pr1#

您可以使用aws-cli登录方法进行身份验证
您只需要https://plugins.jenkins.io/credentials-binding/使用这个withCredentials函数,以及这个https://plugins.jenkins.io/aws-credentials/ AWS凭证插件,就可以使用AccessKeyIdSecretAccessKey创建全局或系统凭证。
然后在pipeline中的任何阶段步骤中使用下面的代码片段(推荐:第一阶段),以便您可以为该管道示例验证aws-cli。
注意:请确保您没有为此目的使用启用MFA的帐户,并在节点模板上预先安装aws-cli。

stages {
        stage("aws-cli-login") {
            steps {
                withCredentials([[
                    $class: "AmazonWebServicesCredentialsBinding",
                    credentialsId: "your.aws.credentials",
                    accessKeyVariable: "AWS_ACCESS_KEY_ID",
                    secretKeyVariable: "AWS_SECRET_ACCESS_KEY"
                ]]) {
                    sh 'aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID'
                    sh 'aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY'
                }
                sh "aws do something here or in any stage further"
            }
        }
    }

相关问题