Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
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:
1on:2 push:3 branches:4 - master5# ...6 - name: Update README7 run: python update_readme.py --rewrite8 - name: Commit README back to the repo9 run: |-10 git config --global user.email "readme-bot@example.com"11 git config --global user.name "README-bot"12 git diff --quiet || (git add README.md && git commit -m "Updated README")13 git pushMy 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:
1git 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:
1# publish any new files2git checkout master3git add -A4timestamp=$(date -u)5git commit -m "Automated publish: ${timestamp} ${GITHUB_SHA}" || exit 06git pull --rebase publisher master7git push publisher masterCleanest example yet: https://github.com/simonw/coronavirus-data-gov-archive/blob/master/.github/workflows/scheduled.yml
1name: Fetch latest data2
3on:4 push:5 repository_dispatch:6 schedule:7 - cron: '25 * * * *'8
9jobs:10 scheduled:11 runs-on: ubuntu-latest12 steps:13 - name: Check out this repo14 uses: actions/checkout@v215 - name: Fetch latest data16 run: |-17 curl https://c19downloads.azureedge.net/downloads/data/data_latest.json | jq . > data_latest.json18 curl https://c19pub.azureedge.net/utlas.geojson | gunzip | jq . > utlas.geojson19 curl https://c19pub.azureedge.net/countries.geojson | gunzip | jq . > countries.geojson20 curl https://c19pub.azureedge.net/regions.geojson | gunzip | jq . > regions.geojson21 - name: Commit and push if it changed22 run: |-23 git config user.name "Automated"24 git config user.email "actions@users.noreply.github.com"25 git add -A26 timestamp=$(date -u)27 git commit -m "Latest data: ${timestamp}" || exit 028 git push