Checking git status as condition of step

Hi there,

I’m writing a Github Action which runs some linting and then should submit a pull request back to the original branch.

One thing I’m wanting to do as part of this, is have a check/condition on the ‘open pull request’ step to check the git status of the checked out version of the repo.

I know you can do something like this but I’m not sure how I’d get the git status in there.

if: "contains(github.event.head_commit.message, '[build]')"

Alternatively, is there a way I can check if the output of a specific step contains a certain string. As then I could check the lint tool to check for a word like ‘Success’.

Thanks

What you have under the if key there is a GitHub Actions expression. It’s not a full-blown scripting language or anything like that. It does not allow you to execute commands for instance. But you can run shell code in a step, let it set an output and then refer to that output in another step’s if condition like so:

- name: Check Git status
  id: git
  shell: bash
  run: |
    RESULT=$(git status)
    echo "::set-output name=status::$RESULT"
- name: Other task
  if: contains(steps.git.outputs.result, '...')
  ...

I’m not sure if multiple lines returned by Git status could cause problems here. It might be better to check if a certain word is contained in the result in the shell code and then set the output to true and use if: steps.git.outputs.result == 'true' as condition instead:

- name: Check Git status
  id: git
  shell: bash
  run: |
    RESULT=$(git status)
    if [[ $RESULT == *"foobar"* ]]; then
      echo "::set-output name=status::true"
    fi
- name: Other task
  if: contains(steps.git.outputs.result, 'true')
  ...