nixfiles/pkgs/update-nixfiles/update-nixfiles.sh

120 lines
3 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 () {
2024-05-16 22:21:01 +02:00
git -C "${ROOT}/nixfiles" "$@"
2024-05-16 17:07:39 +02:00
}
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
2024-11-04 21:19:57 +01:00
2024-11-21 00:51:39 +01:00
build_jobs="$(curl --fail -s -L -H "Accept: application/json" "${JOBSET_URL}/evals" | jq -r ".evals | max_by(.id) | .builds | .[]")"
2024-05-16 14:28:11 +02:00
for build in ${build_jobs}; do
2024-05-16 17:07:39 +02:00
local build_status
2024-11-04 21:19:57 +01:00
2024-05-16 17:07:39 +02:00
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
2024-11-04 21:19:57 +01:00
2024-05-16 17:07:39 +02:00
build_status="$(curl --fail -s -L -H "Accept: application/json" "${HYDRA_URL}/build/${build}" | jq -r ".buildstatus")"
2024-11-04 21:19:57 +01:00
[[ $build_status != 0 ]] &&
echo "Build ${build} failed" &&
exit 1
2024-05-16 17:07:39 +02:00
echo "Build ${build} was successful"
done
2024-11-04 21:19:57 +01:00
2024-11-10 22:56:53 +01:00
# Idk why this is broken someone should fix me
# local last_error
# local now
#
# last_error="$(curl --fail -s -L -H "Accept: application/json" "${JOBSET_URL}" | jq -r ".errortime")"
# now="$(date +%s)"
#
# [[ $last_error -gt $now ]] &&
# echo "Evaluation error encountered at $(date +%Y-%m-%d-%H:%M:%S --date="@${last_error}")" &&
# exit 1
2024-05-16 17:07:39 +02:00
}
wait_for_hydra () {
local git_rev
local hydra_rev
local counter
counter=0
2024-11-04 21:19:57 +01:00
git_rev="$(git -C "${1}/nixfiles" rev-parse update-inputs)"
2024-11-10 22:56:53 +01:00
while [[ $counter -lt 180 ]]; do
counter=$((counter +1))
hydra_rev="$(curl -s -L -H "Accept: application/json" "${2}/evals" | jq -r '.evals | max_by(.id) | .flake' | sed -E "s/.+&rev=(.*)/\1/g")"
2024-05-16 17:07:39 +02:00
if [[ "${git_rev}" == "${hydra_rev}" ]]; then
echo "Hydra got new commit"
break
fi
2024-11-10 22:56:53 +01:00
sleep 5
2024-05-16 14:28:11 +02:00
done
2024-11-10 22:56:53 +01:00
if [[ $counter -ge 180 ]]; then
2024-05-16 17:07:39 +02:00
echo "Hydra no workey"
exit 1
fi
2024-05-16 14:28:11 +02:00
}
2024-05-16 22:21:01 +02:00
ssh-agent -a "${ROOT}/ssh-agent"
SSH_AUTH_SOCK="${ROOT}/ssh-agent"
export SSH_AUTH_SOCK
ssh-add "/run/secrets/services/update-nixfiles/privateKey"
2024-06-20 14:18:21 +02:00
git clone forgejo@woof.rip:emily/nixfiles.git "${ROOT}/nixfiles"
2024-05-16 17:07:39 +02:00
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 22:21:01 +02:00
pushd "${ROOT}/nixfiles"
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"
2024-06-20 14:18:21 +02:00
export -f wait_for_hydra
2024-11-04 21:19:57 +01:00
timeout 4h bash -c "wait_for_hydra ${ROOT} ${JOBSET_URL}"
2024-05-16 17:07:39 +02:00
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
2024-05-16 22:21:01 +02:00
2024-05-16 17:07:39 +02:00
echo "Update successful"