Auto Update #111
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto Update | |
| on: | |
| schedule: | |
| - cron: "0 */3 * * *" | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| concurrency: | |
| group: auto-update | |
| cancel-in-progress: true | |
| jobs: | |
| update: | |
| timeout-minutes: 5 | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ github.ref_name }} | |
| fetch-depth: 1 | |
| sparse-checkout: | | |
| flake.nix | |
| update.sh | |
| - name: Read latest release and update flake.nix | |
| id: latest | |
| run: | | |
| set -euo pipefail | |
| values="$(./update.sh)" | |
| version="$(sed -n 's/^version = "\(.*\)";$/\1/p' <<<"${values}" | head -n1)" | |
| if [[ -z "${version}" ]]; then | |
| echo "Failed to parse version from update.sh output." | |
| echo "${values}" | |
| exit 1 | |
| fi | |
| current_version="$(sed -n 's/^[[:space:]]*version = "\(.*\)";$/\1/p' flake.nix | head -n1)" | |
| if [[ "${current_version}" == "${version}" ]]; then | |
| echo "changed=false" >>"${GITHUB_OUTPUT}" | |
| exit 0 | |
| fi | |
| echo "changed=true" >>"${GITHUB_OUTPUT}" | |
| echo "version=${version}" >>"${GITHUB_OUTPUT}" | |
| # Update version in flake.nix | |
| sed -i -E "s/version = \"[^\"]+\";/version = \"${version}\";/" flake.nix | |
| # Update per-platform url and hash in flake.nix | |
| for system in x86_64-linux x86_64-darwin aarch64-darwin; do | |
| url="$(awk "/${system} = \{/,/\}/" <<<"${values}" | sed -n 's/^[[:space:]]*url = "\(.*\)";$/\1/p')" | |
| hash="$(awk "/${system} = \{/,/\}/" <<<"${values}" | sed -n 's/^[[:space:]]*hash = "\(.*\)";$/\1/p')" | |
| awk -v sys="${system}" -v new_url="${url}" -v new_hash="${hash}" ' | |
| $0 ~ sys " = \\{" { in_block=1 } | |
| in_block && /url = "/ { sub(/url = "[^"]*"/, "url = \"" new_url "\"") } | |
| in_block && /hash = "/ { sub(/hash = "[^"]*"/, "hash = \"" new_hash "\""); in_block=0 } | |
| { print } | |
| ' flake.nix > flake.nix.tmp && mv flake.nix.tmp flake.nix | |
| done | |
| - name: Commit and push changes | |
| if: steps.latest.outputs.changed == 'true' | |
| run: | | |
| set -euo pipefail | |
| if git diff --quiet -- flake.nix; then | |
| echo "No update available." | |
| exit 0 | |
| fi | |
| version='${{ steps.latest.outputs.version }}' | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git add flake.nix | |
| git commit -m "${version}" | |
| git push |