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

419 Views Asked by At

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/[email protected]
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!

1

There are 1 best solutions below

0
On BEST ANSWER

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

version: 2.1
orbs:
  aws-ecr: circleci/[email protected]
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