0

I have a CI/CD pipeline in GitHub Actions that runs and one of the steps is to commit to another repository. What it does is that it clones to external repository, moves files into it, and then commits it back to the external repository.

The thing is, there is no grantee that there will be a new file to commit.

When that happens, the repository fails because Git throws an error as shown

enter image description here

How can I get around that?

SopMe
  • 57
  • 4
  • Not the best thing to do but you could try `git commit blahblah || echo Probably there was nothing there to commit`. That way, if commit fails, the command as a whole will still succeed. – eftshift0 Oct 21 '22 at 19:04
  • @eftshift0 Can be just a no-op: `git commit || :` The problem with the approach is: it ignores any error, not only "nothing to commit". – phd Oct 22 '22 at 06:53

1 Answers1

3

You could use the git status --porcelain command (reference 1 + reference 2) to check if some changes occurred.

It could look like this using bash:

      run:
        if [[ `git status --porcelain` ]]; then
          echo "OK: Changes detected."
        else
          echo "WARNING: No changes were detected."
        fi
      shell: bash

Obs: I'm using it in a action to git commit push changes.

GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
  • Tried it and it failed. This was the output from GH Actions: https://pastebin.com/raw/Zf1ad8Jw This was my action script: https://pastebin.com/raw/7GsuC6Ap [sorry, cant paste code in comments] – SopMe Oct 21 '22 at 18:47
  • 1
    I made a test [here](https://github.com/GuillaumeFalourd/poc-github-actions/actions/runs/3299962768) with [2 jobs using this implementation](https://github.com/GuillaumeFalourd/poc-github-actions/blob/main/.github/workflows/workflow-tester66.yml), and it worked as expected: changes detected for the first one, and WARNING for the second one. Are you using the `actions/checkout` in the workflow as well? – GuiFalourd Oct 21 '22 at 19:33
  • 1
    @SopMe: your script runs `git status --porcelain` in one repo, then `cd /tmp/other-repo && various-git-commands-here`, so you're testing one repo and committing in another repository entirely. Perhaps you should do your testing where you plan to do your committing... – torek Oct 22 '22 at 05:37