Skip to main content

Handling Exit Code 2 in CircleCI Orbs for Workflow Success

Overview

When integrating Terraform linting into CircleCI workflows using custom orbs, it's common to encounter scenarios where a specific exit code should not cause the workflow to fail. This article guides you through configuring a CircleCI job to pass even if a command within an orb exits with code 2, which typically indicates a failure.

Prerequisites

  • A CircleCI account and a project set up with a .circleci/config.yml file.

  • A custom Terraform orb or command that you want to modify.

  • Familiarity with shell scripting and CircleCI configuration syntax.

Instructions

To ensure that your workflow passes when the terraform/lint command exits with code 2, you can use a shell trap to catch and handle the exit code. Here's how to implement this:

  1. Define a trap function within your orb command to catch the exit code.

  2. Check if the exit code is 2, and if so, print a success message and exit with code 0.

  3. Ensure that the trap function is called upon the script's exit.

Solution

Modify your terraform/lint command to include a trap function like the one below:

markassuccess() {
  rv=$?
  if [ $rv -eq 2 ]; then
    echo "Linting issues detected, but marking the step as successful."
    exit 0
  else
    echo "Exiting with code $rv."
    exit $rv
  fi
}trap markassuccess EXIT

Place this function at the beginning of your terraform/lint command script. This way, if tflint exits with code 2, the trap will catch it, and the job will exit successfully, allowing the workflow to continue.

Additional Resources

By implementing the above solution, you can control the outcome of your workflows more precisely, ensuring that specific exit codes do not inadvertently cause your builds to fail.

Did this answer your question?