Running Steps Conditionally in CircleCI Parallel Jobs


When using parallelism in CircleCI, you may encounter scenarios where you want certain steps to run only once instead of in each parallel job. This article guides you on how to execute a step in a single run within parallel jobs, using CircleCI's environment variables.


  • A CircleCI account and a project set up for CI/CD.
  • A config.yml file configured for parallel job execution.


To run a step only once within parallel jobs, you can utilize the CIRCLE_NODE_INDEX environment variable provided by CircleCI. This variable indicates the index of the current container in a parallel job, starting from 0.

Here's how to conditionally run a step based on the CIRCLE_NODE_INDEX:

  1. Open your .circleci/config.yml file.
  2. Locate the job that contains the parallel steps.
  3. Add a conditional check in the command section of the step you want to run once.


Below is an example of how to use the CIRCLE_NODE_INDEX to run any command only in the first parallel job:

- run:
name: Run Your Command
command: |
if [ "${CIRCLE_NODE_INDEX}" -eq "0" ]; then
//code goes here

In this example, the your code will execute only if the CIRCLE_NODE_INDEX is equal to 0, meaning it will run only in the first of the parallel jobs.


Additional Resources

For more information on CircleCI environment variables and parallelism, you can refer to the following resources: - CircleCI Environment Variables Documentation - CircleCI Parallelism Guide

By leveraging the CIRCLE_NODE_INDEX, you can efficiently manage your parallel jobs and optimize your CI/CD pipeline in CircleCI.

Was this article helpful?
0 out of 0 found this helpful



Article is closed for comments.