I have setup an environment variable using:
echo "::set-env name=PROJECT_TO_TEST::$(sh scripts/test.sh)"
test.sh just echoes "SomeString, MoreString"
I do see PROJECT_TO_TEST variable is correctly getting set by doing:
echo "env.PROJECT_TO_TEST = ${{ env.PROJECT_TO_TEST }}"
Which outputs "env.PROJECT_TO_TEST = SomeString, MoreString"
In a step right after, I'm doing a if check to see the PROJECT_TO_TEST variable contains some strings like so:
- name: Conditionally Run
if: contains('${{ env.PROJECT_TO_TEST }}', 'SomeString')
run: |
echo "SomeString did exist and should run"
- name: Conditionally Run
if: contains('${{ env.PROJECT_TO_TEST }}', 'ShouldNotRun')
run: |
echo "ShouldNotRun"
In this case, only the "SomeString did exist and should run" should get printed, but "ShouldNotRun" is also getting printed.
Full code here: https://github.com/gomfucius/github-actions/blob/master/.github/workflows/pullrequest.yml
Workflow that illustrates the problem: https://github.com/gomfucius/github-actions/runs/590320131
You don't need ${{}}
inside if
, as it can access env
context directly.
- run: echo PROJECT_TO_TEST=SomeString,MoreString >> $GITHUB_ENV
- name: Conditionally Run
if: contains(env.PROJECT_TO_TEST, 'SomeString')
run: echo "SomeString did exist and should run"
- name: Conditionally Run
if: contains(env.PROJECT_TO_TEST, 'ShouldNotRun')
run: echo "ShouldNotRun"
Using "set-env" has been deprecated by Github.
The
set-env
command is disabled. Please upgrade to using Environment Files or opt into unsecure command execution by setting theACTIONS_ALLOW_UNSECURE_COMMANDS
environment variable totrue
. For more information see: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/
You should instead use the following syntax:
- name: set env variable
run: |
echo "PROJECT_TO_TEST=$(sh scripts/test.sh)" >> $GITHUB_ENV
# Then you can use it in your if statement like so
- name: Conditionally Run
if: contains(env.PROJECT_TO_TEST, 'SomeString')
run: |
echo "let's run"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With