Git commands as if condition of a step in workflow

Hello, I want to only run the ‘Commit and Push’ step if there are some changes in the repo.
What I tried to do:

- name: Commit and Push
      if: 'git diff --exit-code' 
   run: | 
       git add -u
       git commit -m "github-actions added date data"
       git push --force https://${GITHUB_ACTOR}:${{secrets.GITHUB_TOKEN}}@github.com/${GITHUB_REPOSITORY}.git HEAD:${REMOTE_BRANCH} 

But it gives error (I tried to search online whether an git command can be used as condition or not, I know it works in linux terminal but as error suggests github action does not seem to support it):

Invalid workflow file 
The workflow is not valid. .github/workflows/main.yml (Line: 45, Col: 11): Unrecognized named-value: 'git'. Located at position 1 within expression: git diff --exit-code 

If this is not the correct way, please recommend any other option to achieve the same?

Thanks

if: is no shell, so no, running commands there is not supported. What you can do is set an environment variable or output variable in a previous step, and check that. Example with environment variable:

- name: check for changes
  run: |
    if git diff --exit-code; then
      echo "::set-env name=changes_exist::true"
    else
      echo "::set-env name=changes_exist::false"
    fi
- name: Commit and Push
  if: ${{ env.changes_exist == 'true' }}
  run: | 
    git add -u
    git commit -m "github-actions added date data"
    git push --force https://${GITHUB_ACTOR}:${{secrets.GITHUB_TOKEN}}@github.com/${GITHUB_REPOSITORY}.git HEAD:${REMOTE_BRANCH}

Also keep in mind that indentation is part of YAML syntax, uneven indentation like in your example might cause additional errors.

1 Like

Hi Luna,

Thanks for the approach. I tried but still getting error:

    - name: check for changes
      run: |
        if git diff --exit-code; then
            echo "::set-env name=changes_exist::true"
        else
            echo "::set-env name=changes_exist::false"   
        fi
        
    - name: Commit and Push Only if file updated
      if: env.changes_exist == 'true'
      run: |
        git add -u
        git commit -m "github-actions added date data"
        git push --force https://${GITHUB_ACTOR}:${{secrets.GITHUB_TOKEN}}@github.com/${GITHUB_REPOSITORY}.git HEAD:${REMOTE_BRANCH}

I tried many permutations and in one of those the step was actually skipped but job failed.

What am I missing here?

Thanks

1 Like

Sorry, I put true and false the wrong way around in my example. According to the documentation git diff --exit-code will return 0 if there are no changes, and in Bash 0 means true, contrary to C… :sweat_smile:

So the check should be:

- name: check for changes
  run: |
    if git diff --exit-code; then
      echo "::set-env name=changes_exist::false"
    else
      echo "::set-env name=changes_exist::true"
    fi

I hope this works, at least the mixup explains why you got “nothing to commit” in the first screenshot.

1 Like

No, dont be sorry. Programming with such things is confusing.

Thanks a lot, it worked.

image

1 Like