Environment variable empty in one step, but populated in the previous

I’m very new to CircleCI, and have inherited a partially completed setup that has some shortcomings. As of now, I am trying to start a PowerShell command with different parameters based on the branch name. Problem is that the environment variable seems to be empty in the PowerShell step. I have a debug-step above where I verify that the variable is set, and in that step the variable is populated as expected.
This is a slightly simplified version of my pipeline. What am I doing wrong?

      image: windows-default
    resource_class: customer_name/cicd02
      APP_SERVER01: ""
      - run:
          name: Set AppServers
          command: |
                  if [ "$CIRCLE_BRANCH" == "dev" ]; then
                    echo "export APP_SERVER01=devServer" >> $BASH_ENV
                  elif [ "$CIRCLE_BRANCH" == "test" ]; then
                    echo "export APP_SERVER01=testServer" >> $BASH_ENV
                  elif [ "$CIRCLE_BRANCH" == "prod" ]; then
                    echo "export APP_SERVER01=prodServer" >> $BASH_ENV
      - run:
          name: Verify string interpolation etc
          command: echo "Value of AppServer01 is $APP_SERVER01" # This prints correct value

      - run: 
          name: Run pre-deploy
          shell: powershell.exe
          command: |
                  $App_Server01 = $env:APP_SERVER01;
                  Write-Host "App_Server01: $App_Server01, env:APP_SERVER01: $env:APP_SERVER01" # This prints empty values

Any help would be appreciated, Chat GPT has already given up, and as far as I can tell from the documentation this should work. Unless this specific case is not supprted for some reason…

Do you mean the CIRCLE_BRANCH environment variable?

If so you can check to see if it was set at all by looking at the “Preparing environment variables” output screen in the STEPS section of the pipeline output within the web GUI.

One thing this variable will not be set if the pipeline was started by a tag being set on the repo, instead, you will end up with a CIRCLE_TAG environment variable being set with the tag value.

No, CIRCLE_BRANCH is populated, though I had not thought about potential other ways of triggering the pipeline. Thanks for that headsup :slight_smile: The variable that is not populated is APP_SERVER01. I have tried to illustrate when it is populated in the code snippet.

OK, I had to ask as I at least had a clear answer for that type of issue.

I’m not a user of PowerShell in any environment, so I can not be certain about its features, does it support the idea of $BASH_ENV?

  • While you have not specified a shell for your first run command the default is going to be PowerShell, according to this

So you are using a bash type export between shell instances, but the shell you are running in the next step is again PowerShell.

I was able to find a workaround by moving the if/else statements into the “Run pre-deploy” step and hard coding the values for APP_SERVER01 into the statements. However, this is a pretty messy way of doing things, especially since I have more steps where I want to use the same environment variable and several variables (the code snippet is just for illustration).
Leaving this question open