Constructing environment variables using command parameters

I have a command that looks something like this.

run-command:
  parameters:
    NUM:
      type: string
  steps:
    - checkout
    - run:
        command: |
          echo "export MY_ENV=MY_<< parameters.NUM >>_ENV" >> $BASH_ENV
          source $BASH_ENV
          echo $MY_ENV

I would like to set the MY_ENV environment variable using the passed in parameters as shown above. So if NUM = 15, then MY_ENV would end up being the value of MY_15_ENV, which I’ve set in the environment variables page.

Regardless of all of the iterations that I’ve tried, I always receive this error.

Unclosed '<<' tag in string: '<< parameters'

How do I do this correctly?

How are you trying to trigger this? Are you trying to pass pipeline parameters when triggering through the API? You need to add parameters using the top level parameters key. Then you can use the example in the docs to trigger jobs and pass data into the defined parameters.

Here’s your code working as a basic command as well.

version: 2.1

commands:
  run-command:
    parameters:
      NUM:
        type: string
    steps:
      - checkout
      - run:
          command: |
            echo "export MY_ENV=MY_<< parameters.NUM >>_ENV" >> $BASH_ENV
            source $BASH_ENV 
            echo $MY_ENV

jobs:
    build:
        docker:
            - image: circleci/node
        steps:
            - checkout
            - run-command:
                NUM: "15"

It might be helpful to share more context or config.yml

You’re right. This works out of the box. My error came from a completely unrelated line of code. Thank you for the response.

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