Python 在Jenkins建立Docker形象时如何通过AWS认证?

Python 在Jenkins建立Docker形象时如何通过AWS认证?,python,amazon-web-services,docker,jenkins,aws-cdk,Python,Amazon Web Services,Docker,Jenkins,Aws Cdk,嗨,我在詹金斯工作,以建立我的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 j

嗨,我在詹金斯工作,以建立我的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
 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'
        }
      }
在詹金斯,我正在构建这个码头工人形象,如下所示

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
 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调用,但没有找到凭据。已尝试:默认凭据。

在詹金斯,我有我的AWS证书,我可以像这样访问它

 steps {
        withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',credentialsId: "${env.PROJECT_ID}-aws-${env.ENVIRONMENT}"]]) {
           sh 'ls -la'
           sh "bash ./scripts/build.sh"
        }
      }
但在建立docker形象时,我如何传递这些凭证ID呢。有人能帮我弄清楚吗。任何帮助都将不胜感激。谢谢

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

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


如果您使用的是Jenkins管道,您可以在此处找到所有文档,或许您可以尝试步骤

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

参考:


我可以通过如下凭证

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

谢谢,但我不想推图像以记住,您不应该在Docker图像中存储凭据,因为每个人都可以在那里获取凭据。凭据通常作为环境变量传递给Docker容器。您的cdkimage内容是什么?(假设这是dockerfile)