Looping through environments in circleci config.yml

Hello,

I have terraform files for development and staging environments. The formats are as follow:
terraform/env-dev/ : for development environment
terraform/env-dev/ : for staging environment

Here is my config.yml file:

version: 2.1

jobs:
  mdl:
    docker:
      - image: rsrchboy/mdl
    steps:
      - checkout
      - run:
          name: Validate Markdown
          command: |
            mdl $(find . -type f -name '*.md')

  terraform:
    parameters:
      envs: 
        description: The target environment must be one of "env-dev", "env-staging".
        default: "env-dev"
        type: enum
        enum: ["env-dev", "env-staging"]
    docker:
      - image: hashicorp/terraform:0.12.31
    steps:
      - checkout
      - run:
          name: Create a .key directory for gcp credentials and configure environmental variables.
          command: |
            for i in <<parameters.envs>>; do
              if [ $i == "env-dev" ]
              then
                cd terraform/$i
                mkdir .keys/ 
                echo $ACCOUNT > .keys/account.json
                echo $TOKEN > .keys/token 
                export AWS_ACCESS_KEY_ID=`echo $AWS_ACCESS_KEY_ID`
                export $AWS_SECRET_ACCESS_KEY=`echo $AWS_SECRET_ACCESS_KEY`
       
              elif [ $i == "env-staging" ] 
              then
                cd terraform/$i
                mkdir .keys/ 
                echo $ACCOUNT_STAGING > .keys/account.json
                echo $TOKEN > .keys/token 
                export AWS_ACCESS_KEY_ID=`echo $AWS_ACCESS_KEY_ID`
                export $AWS_SECRET_ACCESS_KEY=`echo $AWS_SECRET_ACCESS_KEY`   
              else
                echo "Something is wrong".
              fi   
            done

      - run:
          name: Validate and Plan terraform code
          command: |
            printf "Validating and Planning terraform code\n"
            for i in <<parameters.envs>>; do
               cd terraform/$i && terraform init -input=false -backend=true && (terraform validate -json=false && echo "√ passed") && \
               (terraform plan -out=tfplan -target module.gcp_kms ) || exit 1
            done

workflows:
  version: 2
  build:
    jobs:
      - mdl
      - terraform

What I want to achieve is for CircleCI to loop through each of the env-dev and env-staging environments and run these terraform commands. However, this only appear to work successfully for env-dev environment only leaving out env-staging.

It seems like the << parameters.envs >> isn’t doing exactly what I intend to achieve. What is the best way to carry out this process. parameter of type list is not supported at this time.

Thank you for your help guys!

Anyone can help here?

After many trials and errors, I found a combination that worked for me. After cd into terraform/env-dev directory and running terraform commands, CircleCI needs to cd one step back into terraform directory before it can cd again into env-staging directory. This solves the problem env-staging being not looped into.

Finally, the <<parameter.evns>> of type string still allows for the looping to be successful after spending hours trying to get enum to work.

Thank you every viewer. Sorry for being silly with my questions. I am barely one week old with CircleCI.

1 Like

@barywhyte Thank you for posting what worked for you! There are no silly questions, feel free to ask whatever is on your mind!