CircleCI Workflows

Workflows help you increase the speed of your software development through faster feedback, shorter reruns, and more efficient use of resources. CircleCI Workflows are a set of rules for defining a collection of jobs and their run order. Workflows support complex job orchestration using a simple set of configuration keys to help you resolve failures sooner.

With workflows, you can:

  • Run and troubleshoot jobs independently with real-time status feedback.
  • Schedule workflows for jobs that should only run periodically.
  • Fan-out to run multiple jobs concurrently for efficient version testing.
  • Fan-in to quickly deploy to multiple platforms.

For example, if only one job in a workflow fails, you will know it is failing in real-time. Instead of wasting time waiting for the entire build to fail and rerunning the entire job set, you can rerun just the failed job.

Build a Workflow

Now that you’ve built all the jobs this workshop pipeline needs, its time to orchestrate how and when those jobs are executed. This will require you to define a Workflow.

Copy the snippet below and append it to the bottom of your config.yml file:

      - run_tests
      - scan_app
      - build_docker_image
      - deploy_aws_ecs:
            - build_docker_image          
      - smoketest_deployment:
            - deploy_aws_ecs
      - approve_destroy:
          type: approval
            - smoketest_deployment
      - destroy_aws_ecs:
            - approve_destroy

workflows: build: block defines a CircleCI workflow that will define the jobs to execute and how they’re executed. The scan_deploy is the an identifier/name for the workflow. A CircleCI pipeline can execute multiple workflows but in this workshop you define only one.

jobs: this key defines the list of jobs that will be executed in this pipeline. The jobs listed in this workflow are the jobs you defined throughout this workshop, with the exception of the - approve_destroy: job.

Since you defined the jobs listed in this workflow, we’ll discuss the requires: and type: keys.

requires: key

Workflows define a list of jobs and their run order. It is possible to run jobs concurrently, sequentially, on a schedule, or with a manual gate using an approval job. Jobs are run in parallel by default, so you must explicitly require any dependencies by their job name. The requires: key is what creates dependencies between jobs. In the workflow snippet above, there are jobs that must be execute before others. For example the Docker image must be created and pushed to Docker Hub before the Amazon ECS service can pull and deploy it.

type: approval

A job may have a type of approval indicating it must be manually approved before downstream jobs may proceed. Jobs run in the dependency order until the workflow processes a job with the type: approval key followed by a job on which it depends. For example, the approve_destroy job listed in the workflow, stops the pipeline from continuing until a manual action occurs, which in CircleCI, requires an authorized user to click the Approve button in the CircleCI Dashboard.

Congratulations! You created a workflow for your pipeline. At this point your CI/CD pipeline is complete and can be triggered or executed.

In the next section you will learn how to trigger this pipeline that you’ve defined in the config.yml file. Jump over to the next section and learn how to trigger your pipeline.