Github action nothing to commit, working tree clean
Asked Answered
N

2

5

I have a git action, I have to make sure if there is nothing to add then not commit or push.

but how can I check if there is something to add and commit if necessary.

Here is an example of how I do at the moment:

enter image description here

on:
  push:
    branches:
      - testing

name: Build
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Check out current commit

      - name: Install
        run: npm install

      - name: Build
        run: npm run build

      - name: Commit
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add .
          git commit -m "Build" -a

      - name: Push
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
Nasia answered 5/3, 2022 at 12:2 Comment(0)
V
7

While the previous answer from @chenrui may still work, it will produce a warning like the following:

Warning: The set-output command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

As of October 11, 2022, GitHub has deprecated the save-state and set-output commands. Here is an updated snippet according to GitHub's recommendations:

on:
  push:
    branches:
      - testing

name: Build
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Check out current commit

      - name: Install
        run: npm install

      - name: Build
        run: npm run build

      - name: Check if there are any changes
        id: verify_diff
        run: |
          git diff --quiet . || echo "changed=true" >> $GITHUB_OUTPUT

      - name: Commit
        if: steps.verify_diff.outputs.changed == 'true'
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add .
          git commit -m "Build" -a

      - name: Push
        if: steps.verify_diff.outputs.changed == 'true'
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}

Vesuvius answered 5/11, 2022 at 7:11 Comment(0)
V
4

Here is what I would recommend:

  • you can add a separate step to do the diff check
  • use the diff check outputs to do the add/commit

It would be like something as follows (the follow example is how we extract the new translations and checkin the changes):

      - name: Check if there is any new translations
        id: verify_diff
        run: |
          npx prettier -w packages/trn/transifex
          git diff --quiet packages/trn/transifex/en_US.json || echo "::set-output name=new_translations_exist::true"

      - name: Commit files
        if: steps.verify_diff.outputs.new_translations_exist == 'true'
        run: |
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Action"
          git add packages/trn/transifex
          git commit -m "bot: extract the latest transactions"
          git push
Vinculum answered 5/3, 2022 at 17:17 Comment(5)
i.sstatic.net/XnWa2.pngNasia
i.sstatic.net/QH8xV.pngNasia
Where am I doing wrong?Nasia
do you have folder called assets?Vinculum
Yep, I'm also trying everything using technote-space/get-diff-action. Here is the link: shorturl.at/elAC2Nasia

© 2022 - 2025 — McMap. All rights reserved.