til: github-actions_commit-if-file-changed.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| github-actions_commit-if-file-changed.md | github-actions | Commit a file if it changed | https://github.com/simonw/til/blob/main/github-actions/commit-if-file-changed.md | This recipe runs a Python script to update a README, then commits it back to the parent repo but only if it has changed: ```yaml 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: ```bash git diff --quiet || (git add README.md && git commit -m "Updated README") ``` Mikeal Rogers has a [publish-to-github-action](https://github.com/mikeal/publish-to-github-action) which uses a [slightly different pattern](https://github.com/mikeal/publish-to-github-action/blob/000c8a4f43e2a7dd4aab81e3fdf8be36d4457ed8/entrypoint.sh#L21-L27): ```bash # 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 ```yaml 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 ``` | <p>This recipe runs a Python script to update a README, then commits it back to the parent repo but only if it has changed:</p> <div class="highlight highlight-source-yaml"><pre><span class="pl-ent">on</span>: <span class="pl-ent">push</span>: <span class="pl-ent">branches</span>: - <span class="pl-s">master</span> <span class="pl-c"><span class="pl-c">#</span> ...</span> - <span class="pl-ent">name</span>: <span class="pl-s">Update README</span> <span class="pl-ent">run</span>: <span class="pl-s">python update_readme.py --rewrite</span> - <span class="pl-ent">name</span>: <span class="pl-s">Commit README back to the repo</span> <span class="pl-ent">run</span>: <span class="pl-s">|-</span> <span class="pl-s"> git config --global user.email "readme-bot@example.com"</span> <span class="pl-s"> git config --global user.name "README-bot"</span> <span class="pl-s"> git diff --quiet || (git add README.md && git commit -m "Updated README")</span> <span class="pl-s"> git push</span></pre></div> <p>My first attempt threw an error if I tried o run <code>git commit -m ...</code> and the README had not changed.</p> <p>It turns out <code>git diff --quiet</code> exits with a 1 exit code if anything has changed, so this recipe adds the file and commits it only if something differs:</p> <div class="highlight highlight-source-shell"><pre>git diff --quiet <span class="pl-k">||</span> (git add README.md <span class="pl-k">&&</span> git commit -m <span class="pl-s"><span class="pl-pds">"</span>Updated README<span class="pl-pds">"</span></span>)</pre></div> <p>Mikeal Rogers has a <a href="https://github.com/mikeal/publish-to-github-action">publish-to-github-action</a> which uses a <a href="https://github.com/mikeal/publish-to-github-action/blob/000c8a4f43e2a7dd4aab81e3fdf8be36d4457ed8/entrypoint.sh#L21-L27">slightly different pattern</a>:</p> <div class="highlight highlight-source-shell"><pre><span class="pl-c"><span class="pl-c">#</span> publish any new files</span> git checkout master git add -A timestamp=<span class="pl-s"><span class="pl-pds">$(</span>date -u<span class="pl-pds">)</span></span> git commit -m <span class="pl-s"><span class="pl-pds">"</span>Automated publish: <span class="pl-smi">${timestamp}</span> <span class="pl-smi">${GITHUB_SHA}</span><span class="pl-pds">"</span></span> <span class="pl-k">||</span> <span class="pl-c1">exit</span> 0 git pull --rebase publisher master git push publisher master</pre></div> <p>Cleanest example yet: <a href="https://github.com/simonw/coronavirus-data-gov-archive/blob/master/.github/workflows/scheduled.yml">https://github.com/simonw/coronavirus-data-gov-archive/blob/master/.github/workflows/scheduled.yml</a></p> <div class="highlight highlight-source-yaml"><pre><span class="pl-ent">name</span>: <span class="pl-s">Fetch latest data</span> <span class="pl-ent">on</span>: <span class="pl-ent">push</span>: <span class="pl-ent">repository_dispatch</span>: <span class="pl-ent">schedule</span>: - <span class="pl-ent">cron</span>: <span class="pl-s"><span class="pl-pds">'</span>25 * * * *<span class="pl-pds">'</span></span> <span class="pl-ent">jobs</span>: <span class="pl-ent">scheduled</span>: <span class="pl-ent">runs-on</span>: <span class="pl-s">ubuntu-latest</span> <span class="pl-ent">steps</span>: - <span class="pl-ent">name</span>: <span class="pl-s">Check out this repo</span> <span class="pl-ent">uses</span>: <span class="pl-s">actions/checkout@v2</span> - <span class="pl-ent">name</span>: <span class="pl-s">Fetch latest data</span> <span class="pl-ent">run</span>: <span class="pl-s">|-</span> <span class="pl-s"> curl https://c19downloads.azureedge.net/downloads/data/data_latest.json | jq . > data_latest.json</span> <span class="pl-s"> curl https://c19pub.azureedge.net/utlas.geojson | gunzip | jq . > utlas.geojson</span> <span class="pl-s"> curl https://c19pub.azureedge.net/countries.geojson | gunzip | jq . > countries.geojson</span> <span class="pl-s"> curl https://c19pub.azureedge.net/regions.geojson | gunzip | jq . > regions.geojson</span> <span class="pl-s"></span> - <span class="pl-ent">name</span>: <span class="pl-s">Commit and push if it changed</span> <span class="pl-ent">run</span>: <span class="pl-s">|-</span> <span class="pl-s"> git config user.name "Automated"</span> <span class="pl-s"> git config user.email "actions@users.noreply.github.com"</span> <span class="pl-s"> git add -A</span> <span class="pl-s"> timestamp=$(date -u)</span> <span class="pl-s"> git commit -m "Latest data: ${timestamp}" || exit 0</span> <span class="pl-s"> git push</span></pre></div> | <Binary: 48,269 bytes> | 2020-04-19T10:27:46-07:00 | 2020-04-19T17:27:46+00:00 | 2020-04-28T12:33:00-07:00 | 2020-04-28T19:33:00+00:00 | 3b4a2012993962434fc8f5853cf5396b | commit-if-file-changed |