【问题标题】:How to pass AWS credential when building Docker image in Jenkins?在 Jenkins 中构建 Docker 映像时如何传递 AWS 凭证?
【发布时间】:2020-01-14 06:49:51
【问题描述】:

您好,我正在 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 被执行时我得到错误Need to perform AWS calls for account 1234567 but no credentials found. Tried: default credentials.

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

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

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

【问题讨论】:

  • 请记住,您不应该将凭据存储在 Docker 映像中,因为每个人都可以在那里获取它们。凭据通常作为环境变量传递给 Docker 容器。

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


【解决方案1】:

我能够传递如下凭据。

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

【讨论】:

  • 你的 cdkimage 内容是什么? (假设这是一个 dockerfile)
【解决方案2】:

您应该安装“Amazon ECR”插件并重新启动 Jenkins

使用您的凭据完成插件。并在管道中指定

您可以在这里找到所有文档https://wiki.jenkins.io/display/JENKINS/Amazon+ECR

【讨论】:

  • 谢谢,但我不想将图像推送到 ecr
【解决方案3】:

如果您使用的是 Jenkins 管道,也许您可​​以尝试withAWS 步骤。

这应该提供一种访问 Jenkins aws 凭据的方法,然后在运行 docker 容器时将凭据作为 docker 环境传递。

参考:

https://github.com/jenkinsci/pipeline-aws-plugin

https://jenkins.io/doc/book/pipeline/docker/

【讨论】:

    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 2021-08-06
    • 2019-12-29
    • 2019-11-15
    • 2020-06-28
    • 2018-03-27
    相关资源
    最近更新 更多