dockeramazon-ecscirclecicircleci-workflows

How to make Circle CI upload and deploy an image on AWS ECS only after tests are success?


I deploy my Node.Js app via Circle CI to AWS ECS.

I would like first the tests to run, and only then to push the image to the repo. However, currently the task of running the tests runs at the same time as the AWS ECS task. Below is my ./circleci/config.yml file.

How do I change this behavior, so that the image is pushed only if the tests were successful?

version: 2.1
orbs:
  aws-ecr: circleci/aws-ecr@6.10.0
jobs:
  test:
    docker:
      - image: cypress/base:12.18.0
    steps:
      - checkout
      - run: npm ci
      - run: npm run cy:verify
      # save npm dependencies and Cypress binary for future runs
      - save_cache:
          key: cache-{{ checksum "package.json" }}
          paths:
            - ~/.npm
            - ~/.cache
      - run: npm run test:ci

workflows:
  version: 2.1
  test:
    jobs:
      - test
      - aws-ecr/build-and-push-image:
          create-repo: true
          no-output-timeout: 10m
          repo: 'stage-instance'

Thank you!


Solution

  • Add a requires step in workflow to filter based on condition.

    version: 2.1
    orbs:
      aws-ecr: circleci/aws-ecr@6.10.0
    jobs:
      test:
        docker:
          - image: cypress/base:12.18.0
        steps:
          - checkout
          - run: npm ci
          - run: npm run cy:verify
          # save npm dependencies and Cypress binary for future runs
          - save_cache:
              key: cache-{{ checksum "package.json" }}
              paths:
                - ~/.npm
                - ~/.cache
          - run: npm run test:ci
    
    workflows:
      version: 2.1
      test:
        jobs:
          - test
          - aws-ecr/build-and-push-image:
              create-repo: true
              no-output-timeout: 10m
              repo: 'stage-instance'
              requires:
                - test