1
0
Fork 0
forked from emily/nixfiles
nixfiles-emily/pkgs/update-nixfiles/update-nixfiles.sh

96 lines
2.4 KiB
Bash
Raw Normal View History

2024-05-16 14:28:11 +02:00
#!/usr/bin/env bash
set -euo pipefail
2024-05-16 17:07:39 +02:00
BRANCH="update-inputs-$(date +%Y-%m-%d-%H-%M)"
2024-05-16 14:28:11 +02:00
HYDRA_URL="https://hydra.kyouma.net"
JOBSET_URL="${HYDRA_URL}/jobset/nixfiles/update-inputs"
ROOT="$(mktemp -d)"
2024-05-16 17:07:39 +02:00
gitin () {
git -C "${ROOT}" "$@"
}
merge_theirs () {
gitin merge -s ours "${BRANCH}" -m "Update from ${BRANCH}"
gitin branch temp
gitin reset --hard "${BRANCH}"
gitin reset --soft temp
gitin commit --amend --no-edit
gitin branch -D temp
}
2024-05-16 14:28:11 +02:00
test_build () {
local build_jobs
build_jobs="$(curl --fail -s -L -H "Accept: application/json" "${JOBSET_URL}/latest-eval" | jq -r ".builds | .[]")"
for build in ${build_jobs}; do
2024-05-16 17:07:39 +02:00
local build_status
while true; do
local build_finished
build_finished="$(curl --fail -s -L -H "Accept: application/json" "${HYDRA_URL}/build/${build}" | jq -r ".finished")"
[[ ${build_finished} == 1 ]] && break
sleep 5
done
build_status="$(curl --fail -s -L -H "Accept: application/json" "${HYDRA_URL}/build/${build}" | jq -r ".buildstatus")"
[[ $build_status != 0 ]] && echo "Build ${build} failed" && exit 1
echo "Build ${build} was successful"
done
}
wait_for_hydra () {
local git_rev
local hydra_rev
local counter
counter=0
git_rev="$(gitin rev-parse update-inputs)"
while [[ $counter -le 30 ]]; do
hydra_rev="$(curl -s -L -H "Accept: application/json" "${JOBSET_URL}/latest-eval" 2> /dev/null | jq -r .flake | sed -E "s/.+&rev=(.*)/\1/g")"
if [[ "${git_rev}" == "${hydra_rev}" ]]; then
echo "Hydra got new commit"
break
fi
sleep 30
((counter++))
2024-05-16 14:28:11 +02:00
done
2024-05-16 17:07:39 +02:00
if [[ $counter -ge 30 ]]; then
echo "Hydra no workey"
exit 1
fi
2024-05-16 14:28:11 +02:00
}
2024-05-16 17:07:39 +02:00
git clone git@git.bsd.gay:snaki/nixfiles.git "${ROOT}"
gitin fetch --all
gitin checkout origin/main
2024-05-16 14:28:11 +02:00
2024-05-16 17:07:39 +02:00
gitin checkout -b "${BRANCH}"
2024-05-16 14:28:11 +02:00
2024-05-16 17:07:39 +02:00
pushd "${ROOT}"
2024-05-16 14:28:11 +02:00
nix flake update --commit-lock-file
2024-05-16 17:07:39 +02:00
popd
if gitin diff --quiet origin/update-inputs "${BRANCH}"; then
echo "No update needed"
gitin checkout update-inputs
gitin branch -D "${BRANCH}"
exit 0
fi
gitin push --set-upstream origin "${BRANCH}"
gitin checkout update-inputs
merge_theirs
gitin push origin update-inputs
echo "Waiting for hydra to get new commit"
wait_for_hydra
echo "Testing if all build jobs completed successfully"
test_build
echo "All build jobs were successful"
2024-05-16 14:28:11 +02:00
2024-05-16 17:07:39 +02:00
echo "Merging ${BRANCH} into main"
gitin checkout main
gitin merge --ff-only "${BRANCH}"
gitin push origin main
echo "Update successful"