Issue in fetching updated environment variable value from Context

Hello Team, By following How to insert files as environment variables with Base64 – CircleCI Support Center, I am able to store data.

I have updated the context environment variable(USER_NAME) using “CircleCI API”. After successful updation, in the shell USER_NAME value still shows old value not updated value. kindly suggest.

What am i supposed to do to access updated value of USER_NAME?

version: 2.1

  version: 2
      - build-and-test:
          context: ABC_CONT

      - image: cimg/python:3.9.4
      - run: 
            name: Data Update
            command: |
                 echo "$USER_NAME"    #actual user name: actUser123        
                 curl \
                 --request PUT \
                 --url \
                 --header 'content-type: application/json' \
                 -u "$CIRCLE_API_USER_TOKEN:" \
                 --data '{"value":"modUser123"}'
			      echo "$USER_NAME"    #I am expecting "modUser123" but returns actUser123
      - run:
            name: Access updated data
            command: |
                  echo "$USER_NAME"    #I am expecting "modUser123" but returns actUser123
1 Like

Hi @ramakrishnareddysatt,

Thank you for your question!

When using a context within a CircleCI config file, CircleCI will pull in those variables when the container is created. You can see this happening by looking at the Preparing environment variables step of any job.

Due to the environment variables being set at the beginning of a job, they are not automatically updated when changing the values stored in a context. The new value would be reflected in the next job you run with the same context.

One thing you could try is to overwrite the value of the environment variable locally after making the API call to update the value stored in the context.

If you would like to use the local environment variable in a different step than the one it was updated, you may need to run a command like this using BASH_ENV after making the API call:

echo 'export USER_NAME=modUser123' >> $BASH_ENV

This would make the updated value available in every step following the one where the value was changed.

Also, I would like to suggest running the API calls locally as well to test if they are successful first or not. This would be to eliminate any possible errors such as using the wrong variable for your API token, or syntax errors, etc.

Let me know if this points you in the right direction, and please feel free to respond with any additional questions!