CirclCI Pipeline Set a Variable within a Job and read it from Other with Condition evaluated with empty

Viewed 21

Please I'm trying to run some steps in the CircleCI Pipeline with conditions happened in the previous step. I tried a lot of tricks like exposing the value from Step 1 to global vars and pickup it in Step 2, I can see and print the variables in Step 2 but using WHEN BLOCK forever evaluated with Empty. I searched a lot and I knew that logical conditions already evaluated before running the jobs, Please I need alternative way to execute steps in second job in case a condition happened in Step 1?

I pasted here the example that I'm trying to fix

version: 2.1

orbs:

workflows:
  test-and-deploy:
    jobs:
      - set-data:
          context: my-context
      - read-data:
          context: my-context
          requires:
            - set-data      
   
definitions:
  node_image: &node-image
    docker:
      - image: cimg/node:14.15.5

executors:
  base-12-14-0:
    description: |
      Single Docker container with Node 12.14.0 and Cypress dependencies
      see https://github.com/cypress-io/cypress-docker-images/tree/master/base.
      Use example: `executor: cypress/base-12-14-0`.
    docker:
      - image: cypress/base:12.14.0

jobs:
  set-data:
    <<: *node-image
    description: Sets the data
    steps:
      - run: echo "VAR=app" > global-vars
      - persist_to_workspace: 
          root: .
          paths:
            - global-vars
  read-data: 
    <<: *node-image
    description: read the data
    steps:   
      - attach_workspace: 
          at: .
      - run: ls 
      - run: cat global-vars // I COULD HERE SEE THE CORRECT VAR inside global-vars
      - run: cat global-vars >> $BASH_ENV
      - run: echo "Test $VAR"  // Successfully Printed
      - when: 
          condition: 
            matches: {
              pattern: "app",
              value: $VAR
            }
          steps:
            - run: echo "Condition Executed"
1 Answers

It's not possible to use environment variables in logic statements. The reason is that logic statements are evaluated at configuration compilation time, whereas environment variables are interpolated at run time.

The only workaround I know of is to use the CircleCI dynamic configuration functionality to set pipeline parameters' values in the "setup workflow" that you then pass to the "continuation" workflow.

And by the way, you're not using $BASH_ENV correctly (https://circleci.com/docs/env-vars#setting-an-environment-variable-in-a-shell-command). But again, even if you did, you wouldn't be able to use an environment variable in a logic statement.

Related