Modify environment variable for entire workflow

Hello,

I have a workflow with 2 jobs. The first workflow must set an environment variable to some value, so that the value persists for access to the second job. But I could not figure out how to do that.

Here is an example of my situtation:

name: Play with environment variables
# Set an environment variable with initial value
env:
  TEST_RESULT: 'success'

on:
  push:

jobs:
  job-1:
    name: Change env variable
    runs-on: ubuntu-latest
    steps:
      - name: Print env variable
        run: echo 'TEST_RESULT is' $TEST_RESULT
        # prints 'TEST_RESULT is success'
      - name: Set env variable
        run: echo "::set-env name=TEST_RESULT::failure"
      - name: Print env variable
        run: echo 'TEST_RESULT is' $TEST_RESULT
        # prints 'TEST_RESULT is failure'

  job-2:
    name: Access modified env variable
    runs-on: ubuntu-latest
    needs: job-1
    steps:
      - name: Print env variable
        run: echo 'TEST_RESULT is' $TEST_RESULT
        # prints 'TEST_RESULT is success'

So, I initially specified an environment variable TEST_RESULT, and set its value to ‘success’.

Then, in the first job (job-1) I first printed TEST_RESULT and saw it was indeed ‘success’. I then changed the value of TEST_RESULT to ‘failure’, and additionally printed its value after modification - final result of TEST_RESULT in job-1 was ‘failure’.

Then a second job (job-2) started. I also looked at the value of TEST_RESULT there, but the value was the original ‘success’ value.

So, is the a possibility to persist values of environment variables, so that subsequent jobs have access to updated values and not the original value?

@alishstrong ,

You can use the outputs to pass the updated value of the environment variable from job-1 to job-2.

I have tested the following example, it can work fine as expected (see here):

env:
  TEST_RESULT: 'success'

jobs:
  job1:
    name: Change env variable

    # set job-level output and pass the value of step-level output to it
    outputs:
      test-result: ${{ steps.set_outputs.outputs.test-result }}

    runs-on: ubuntu-latest
    steps:
      - name: Print value of env variable
        run: echo "TEST_RESULT = $TEST_RESULT"

      - name: Change env variable
        run: echo "::set-env name=TEST_RESULT::failure"

      - name: Print new value of env variable
        run: echo "TEST_RESULT = $TEST_RESULT"

      # set step-level output and pass the value of env variable to it
      - name: set outputs
        id: set_outputs
        run: |
          echo "::set-output name=test-result::$TEST_RESULT"
  job2:
    name: Access modified env variable
    needs: [job1]
    runs-on: ubuntu-latest
    steps:
      - name: Print the output of job1
        run: echo "needs.job1.outputs.test-result = ${{ needs.job1.outputs.test-result }}"

      - name: Print value of env variable
        run: echo "TEST_RESULT = $TEST_RESULT"

      # use job1's output to update the env variable
      - name: Change env variable
        run: echo "::set-env name=TEST_RESULT::${{ needs.job1.outputs.test-result }}"

      - name: Print new value of env variable
        run: echo "TEST_RESULT = $TEST_RESULT"
1 Like