Skip to content

Commit

Permalink
feat: refactor auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
oluceps committed Nov 25, 2024
1 parent acf5a0e commit afec538
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 81 deletions.
79 changes: 5 additions & 74 deletions .github/workflows/sync-upstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
owner = "daeuniverse";
repo = "dae";
inherit rev hash;
fetchSubmodules = true;
};
inherit vendorHash;
env.VERSION = version;
Expand Down
11 changes: 8 additions & 3 deletions main.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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="
}
}
}
}

0 comments on commit afec538

Please sign in to comment.