# An opinionated version of a Terraform pipeline.
# This is for a simple scenario when we need to run terraform plan/apply without any others commands.

# The "plan" is run for each branch and for each environment except branch hotfix/* dedicated for prod.
# The "apply" is only manually run on branch develop for dev, default branch for stg/prd and hotfix/* for prd.

# If you want to auto apply, set the variables AUTO_APPLY_DEV, AUTO_APPLY_STG or AUTO_APPLY_PRD to "true".

include:
  - local: functions/terraform.yml
  - local: workflows/branch-pipelines.yml

stages:
  # A list of generic stages for our different environments
  - init-validate
  - before-plan-dev
  - plan-dev
  - apply-dev
  - after-apply-dev
  - before-plan-stg
  - plan-stg
  - apply-stg
  - after-apply-stg
  - before-plan-prd
  - plan-prd
  - apply-prd
  - after-apply-prd

init-validate:
  stage: init-validate
  extends:
    - .init-validate

# DEV #########################################################################
plan-dev:
  # Run terraform plan on dev
  stage: plan-dev
  extends:
    - .plan-dev
  rules:
    - if: '$DISABLE_DEV == "true"'
      when: never
    - if: '$CI_COMMIT_BRANCH =~ /^hotfix\// || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: never # do not run this on hotfix and default branch
    - when: on_success
  needs:
    - job: init-validate

apply-dev:
  # Run terraform apply on dev only on branch develop
  stage: apply-dev
  extends:
    - .apply-dev
  rules:
    - if: '$DISABLE_DEV == "true"'
      when: never
    - if: '$AUTO_APPLY_DEV == "true" && $CI_COMMIT_BRANCH == "develop"'
    - if: '$CI_COMMIT_BRANCH == "develop"'
      when: manual
  needs:
    - job: init-validate
    - job: plan-dev

# STG #########################################################################
plan-stg:
  # Run terraform plan on stg
  stage: plan-stg
  extends:
    - .plan-stg
  rules:
    - if: '$DISABLE_STG == "true"'
      when: never
    - if: '$CI_COMMIT_BRANCH =~ /^hotfix\//'
      when: never # do not run this on hotfix
    - when: on_success
  needs:
    - job: init-validate

apply-stg:
  # Run terraform apply on stg only on default branch
  stage: apply-stg
  extends:
    - .apply-stg
  rules:
    - if: '$DISABLE_STG == "true"'
      when: never
    - if: '$AUTO_APPLY_STG == "true" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: manual
  needs:
    - job: init-validate
    - job: plan-stg

# MCO #########################################################################
plan-mco:
  stage: plan-prd
  extends:
    - .plan-mco
  rules:
    - if: '$ENABLE_ADDITIONAL_ENV_MCO == "true"'
      when: on_success
    - when: never
  needs:
    - job: init-validate

apply-mco:
  # Run terraform apply on branch default branch and hotfix/* in the same stage than prd
  stage: apply-prd
  extends:
    - .apply-mco
  rules:
    - if: '$ENABLE_ADDITIONAL_ENV_MCO != "true" && $DISABLE_PRD != "true"'
      when: never
    - if: '$ENABLE_ADDITIONAL_ENV_MCO == "true" && $AUTO_APPLY_PRD == "true" && ($CI_COMMIT_BRANCH =~ /^hotfix\// || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH)'
    - if: '$ENABLE_ADDITIONAL_ENV_MCO == "true" && ($CI_COMMIT_BRANCH =~ /^hotfix\// || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH)'
      when: manual
  needs:
    - job: init-validate
    - job: plan-mco

# PRD #########################################################################
plan-prd:
  # Run terraform plan on prd
  stage: plan-prd
  extends:
    - .plan-prd
  rules:
    - if: '$DISABLE_PRD == "true"'
      when: never
    - when: on_success
  needs:
    - job: init-validate

apply-prd:
  # Run terraform apply on prd only on default branch and hotfix/*
  stage: apply-prd
  extends:
    - .apply-prd
  rules:
    - if: '$DISABLE_PRD == "true"'
      when: never
    - if: '$AUTO_APPLY_PRD == "true" && ($CI_COMMIT_BRANCH =~ /^hotfix\// || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH)'
    - if: '$CI_COMMIT_BRANCH =~ /^hotfix\// || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: manual
  needs:
    - job: init-validate
    - job: plan-prd
