Reputation: 4438
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:
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 }}
Upvotes: 5
Views: 3274
Reputation: 1017
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 }}
Upvotes: 7
Reputation: 9866
Here is what I would recommend:
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
Upvotes: 4