fix: add functions to restart k3s services and wait for readiness in deployment workflow

This commit is contained in:
2026-05-02 01:16:12 +08:00
parent 10aa98f3b0
commit 1b568bf58a
+57 -17
View File
@@ -695,30 +695,68 @@ jobs:
echo "Sudo mode: ${SUDO_MODE}" echo "Sudo mode: ${SUDO_MODE}"
kubectl_cmd version --client kubectl_cmd version --client
check_plain_http_registry() { restart_k3s_service() {
for unit in k3s k3s-server k3s-agent; do
if sudo_cmd systemctl is-active "${unit}" >/dev/null 2>&1; then
echo "Restarting systemd unit ${unit}..."
sudo_cmd systemctl restart "${unit}"
return 0
fi
done
for svc in k3s k3s-server k3s-agent; do
if [ -x "/etc/init.d/${svc}" ]; then
echo "Restarting init.d service ${svc}..."
sudo_cmd "/etc/init.d/${svc}" restart
return 0
fi
done
echo "Cannot find a k3s service unit to restart."
echo "Manually restart k3s on the NAS, then re-run this workflow."
return 1
}
wait_for_k3s_ready() {
i=0
while [ "${i}" -lt 60 ]; do
if kubectl_cmd get --raw=/readyz >/dev/null 2>&1; then
return 0
fi
i=$((i + 1))
sleep 2
done
echo "k3s API did not return to ready within 120s."
return 1
}
ensure_plain_http_registry() {
if [ "${REGISTRY_PLAIN_HTTP}" != "true" ]; then if [ "${REGISTRY_PLAIN_HTTP}" != "true" ]; then
return 0 return 0
fi fi
registry_file="/etc/rancher/k3s/registries.yaml" registry_file="/etc/rancher/k3s/registries.yaml"
if ! sudo_cmd test -f "${registry_file}"; then desired_endpoint="http://${REGISTRY_HOST}"
echo "Gitea Registry is configured as plain HTTP, but ${registry_file} does not exist."
echo "Create it on the NAS before deployment:" if sudo_cmd test -f "${registry_file}" \
echo "mirrors:" && sudo_cmd grep -F "${desired_endpoint}" "${registry_file}" >/dev/null 2>&1; then
echo " \"${REGISTRY_HOST}\":" return 0
echo " endpoint:" fi
echo " - \"http://${REGISTRY_HOST}\""
echo "Then restart k3s." echo "Writing k3s plain-HTTP mirror config to ${registry_file}..."
registry_yaml="$(printf 'mirrors:\n "%s":\n endpoint:\n - "%s"\n' \
"${REGISTRY_HOST}" "${desired_endpoint}")"
if ! printf '%s' "${registry_yaml}" | sudo_cmd tee "${registry_file}" >/dev/null; then
echo "Failed to write ${registry_file} via sudo."
echo "Create it manually on the NAS with:"
echo "${registry_yaml}"
echo "and restart k3s, then re-run this workflow."
exit 1 exit 1
fi fi
if ! sudo_cmd grep -F "http://${REGISTRY_HOST}" "${registry_file}" >/dev/null 2>&1; then if ! restart_k3s_service; then
echo "Gitea Registry is configured as plain HTTP, but ${registry_file} does not contain http://${REGISTRY_HOST}." exit 1
echo "Add this mirror endpoint and restart k3s:" fi
echo "mirrors:" if ! wait_for_k3s_ready; then
echo " \"${REGISTRY_HOST}\":"
echo " endpoint:"
echo " - \"http://${REGISTRY_HOST}\""
exit 1 exit 1
fi fi
} }
@@ -897,9 +935,11 @@ jobs:
esac esac
} }
if [ -n "${IMAGE_LIST}" ]; then
ensure_plain_http_registry
fi
ensure_namespace ensure_namespace
if [ -n "${IMAGE_LIST}" ]; then if [ -n "${IMAGE_LIST}" ]; then
check_plain_http_registry
ensure_registry_secret ensure_registry_secret
else else
echo "No application images selected; skipping registry pull-secret setup." echo "No application images selected; skipping registry pull-secret setup."