CARVIEW |
Select Language
HTTP/2 200
date: Fri, 10 Oct 2025 08:18:31 GMT
server: Fly/6f91d33b9d (2025-10-08)
content-type: text/html; charset=utf-8
content-encoding: gzip
via: 2 fly.io, 2 fly.io
fly-request-id: 01K76K6FQE1PY5CXV2JV3JGYEC-bom
Commit a file if it changed | Simon Willison’s TILs
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
Related
- github-actions Conditionally running a second job in a GitHub Actions workflow - 2022-07-11
- github-actions GitHub Actions job summaries - 2022-05-17
- github-actions Running different steps on a schedule - 2020-04-20
- github-actions Only run GitHub Action on push to master / main - 2020-04-19
- github-actions Deploying a live Datasette demo when the tests pass - 2022-03-27
- github-actions Updating a Markdown table of contents with a GitHub Action - 2020-07-22
- readthedocs Updating stable docs in ReadTheDocs without pushing a release - 2023-08-20
- github-actions Using Prettier to check JavaScript code style in GitHub Actions - 2020-12-31
- github-actions Set environment variables for all steps in a GitHub Action - 2020-04-19
- github-actions Running tests against multiple versions of a Python dependency in GitHub Actions - 2023-09-15
Created 2020-04-19T10:27:46-07:00, updated 2020-04-28T12:33:00-07:00 · History · Edit