name: Update flake.lock on: schedule: - cron: "0 6 * * 1" # Mondays 06:00 UTC workflow_dispatch: concurrency: group: update-flake-lock cancel-in-progress: false jobs: update: runs-on: nix steps: - name: Install Node run: | set -euo pipefail export NIX_CONFIG="experimental-features = nix-command flakes" # actions/checkout is a JS action; nixos/nix already has git/curl. nix profile add nixpkgs#nodejs_22 nixpkgs#python3 echo "$HOME/.nix-profile/bin" >> "$GITHUB_PATH" - name: Checkout uses: actions/checkout@v4 with: persist-credentials: false - name: Update flake.lock id: update run: | set -euo pipefail export NIX_CONFIG="experimental-features = nix-command flakes" nix flake update if git diff --quiet -- flake.lock; then echo "changed=false" >> "$GITHUB_OUTPUT" else echo "changed=true" >> "$GITHUB_OUTPUT" fi - name: Create pull request if: steps.update.outputs.changed == 'true' env: TOKEN: ${{ secrets.GITHUB_TOKEN }} SERVER_URL: ${{ github.server_url }} REPOSITORY: ${{ github.repository }} OWNER: ${{ github.repository_owner }} run: | set -euo pipefail BRANCH="chore/update-flake-lock" BASE="main" TITLE="chore: update flake.lock" BODY="Automated \`flake.lock\` update from the scheduled workflow. Merging this pull request deploys it to hetzner-cloud-1." git config user.name "forgejo-actions[bot]" git config user.email "forgejo-actions[bot]@users.noreply.local" git checkout -B "$BRANCH" git add flake.lock git commit -m "$TITLE" -m "Deploy-On-Merge: flake-lock" git remote set-url origin "https://oauth2:${TOKEN}@${SERVER_URL#https://}/${REPOSITORY}.git" git push --force -u origin "HEAD:refs/heads/${BRANCH}" api="${SERVER_URL}/api/v1/repos/${REPOSITORY}" # Forgejo matches head to the branch name. owner:branch is the # GitHub form and returns an empty list here, which made the job # open a second pull request and die with 409. find_pr() { curl -fsS \ -H "Authorization: token ${TOKEN}" \ -H "Content-Type: application/json" \ "${api}/pulls?state=open&head=${1}" \ | python3 -c 'import json,sys; d=json.load(sys.stdin); print(d[0]["number"] if d else "")' } existing="$(find_pr "$BRANCH")" if [ -z "$existing" ]; then existing="$(find_pr "${OWNER}:${BRANCH}")" fi if [ -z "$existing" ]; then python3 - "$api" "$TOKEN" "$TITLE" "$BODY" "$BRANCH" "$BASE" <<'PY' import json, sys, urllib.error, urllib.request api, token, title, body, head, base = sys.argv[1:] req = urllib.request.Request( f"{api}/pulls", data=json.dumps( {"title": title, "body": body, "head": head, "base": base} ).encode(), headers={ "Authorization": f"token {token}", "Content-Type": "application/json", }, method="POST", ) try: with urllib.request.urlopen(req) as resp: print(resp.read().decode()) except urllib.error.HTTPError as exc: if exc.code != 409: raise print("pull request already exists") PY else echo "Open PR #${existing} already exists for ${BRANCH}; branch force-pushed." fi