python - 在 Jenkins 中构建 Docker 镜像时如何传递 AWS 凭证?

标签 python amazon-web-services docker jenkins aws-cdk

您好,我正在 jenkins 中构建我的 AWS CDK 项目。我已经创建了我的 docker 文件,如下所示。

FROM python:3.7.4-alpine3.10
ENV CDK_VERSION='1.14.0'

RUN mkdir /cdk

COPY ./requirements.txt /cdk/
COPY ./entrypoint.sh /usr/local/bin/
COPY ./aws /cdk/
WORKDIR /cdk

RUN apk -uv add --no-cache groff jq less
RUN apk add --update nodejs npm
RUN apk add --update bash && rm -rf /var/cache/apk/*
RUN npm install -g aws-cdk
RUN pip3 install -r requirements.txt

RUN ls -la
ENTRYPOINT ["entrypoint.sh"]

RUN cdk synth
RUN cdk deploy

在 jenkins 中,我正在构建这个 Docker 镜像,如下所示。

 stages {
     stage('Dev Code Deploy') {
      when {
        expression {
          return BRANCH_NAME = 'Develop'
        }
      }
      agent {
        dockerfile {
          additionalBuildArgs "--build-arg 'http_proxy=${env.http_proxy}' --build-arg 'https_proxy=${env.https_proxy}'"
          filename 'Dockerfile'
          args '-u root:root'
        }
      }

在上面的代码中,我没有提供 AWS 凭证,因此,当执行 cdk Synth 时,我收到错误 需要对帐户 1234567 执行 AWS 调用,但未找到凭证。尝试过:默认凭据。

在 Jenkins 中,我有我的AWS凭证,我可以像这样访问它

 steps {
        withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',credentialsId: "${env.PROJECT_ID}-aws-${env.ENVIRONMENT}"]]) {
           sh 'ls -la'
           sh "bash ./scripts/build.sh"
        }
      }

但是在构建docker镜像时如何传递这些credentialsId。有人可以帮我弄清楚吗?任何帮助,将不胜感激。谢谢

最佳答案

我能够传递如下凭据。

steps {
        script {
          node {
            checkout scm
              withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',credentialsId: "${env.PROJECT_ID}-aws-${CFN_ENVIRONMENT}"]]) {
                abc = docker.build('cdkimage', "--build-arg http_proxy=${env.http_proxy} --build-arg https_proxy=${env.https_proxy} .")
                abc.inside{
                sh 'ls -la'
                sh "bash ./scripts/build.sh"
              }
        }
        }
      }

我在build.sh中添加了以下代码

cdk synth
cdk deploy

关于python - 在 Jenkins 中构建 Docker 镜像时如何传递 AWS 凭证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59728756/

相关文章:

python - 使用开放式CV python对两个图像进行边缘检测和直方图匹配。

python-3.x - 飞溅问题(d-bus,QSslSocket,libpng)

reactjs - 不是来自 webpack 的内容由/app/public docker 提供

docker - 如何在 consul 容器中为同一主机上的服务定义 HTTP 健康检查?

python - 导入MsgPackSerializer时导入错误

python - python中的低通线性滤波器

amazon-web-services - Python Boto AWS 删除 VPC 安全组规则

php - 单击 preSignedRequest 链接时,AWS S3 强制下载文件

amazon-web-services - 如何将CloudFormation创建的SNS主题ARN输出到命令行?

python - 编码转储更快,cPickle 加载更快