Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Actions If contains function not working with env.VARIABLE

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

like image 512
Genki Avatar asked Apr 15 '20 21:04

Genki


2 Answers

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"
like image 185
Samira Avatar answered Oct 19 '22 04:10

Samira


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 the ACTIONS_ALLOW_UNSECURE_COMMANDS environment variable to true. 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"
like image 22
Nicolas Oste Avatar answered Oct 19 '22 04:10

Nicolas Oste