Search code examples
githubgithub-actionsgit-commit

Github Actions: Cache Error and end action without fail message


I have a github action that transforms my Readme from one format to the other and which will then push the new Readme to the repository. For the pushing I have defined this job:

  push_readme:
    name: Push new Readme
    needs: generate_readme
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Download readme result from job 1 generate_readme
        uses: actions/download-artifact@v3
        with:
          name: readme
      - name: Commit files
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git status
          git add READMEmd.md
          git commit -m "Actions Generated Readme"
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

The commit returns an error when there is nothing to commit - which happens whenever the readme was not updated in the recent push. This is expected and fine. However, I would like to handle this error properly s.t. the action simply ends when it occurs WITHOUT telling me it failed. Instead I'd like something in the sense of "There is no new README to commit. Ending the action".

Could anyone point me to how to do that? I failed to find the solution yet.


Solution

  • You can utilize Bash and check git diff for the README file and set an output parameter with GITHUB_OUTPUT for the next step to check if there indeed is a commit.

    Here is an example:

          - name: Commit files
            id: commit
            run: |
              git config --local user.email "action@github.com"
              git config --local user.name "GitHub Action"
              git status
              if [[ -n $(git diff README.md) ]]; then
                git add README.md
                git commit -m "Actions Generated Readme"
                echo "DONE=true" >> $GITHUB_OUTPUT
              else
                echo "README is the same. Nothing to commit."
              fi
          - name: Push changes
            if: ${{ steps.commit.DONE }}
            uses: ad-m/github-push-action@master
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}