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:

    - master
# ...
    - name: Update README
      run: python --rewrite
    - name: Commit README back to the repo
      run: |-
        git config --global ""
        git config --global "README-bot"
        git diff --quiet || (git add && 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 && 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:

name: Fetch latest data

    - cron:  '25 * * * *'

    runs-on: ubuntu-latest
    - name: Check out this repo
      uses: actions/checkout@v2
    - name: Fetch latest data
      run: |-
        curl | jq . > data_latest.json
        curl | gunzip | jq . > utlas.geojson
        curl | gunzip | jq . > countries.geojson
        curl | gunzip | jq . > regions.geojson
    - name: Commit and push if it changed
      run: |-
        git config "Automated"
        git config ""
        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