From afec53822976d460ce99ead555cba6d19df0fb9b Mon Sep 17 00:00:00 2001 From: oluceps Date: Mon, 25 Nov 2024 22:21:50 +0800 Subject: [PATCH] feat: refactor auto update --- .github/workflows/sync-upstream.yml | 79 ++--------------------------- flake.nix | 1 + main.nu | 11 ++-- metadata.json | 8 +-- 4 files changed, 18 insertions(+), 81 deletions(-) diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml index 2338e5b..717c0d8 100644 --- a/.github/workflows/sync-upstream.yml +++ b/.github/workflows/sync-upstream.yml @@ -30,92 +30,23 @@ jobs: - uses: actions/checkout@main with: token: ${{ steps.generate_token.outputs.token }} - ref: ${{ inputs.branch }} - uses: DeterminateSystems/nix-installer-action@main - uses: DeterminateSystems/magic-nix-cache-action@main - uses: DeterminateSystems/flake-checker-action@main - - - name: Get the latest upstream commit - id: upstream_commit - shell: bash - run: | - #!/usr/bin/env bash - set -exuo pipefail - out=$(nix run nixpkgs#nix-prefetch-git -- --url 'https://github.com/${{ github.repository_owner }}/${{ inputs.project }}.git' --rev 'refs/heads/main' --fetch-submodules --quiet | tee output.json) - echo sha_short=$(echo $out | jq -r '.rev' | cut -c1-7) >> "$GITHUB_OUTPUT" - - - name: Update metadata for a project based off the latest upstream git commit - id: update_metadata - shell: bash + - name: Update package run: | - #!/usr/bin/env bash - set -exo pipefail - - # Convert the output from previous step to valid JSON format - project=${{ inputs.project }} - json_output=$(cat output.json) - - # Extract the necessary values from the json_output - date=$(echo "$json_output" | jq -r '.date' | awk -F'T' '{print $1}') - rev=$(echo "$json_output" | jq -r '.rev') - rev_short=$(echo "$json_output" | jq -r '.rev' | cut -c1-7) - hash=$(echo "$json_output" | jq -r '.hash') - - # Update the metadata.json file - jq --arg version "unstable-$date.$rev_short" \ - --arg rev "$rev" \ - --arg hash "$hash" \ - '.version = $version | .rev = $rev | .hash = $hash' \ - ./$project/metadata.json | tee ./$project/metadata.json.tmp - mv ./$project/{metadata.json.tmp,metadata.json} - - # Retry logic for awk extraction - max_retries=3 - retry_count=0 - vendor= - until [ "$retry_count" -ge "$max_retries" ]; do - vendor="$(nix --log-format raw build .#$project 2>&1 | grep "got: " | awk '/got: / {print $NF}' || echo '')" - vendor_fetch_status=$? - if [ -n "$vendor" ]; then - # If $vendor is not empty, extraction succeeded - vendor_fetch_status=0 - break - fi - retry_count=$((retry_count + 1)) - echo "Retrying nix command and awk extraction ($retry_count/$max_retries)..." - sleep 3 - done - if [ "$vendor_fetch_status" -ne 0 ]; then - echo "nix command failed with status $vendor_fetch_status." - echo "awk extraction failed after $max_retries attempts." - exit 1 - fi - - if [ -z "$vendor" ]; then - # if $vendor is empty - # use original vendorHash - vendor="$(jq -r '.vendorHash' $project/metadata.json)" - fi - - # Continue with script execution using $vendor - echo "Vendor extraction succeeded: $vendor" - - # Update vendorHash in metadata.json - jq --arg vendor "$vendor" \ - '.vendorHash = $vendor' \ - ./$project/metadata.json | tee ./$project/metadata.json.tmp - mv ./$project/{metadata.json.tmp,metadata.json} - + nix develop -c ./main.nu sync ${{ inputs.project }} ${{ inputs.branch }} + - name: Commit changes and push uses: EndBug/add-and-commit@main with: add: "${{ inputs.project }}/metadata.json" commit: --signoff - message: "chore(${{ inputs.project }}): pin ref to refs/head/main (${{ steps.upstream_commit.outputs.sha_short }})" + message: "chore(${{ inputs.project }}): sync to newest `main` head" - name: Build run: | #!/usr/bin/env bash set -exuo pipefail - nix build .#${{ inputs.project }} + nix build .#${{ inputs.project }}-${{ inputs.branch }} diff --git a/flake.nix b/flake.nix index 1d3bf3e..77e272b 100644 --- a/flake.nix +++ b/flake.nix @@ -74,6 +74,7 @@ owner = "daeuniverse"; repo = "dae"; inherit rev hash; + fetchSubmodules = true; }; inherit vendorHash; env.VERSION = version; diff --git a/main.nu b/main.nu index d66a11d..e771620 100755 --- a/main.nu +++ b/main.nu @@ -38,8 +38,8 @@ def "main sync" [ let get_vendor_hash = {|v| let res = nix --log-format raw build $'.#($project)-($v)' | complete; if ($res.exit_code == 0) { - log info "build success. return" - return + log info "build success. remain vendorHash unchange" + return; } let stderr = $res.stderr; let vendor_hash = $stderr | lines | find --regex "got:" | str trim | split row " " | last @@ -126,7 +126,12 @@ def "main sync" [ log info "save file for calc vendorHash" $metadata | save ./metadata.json -f - $metadata = $metadata | update $project { update $v { update vendorHash (do $get_vendor_hash $v) } }; + let new_vendor_hash = do $get_vendor_hash $v; + if ($new_vendor_hash == null) { + log info "skip modify vendorHash" + return; + } + $metadata = $metadata | update $project { update $v { update vendorHash $new_vendor_hash } }; } log info "save final file" diff --git a/metadata.json b/metadata.json index 6cb0b23..bca4b65 100644 --- a/metadata.json +++ b/metadata.json @@ -7,10 +7,10 @@ "vendorHash": "sha256-0Q+1cXUu4EH4qkGlK6BIpv4dCdtSKjb1RbLi5Xfjcew=" }, "unstable": { - "version": "unstable-2024-11-03.d3ab0b2", - "rev": "d3ab0b25e705399d7e610d6f09453623674e20d3", - "hash": "sha256-iUVTnFPeWQkFZBsxbnBe/lVO9FTqmQeSzDsgPa5ZqLY=", + "version": "unstable-2024-11-25.6039b25", + "rev": "6039b256de7b7d2637fb04ea9218734cf04d6397", + "hash": "sha256-GHR4ZtbcwZ3XaA8OH8OkixXHjcQOesZGw2lftWgXeys=", "vendorHash": "sha256-rGwVsDgcNGWppte0BXQF9p0pKbnjqeNBRn5oNxrxsjU=" } } -} +} \ No newline at end of file