gh runner cleanup

This commit is contained in:
Hunter Halloran
2025-12-17 11:17:21 -05:00
parent c01328d826
commit e7cdc324f8

View File

@@ -51,36 +51,61 @@ mkIf builderCfg.githubRunner.enable {
ProtectKernelModules = mkForce false; ProtectKernelModules = mkForce false;
ProtectControlGroups = mkForce false; ProtectControlGroups = mkForce false;
# Override the unconfigure script to be failure-tolerant # Don't override ExecStartPre - let the default module handle configuration
# The '-' prefix means the command failure won't cause the service to fail # Just make the cleanup more tolerant by wrapping the original script
ExecStartPre = mkForce [ ExecStartPre = mkForce (
( let
let # Get the runner package and scripts
unconfigureScript = pkgs.writeShellScript "github-runner-${builderCfg.githubRunner.name}-unconfigure.sh" '' runnerPkg = pkgs.github-runner;
set +e # Don't exit on error
# Create wrapper scripts that are failure-tolerant
runnerDir="${builderCfg.githubRunner.workDir}/${builderCfg.githubRunner.name}" unconfigureWrapper = pkgs.writeShellScript "github-runner-unconfigure-wrapper.sh" ''
set +e # Don't fail on errors
# Try to remove the runner registration if it exists
if [ -e "$runnerDir" ]; then runnerDir="$1"
echo "Cleaning up runner directory: $runnerDir" stateDir="$2"
logDir="$3"
# Try to remove contents, but don't fail if busy
find "$runnerDir" -mindepth 1 -maxdepth 1 -exec rm -rf {} + 2>/dev/null || true # If directory is busy, just skip cleanup with a warning
if [ -d "$runnerDir" ]; then
# If directory still has content but we couldn't delete it, just warn echo "Attempting cleanup of $runnerDir..."
if [ "$(ls -A $runnerDir 2>/dev/null)" ]; then find "$runnerDir" -mindepth 1 -maxdepth 1 -delete 2>/dev/null || {
echo "Warning: Could not fully clean $runnerDir (may be in use)" echo "Warning: Cleanup had issues (directory may be in use), continuing anyway..."
echo "This is normal on first deployment or if runner is already running" }
fi fi
fi
exit 0
exit 0 # Always succeed '';
'';
in configureScript = pkgs.writeShellScript "github-runner-configure.sh" ''
"-${unconfigureScript} ${builderCfg.githubRunner.workDir}/${builderCfg.githubRunner.name} ${builderCfg.githubRunner.workDir} /var/log/github-runner/${builderCfg.githubRunner.name}" set -e
)
]; runnerDir="${builderCfg.githubRunner.workDir}/${builderCfg.githubRunner.name}"
token=$(cat "${builderCfg.githubRunner.tokenFile}")
cd "$runnerDir"
# Configure if not already configured or if --replace is set
if [ ! -f ".runner" ] || [ "${if builderCfg.githubRunner.replace then "true" else "false"}" = "true" ]; then
echo "Configuring GitHub Actions runner..."
${runnerPkg}/bin/Runner.Listener configure \
--unattended \
--url "${builderCfg.githubRunner.url}" \
--token "$token" \
--name "$(hostname)" \
--labels "${lib.concatStringsSep "," builderCfg.githubRunner.extraLabels}" \
--work "_work" \
--replace
else
echo "Runner already configured."
fi
'';
in
[
"-${unconfigureWrapper} ${builderCfg.githubRunner.workDir}/${builderCfg.githubRunner.name} ${builderCfg.githubRunner.workDir} /var/log/github-runner/${builderCfg.githubRunner.name}"
"${configureScript}"
]
);
}; };
}; };