Commit a file if it changed

This recipe runs a Python script to update a README, then commits it back to the parent repo but only if it has changed:

on:
  push:
    branches:
    - master
# ...
    - name: Update README
      run: python update_readme.py --rewrite
    - name: Commit README back to the repo
      run: |-
        git config --global user.email "readme-bot@example.com"
        git config --global user.name "README-bot"
        git diff --quiet || (git add README.md && git commit -m "Updated README")
        git push

My first attempt threw an error if I tried o run git commit -m ... and the README had not changed.

It turns out git diff --quiet exits with a 1 exit code if anything has changed, so this recipe adds the file and commits it only if something differs:

git diff --quiet || (git add README.md && git commit -m "Updated README")

Mikeal Rogers has a publish-to-github-action which uses a slightly different pattern:

# publish any new files
git checkout master
git add -A
timestamp=$(date -u)
git commit -m "Automated publish: ${timestamp} ${GITHUB_SHA}" || exit 0
git pull --rebase publisher master
git push publisher master

Cleanest example yet: https://github.com/simonw/coronavirus-data-gov-archive/blob/master/.github/workflows/scheduled.yml

name: Fetch latest data

on:
  push:
  repository_dispatch:
  schedule:
    - cron:  '25 * * * *'

jobs:
  scheduled:
    runs-on: ubuntu-latest
    steps:
    - name: Check out this repo
      uses: actions/checkout@v2
    - name: Fetch latest data
      run: |-
        curl https://c19downloads.azureedge.net/downloads/data/data_latest.json | jq . > data_latest.json
        curl https://c19pub.azureedge.net/utlas.geojson | gunzip | jq . > utlas.geojson
        curl https://c19pub.azureedge.net/countries.geojson | gunzip | jq . > countries.geojson
        curl https://c19pub.azureedge.net/regions.geojson | gunzip | jq . > regions.geojson
    - name: Commit and push if it changed
      run: |-
        git config user.name "Automated"
        git config user.email "actions@users.noreply.github.com"
        git add -A
        timestamp=$(date -u)
        git commit -m "Latest data: ${timestamp}" || exit 0
        git push

Created 2020-04-19T10:27:46-07:00, updated 2020-04-28T12:33:00-07:00 · History · Edit