diff --git a/.github/workflows/update-flake-hashes.yaml b/.github/workflows/update-flake-hashes.yaml new file mode 100644 index 0000000..88f67aa --- /dev/null +++ b/.github/workflows/update-flake-hashes.yaml @@ -0,0 +1,69 @@ +name: Update flake hashes + +on: + workflow_dispatch: + inputs: + version: + description: "Release version. Defaults to Cargo.toml workspace version." + required: false + release: + types: + - published + +permissions: + contents: write + pull-requests: write + +jobs: + update-flake-hashes: + runs-on: ubuntu-latest + steps: + - name: Checkout GlobalProtect-openconnect + uses: actions/checkout@v6 + + - name: Install Nix + uses: cachix/install-nix-action@v31 + + - name: Update flake hashes + env: + RELEASE_VERSION: ${{ github.event.release.tag_name || inputs.version }} + run: | + if [[ -n "$RELEASE_VERSION" ]]; then + scripts/update-flake-hashes.sh "$RELEASE_VERSION" + else + scripts/update-flake-hashes.sh + fi + + - name: Open pull request + env: + GH_TOKEN: ${{ github.token }} + RELEASE_VERSION: ${{ github.event.release.tag_name || inputs.version }} + run: | + if git diff --quiet -- flake.nix; then + echo "flake.nix hashes are already up to date" + exit 0 + fi + + version="${RELEASE_VERSION:-$(grep '^version' Cargo.toml | head -1 | sed 's/version *= *"\(.*\)"/\1/')}" + version="${version#v}" + branch="chore/update-flake-hashes-v${version}" + + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git switch -c "$branch" + git add flake.nix + git commit -m "chore: update flake hashes for v${version}" + git push --force-with-lease origin "$branch" + + if gh pr view "$branch" --json number > /tmp/flake-pr.json 2> /dev/null; then + pr_number="$(jq -r '.number' /tmp/flake-pr.json)" + gh pr edit "$pr_number" \ + --title "chore: update flake hashes for v${version}" \ + --body "Updates the Nix flake fetchzip hashes for v${version} release assets." + else + gh pr create \ + --title "chore: update flake hashes for v${version}" \ + --body "Updates the Nix flake fetchzip hashes for v${version} release assets." \ + --base main \ + --head "$branch" + fi diff --git a/scripts/update-flake-hashes.sh b/scripts/update-flake-hashes.sh new file mode 100755 index 0000000..d6efc8e --- /dev/null +++ b/scripts/update-flake-hashes.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash + +set -euo pipefail + +REPO="yuezk/GlobalProtect-openconnect" + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" +PROJECT_DIR="$(dirname "$SCRIPT_DIR")" +FLAKE_FILE="$PROJECT_DIR/flake.nix" + +usage() { + echo "Usage: $0 [version]" + echo + echo "Updates flake.nix fetchzip hashes from the published GitHub release assets." + echo "When version is omitted, the workspace package version from Cargo.toml is used." +} + +if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then + usage + exit 0 +fi + +if ! command -v nix > /dev/null; then + echo "nix is required to calculate unpacked fetchzip hashes" >&2 + exit 1 +fi + +if ! command -v jq > /dev/null; then + echo "jq is required to read nix prefetch output" >&2 + exit 1 +fi + +version="${1:-}" +if [[ -z "$version" ]]; then + version="$(grep '^version' "$PROJECT_DIR/Cargo.toml" | head -1 | sed 's/version *= *"\(.*\)"/\1/')" +fi +version="${version#v}" + +if [[ -z "$version" ]]; then + echo "Could not determine release version" >&2 + exit 1 +fi + +prefetch_hash() { + local url="$1" + local attempt + + for attempt in {1..12}; do + if nix \ + --extra-experimental-features nix-command \ + store prefetch-file \ + --hash-type sha256 \ + --json \ + --unpack \ + "$url" \ + | jq -r '.hash'; then + return 0 + fi + + echo "Asset is not ready yet, retrying ($attempt/12): $url" >&2 + sleep 10 + done + + echo "Failed to prefetch release asset: $url" >&2 + return 1 +} + +release_url() { + local asset="$1" + echo "https://github.com/$REPO/releases/download/v$version/$asset" +} + +source_hash="$(prefetch_hash "$(release_url "globalprotect-openconnect-$version.tar.gz")")" +gpgui_x86_64_hash="$(prefetch_hash "$(release_url "gpgui_x86_64.bin.tar.xz")")" +gpgui_aarch64_hash="$(prefetch_hash "$(release_url "gpgui_aarch64.bin.tar.xz")")" + +SOURCE_HASH="$source_hash" \ +GPGUI_X86_64_HASH="$gpgui_x86_64_hash" \ +GPGUI_AARCH64_HASH="$gpgui_aarch64_hash" \ +perl -0pi -e ' + s|(url = "https://github\.com/yuezk/GlobalProtect-openconnect/releases/download/v\$\{version\}/globalprotect-openconnect-\$\{version\}\.tar\.gz";\n\s*hash = ")[^"]+(";)|$1 . $ENV{"SOURCE_HASH"} . $2|e; + s|(x86_64 = ")[^"]+(";)|$1 . $ENV{"GPGUI_X86_64_HASH"} . $2|e; + s|(aarch64 = ")[^"]+(";)|$1 . $ENV{"GPGUI_AARCH64_HASH"} . $2|e; +' "$FLAKE_FILE" + +grep -F "$source_hash" "$FLAKE_FILE" > /dev/null +grep -F "$gpgui_x86_64_hash" "$FLAKE_FILE" > /dev/null +grep -F "$gpgui_aarch64_hash" "$FLAKE_FILE" > /dev/null + +echo "Updated flake.nix hashes for v$version"