Terraform workflow with conditionals

I’m creating a workflow that deploys changes to the infrastructure, the infra has 2 envs on the same repo (test-prod), so it init-plan both envs and applies the changes. What I want to achieve is that if in some of the envs there is no plan it can just skip the approval/apply steps,

   init_plan_test-prod -->  (when, condition: env plan produces changes) --> env approval_job --> env deploy_job
                   L(when, condition: plan no changes) --> exit

Someone know a way of doing this? I’ve used conditionals in the past, but not sure how to use them to read plan changes and apply accordingly.

I would use a pipeline parameter, then write your logic in bash to call another workflow containing the approval and deploy job.

Example:

parameters:
  run-deploy-prod:
    type: boolean
    default: false
    description: plan-prod workflow can set this to true to run the deploy-prod workflow.

jobs:
  init_plan_test-prod:
    steps:
      - init
      - plan
      - test
      - run:
          name: Determine if a deploy is needed
          command: |
            if [[ ... ]]; then
              printf "\nTriggering deploy-prod workflow\n"
              curl -X POST -u ${CIRCLECI_API_KEY}: \
                --url https://circleci.com/api/v2/project/gh/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pipeline \
                --header "Content-Type: application/json" \
                --data '{ "branch": "${CIRCLE_BRANCH}", "parameters": { "run-deploy-prod": true } }'
            fi

workflows:
  # Triggers unless pipeline.parameters.run-deploy-prod is true
  plan-prod:
    unless: << pipeline.parameters.run-deploy-prod >>
    jobs:
      - init_plan_test-prod

  # Triggered by the plan-prod workflow
  deploy-prod:
    when: << pipeline.parameters.run-deploy-prod >>
    jobs:
      - approval_job:
          type: approval
      - deploy_job:
          requires:
            - approval_job

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.