Home > Back-end >  Unable to change a custom workflow environment variable in a job step
Unable to change a custom workflow environment variable in a job step

Time:03-07

I set an environment variable for my workspace like so :

env:
  DATA_BRANCH: master

and then I would like to change it in one of the job steps (for macos)

echo {DATA_BRANCH=osx} >> $GITHUB_ENV

but this does not change the variable.

My complete yml file is:

https://github.com/GrokImageCompression/grok/blob/master/.github/workflows/build.yml

CodePudding user response:

According to the official documentation:

The scope of a custom environment variable is limited to the element in which it is defined.

Which means that if you set a env variable at the workflow level, at the job level, or at the step level. The most specific one will always prevails.

In your case, you set at the workflow level:

env:
  DATA_BRANCH: master

If you want a specific step (or job) to use another value, you just need to set the env variable at that level with the other value.

In your case, it would look like this:

  steps:
    ...
    - name: macos-dependencies
      if: startsWith(matrix.os, 'macos')
      run: |
        ...
      env:
        DATA_BRANCH: osx

It's also worth remembering that using echo name=value >> $GITHUB_ENV won't add the env variable to the current step, but only to the following ones (reference):

The step that creates or updates the environment variable does not have access to the new value, but all subsequent steps in a job will have access.

CodePudding user response:

The problem was a syntax error

echo DATA_BRANCH=osx >> $GITHUB_ENV is the proper syntax - no curly braces required.

  • Related