Configuring bitbucket pipelines with Docker to connect to AWS

牧云@^-^@ 提交于 2020-01-02 06:40:34

问题


I am trying to set up Bitbucket pipelines to deploy to ECS as here: https://confluence.atlassian.com/bitbucket/deploy-to-amazon-ecs-892623902.html

These instructions say how to push to Docker hub, but I want to push the image to Amazon's image repo. I have set AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID in my Bitbucket parameters list and I can run these command locally with no problems (the keys defined in ~/.aws/credentials). However, I keep getting the error 'no basic auth credentials'. I am wondering if it is not recognising the variables somehow. The docs here: http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html say that:

The AWS CLI uses a provider chain to look for AWS credentials in a number of different places, including system or user environment variables and local AWS configuration files. So I am not sure why it isn't working. My bitbucket pipelines configuration is as so (I have not included anything unnecessary):

      - export IMAGE_NAME=$AWS_REPO_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my/repo-name:$BITBUCKET_COMMIT
      # build the Docker image (this will use the Dockerfile in the root of the repo)
      - docker build -t $IMAGE_NAME .
      # authenticate with the AWS repo (this gets and runs the docker login command)
      - eval $(aws ecr get-login --region $AWS_DEFAULT_REGION)
      # push the new Docker image to the repo
      - docker push $IMAGE_NAME

Is there a way of specifying the credentials for aws ecr get-login to use? I even tried this, but it doesn't work:

      - mkdir -p ~/.aws
      - echo -e "[default]\n" > ~/.aws/credentials
      - echo -e "aws_access_key_id = $AWS_ACCESS_KEY_ID\n" >> ~/.aws/credentials
      - echo -e "aws_secret_access_key = $AWS_SECRET_ACCESS_KEY\n" >> ~/.aws/credentials

Thanks


回答1:


Try this:

bitbucket-pipeline.yml

pipelines:
  custom:
    example-image-builder:
      - step:
          image: python:3
          script:
            - export CLONE_ROOT=${BITBUCKET_CLONE_DIR}/../example
            - export IMAGE_LOCATION=<ENTER IMAGE LOCATION HERE>
            - export BUILD_CONTEXT=${BITBUCKET_CLONE_DIR}/build/example-image-builder/dockerfile
            - pip install awscli
            - aws s3 cp s3://example-deployment-bucket/deploy-keys/bitbucket-read-key .
            - chmod 0400 bitbucket-read-key
            - ssh-agent bash -c 'ssh-add bitbucket-read-key; git clone --depth 1 git@bitbucket.org:example.git -b master ${CLONE_ROOT}'
            - cp ${CLONE_ROOT}/requirements.txt ${BUILD_CONTEXT}/requirements.txt
            - eval $(aws ecr get-login --region us-east-1 --no-include-email)
            - docker build --no-cache --file=${BUILD_CONTEXT}/dockerfile --build-arg AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} --build-arg AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} --tag=${IMAGE_LOCATION} ${BUILD_CONTEXT}
            - docker push ${IMAGE_LOCATION}

options:
  docker: true

dockerfile

FROM python:3
MAINTAINER Me <me@me.me>
COPY requirements.txt requirements.txt
ENV DEBIAN_FRONTEND noninteractive
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
RUN apt-get update && apt-get -y install stuff
ENTRYPOINT ["/bin/bash"]

I am running out of time, so for now I included more than just the answer to your question. But this would be a good enough template to work from. Ask questions in the comments if there is any line you don't understand and I will edit the answer.




回答2:


i had the same issue. the error is mainly due to an old version of awscli. you need to use a docker image with a more recent awscli. for my project i use linkmobility/maven-awscli

  1. You need to set the Environnment variables in Bitbucket

  2. small changes to your pipeline

image: Docker-Image-With-awscli

  • eval $(aws ecr get-login --no-include-email --region ${AWS_DEFAULT_REGION} )


来源:https://stackoverflow.com/questions/44380130/configuring-bitbucket-pipelines-with-docker-to-connect-to-aws

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!