Circleci orb with array parameters and for loop

Hi,
I need to use a for loop in run command that goes through the fields of an array that is given as parameters and run the command. I’m wondering how that can be done in circleci orb?
I couldn’t find this in documentations. Here is what I need:

orb.yml

commands:
  terraformvalidation:
    description: "Validating terraform"
    parameters:
      envs:
        description: project environments
        default: [dev, test, stage, prod]
        type: list
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Validate terraform code
          command: |
            printf "Validating terraform code\n"
            cd ~/repo/terraform
            for $i in [<<parameters.envs>>]; do
               (cd $i && terraform init -backend=false && terraform validate)
            done

Any help would be appreciated.

Hi @mf69,

At the moment we don’t have a list type parameter, just string, enum, boolean, and integer. We have a bit more information in our “Reusable Config Reference Guide”.

With that said, I think you can still accomplish what you are looking for by using a string parameter. I setup a quick config to demonstrate this:

version: 2.1

jobs:
  build:
    parameters:
      envs:
        description: project environments
        default: "dev test stage prod"
        type: string
    docker:
      - image: circleci/node
    steps:
      - checkout
      - run:
          name: Validate terraform code
          command: |
            printf "Validating terraform code\n"
            for i in <<parameters.envs>>; do
               echo "test: $i"
            done
workflows:
  build-deploy:
    jobs:
      - build

Which results in:

Screen Shot 2020-08-06 at 9.05.05 AM

Hope the above helps clarify and if you need anything else please let me know!

3 Likes

Hi @nbialostosky thanks for your reply. I have tested it in my code but it didn’t work. Here is what I have tried is:

myorb.yml:

version: 2.1
description: my orb

commands:
  terraformvalidation:
    description: "Validating terraform"
    parameters:
      envs:
        description: project environments
        default: ${envs}
        type: string
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Validate terraform code
          command: |
            printf "Validating terraform code\n"
            cd ~/repo/terraform
            for i in <<parameters.envs>>; do
              printf "env: $i"
              cd $i && terraform init -backend=false && terraform validate
            done
executors:
  tf:
    description: Using a defined Terraform image
    parameters:
      terraformimage:
        type: string
        default: ${terraformimage}
        description: Terraform Image
    docker:
      - image: << parameters.terraformimage >>
    working_directory: /tmp/workspace
jobs:
  validate-terraform:
    parameters:
      terraformimage:
        type: string
        default: ${terraformimage}
        description: Terraform image
      envs:
        description: project environments
        default: ${envs}
        type: string  
    executor:
      name: tf
      terraformimage: << parameters.terraformimage >>
    steps:
    - when:
        condition: << parameters.terraformimage >>
        steps:
          - terraformvalidation:
            envs: <<parameters.envs>>

config.yml:

version: 2.1
orbs:
  myorb: mynamespace/myorb@dev:alpha6
workflows:
  build:
    jobs:
      - myorb/validate-terraform:
          terraformimage: myimage
          envs: "dev, test, stage, prod"
          filters:
            branches:
              only: master

I’m wondering what I am missing in my code?

Hi @mf69, thanks for the reply and providing such helpful details!

I did a bit of testing and I think its actually just a matter of formatting. In your Orb can you update this part in the job:

    steps:
    - when:
        condition: << parameters.terraformimage >>
        steps:
          - terraformvalidation:
            envs: <<parameters.envs>>

To this:

    steps:
    - when:
        condition: << parameters.terraformimage >>
        steps:
          - terraformvalidation:
              envs: <<parameters.envs>>

Essentially the envs param you are passing to terraformvalidation needs to be indented a bit. Can you give that a try and let me know if that works?

1 Like

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