|
| 1 | +name: flake-update |
| 2 | + |
| 3 | +permissions: |
| 4 | + actions: write |
| 5 | + contents: read |
| 6 | + pull-requests: write |
| 7 | + |
| 8 | +on: |
| 9 | + workflow_dispatch: |
| 10 | + schedule: |
| 11 | + - cron: "0 0 * * 1" |
| 12 | + |
| 13 | +jobs: |
| 14 | + update: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: ensure upstream |
| 18 | + run: | |
| 19 | + if gh api "/repos/${GITHUB_REPOSITORY}" | jq -e .fork; then |
| 20 | + echo "::error::This workflow should only run in the upstream repository. Disabling it in this fork, so you can ignore this run failure." |
| 21 | + gh -R "$GITHUB_REPOSITORY" workflow disable "$GITHUB_WORKFLOW" |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + env: |
| 25 | + GH_TOKEN: ${{ github.token }} |
| 26 | + |
| 27 | + - name: ensure ssh key |
| 28 | + run: | |
| 29 | + if [[ -z "$FLAKE_UPDATE_SSH_KEY" ]]; then |
| 30 | + echo "::error::FLAKE_UPDATE_SSH_KEY secret is not set. Please either set this secret or disable this workflow." |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | + env: |
| 34 | + FLAKE_UPDATE_SSH_KEY: ${{ secrets.FLAKE_UPDATE_SSH_KEY }} |
| 35 | + |
| 36 | + - name: install nix |
| 37 | + uses: cachix/install-nix-action@v31 |
| 38 | + |
| 39 | + - name: configure git |
| 40 | + run: | |
| 41 | + git config --global user.name "github-actions[bot]" |
| 42 | + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 43 | +
|
| 44 | + - name: clone repository |
| 45 | + uses: actions/checkout@v5 |
| 46 | + with: |
| 47 | + ssh-key: ${{ secrets.FLAKE_UPDATE_SSH_KEY }} |
| 48 | + |
| 49 | + - run: nix flake update --commit-lock-file |
| 50 | + |
| 51 | + - name: push |
| 52 | + env: |
| 53 | + GH_TOKEN: ${{ github.token }} |
| 54 | + run: | |
| 55 | + set -exuo pipefail |
| 56 | +
|
| 57 | + git fetch origin HEAD:remotes/origin/HEAD |
| 58 | + if git diff --quiet remotes/origin/HEAD; then |
| 59 | + echo "::notice::All flake inputs are already up to date." |
| 60 | + if git ls-remote origin refs/heads/flake-update | grep -q .; then |
| 61 | + echo "::notice::Removing flake-update branch." |
| 62 | + git push -d origin flake-update |
| 63 | + fi |
| 64 | + exit |
| 65 | + fi |
| 66 | +
|
| 67 | + { echo '```' && git log -1 --format=%B | tail -n+3 && echo '```'; } > .pr_body |
| 68 | +
|
| 69 | + echo "::notice::Pushing to the flake-update branch." |
| 70 | + git push -f origin HEAD:refs/heads/flake-update |
| 71 | +
|
| 72 | + url="$(gh api "/repos/${GITHUB_REPOSITORY}/pulls?state=open&head=${GITHUB_REPOSITORY_OWNER}:flake-update&per_page=1" --jq '.[].html_url')" |
| 73 | + if [[ -n "$url" ]]; then |
| 74 | + echo "::notice::PR for flake-update branch already exists: ${url}" |
| 75 | + else |
| 76 | + echo "::notice::Creating a new PR for the flake-update branch." |
| 77 | + gh pr create -t 'chore: update flake.lock' -F .pr_body -H flake-update |
| 78 | +
|
| 79 | + # push again to trigger ci |
| 80 | + git commit --amend --no-edit |
| 81 | + git push -f origin HEAD:refs/heads/flake-update |
| 82 | + fi |
0 commit comments