如何构建Docker镜像并使用CIRCLE 2.0将其推送到ECR?

时间:2017-12-18 09:36:46

标签: amazon-web-services docker circleci circleci-2.0

我试图从CIRCLE 1.0升级到2.0&我无法构建Docker镜像。我有以下工作:|

... There is another Job here which runs some tests
deploy-aws:
    # machine: true
    docker:
      - image: ecrurl/backend
        aws_auth:
              aws_access_key_id: ID1
              aws_secret_access_key: $ECR_AWS_SECRET_ACCESS_KEY  # or project UI envar reference
        environment:
            TAG: $CIRCLE_BRANCH-$CIRCLE_SHA1
            ECR_URL: ecrurl/backend
            DOCKER_IMAGE: $ECR_URL:$TAG
            STAGING_BUCKET: staging
            TESTING_BUCKET: testing
            PRODUCTION_BUCKET: production
            NPM_TOKEN: $NPM_TOKEN

    working_directory: ~/backend

    steps:
    - run:
        name: Install awscli
        command: sudo apt-get -y -qq install awscli

    - checkout

    - run:
        name: Build Docker image
        command: |
          if [ "${CIRCLE_BRANCH}" == "master" ]; then
            docker pull $ECR_URL:latest
            docker build -t backend NODE_ENV=$NODE_ENV  --build-arg NPM_TOKEN=$NPM_TOKEN .
            docker tag backend $DOCKER_IMAGE
            docker push $DOCKER_IMAGE
            docker tag -f $DOCKER_IMAGE $ECR_URL:latest
            docker push $ECR_URL:latest
          fi

workflows:
  version: 2
  build-deploy:
    jobs:
      - build # This one simply runs test
      - deploy-aws:
          requires:
            - build

运行此命令会引发以下错误:

#!/bin/bash -eo pipefail
sudo apt-get -y -qq install awscli
/bin/bash: sudo: command not found
Exited with code 127

我之前所做的一切都是:

dependencies:
  pre:
    - $(aws ecr get-login --region us-west-2)


deployment:
  staging:
    branch: staging
      - docker pull $ECR_URL:latest
      - docker build -t backend NODE_ENV=$NODE_ENV  --build-arg NPM_TOKEN=$NPM_TOKEN .
      - docker tag backend $DOCKER_IMAGE
      - docker push $DOCKER_IMAGE
      - docker tag -f $DOCKER_IMAGE $ECR_URL:latest
      - docker push $ECR_URL:latest

1 个答案:

答案 0 :(得分:1)

以下是我已更改的配置:

deploy-aws:
docker:
  - image: docker:17.05.0-ce-git

 steps:
  - checkout
  - setup_remote_docker
  - run:
      name: Install dependencies
      command: |
        apk add --no-cache \
          py-pip=9.0.0-r1
        pip install \
          docker-compose==1.12.0 \
          awscli==1.11.76

  - restore_cache:
      keys:
        - v1-{{ .Branch }}
      paths:
        - /caches/app.tar
  - run:
      name: Load Docker image layer cache
      command: |
        set +o pipefail
        docker load -i /caches/app.tar | true

  - run:
      name: Build Docker image
      command: |
        if [ "${CIRCLE_BRANCH}" == "master" ]; then
          docker build -t backend --build-arg  .
        fi
  - run:
      name: Save Docker image layer cache
      command: |
        mkdir -p /caches
        docker save -o /caches/app.tar app

  - save_cache:
        key: v1-{{ .Branch }}-{{ epoch }}
        paths:
          - /caches/app.tar
  - run:
      name: Tag and push to ECR
      command: |
        if [ "${CIRCLE_BRANCH}" == "master" ]; then
          docker tag backend $DOCKER_IMAGE
          docker push $DOCKER_IMAGE
          docker tag -f $DOCKER_IMAGE $ECR_URL:latest
          docker push $ECR_URL:latest
        fi

点击此链接:https://github.com/builtinnya/circleci-2.0-beta-docker-example/blob/master/.circleci/config.yml#L39