Skip to content

Commit b6fc26b

Browse files
committed
Fix runner deregistration by properly substituting homedir path
The termination check script was failing to deregister runners because $homedir was inside a single-quoted heredoc ('EOFT'), preventing variable substitution. This caused the script to look for /runner-* instead of /home/ubuntu/runner-*. Fixed by: - Changing heredoc delimiter from 'EOFT' to EOFT (allows substitution) - Escaping shell variables with \ to preserve them for runtime - Ensuring $homedir is substituted at template generation time This ensures runners are properly deregistered before instance termination, preventing orphaned runners in GitHub.
1 parent d22c053 commit b6fc26b

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

src/ec2_gha/templates/user-script.sh.templ

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -288,44 +288,44 @@ rm -f $$V-jobs/$${GITHUB_RUN_ID}-$${GITHUB_JOB}-$$RUNNER_IDX.job
288288
touch $$V-last-activity
289289
EOFC
290290

291-
cat > /usr/local/bin/check-runner-termination.sh << 'EOFT'
291+
cat > /usr/local/bin/check-runner-termination.sh << EOFT
292292
#!/bin/bash
293293
exec >> /tmp/termination-check.log 2>&1
294294
source /usr/local/bin/runner-common-functions.sh
295295
V="/var/run/github-runner"
296-
A="$$V-last-activity"
297-
J="$$V-jobs"
298-
H="$$V-has-run-job"
299-
300-
[ ! -f "$$A" ] && touch "$$A"
301-
L=$$(stat -c %Y "$$A" 2>/dev/null || echo 0)
302-
N=$$(date +%s)
303-
I=$$((N-L))
304-
[ -f "$$H" ] && G=$${RUNNER_GRACE_PERIOD:-60} || G=$${RUNNER_INITIAL_GRACE_PERIOD:-180}
305-
R=$$(grep -l '"status":"running"' $$J/*.job 2>/dev/null | wc -l || echo 0)
306-
307-
if [ $$R -eq 0 ] && [ $$I -gt $$G ]; then
308-
log "TERMINATING: idle $$I > grace $$G"
296+
A="\$$V-last-activity"
297+
J="\$$V-jobs"
298+
H="\$$V-has-run-job"
299+
300+
[ ! -f "\$$A" ] && touch "\$$A"
301+
L=\$$(stat -c %Y "\$$A" 2>/dev/null || echo 0)
302+
N=\$$(date +%s)
303+
I=\$$((N-L))
304+
[ -f "\$$H" ] && G=\$${RUNNER_GRACE_PERIOD:-60} || G=\$${RUNNER_INITIAL_GRACE_PERIOD:-180}
305+
R=\$$(grep -l '"status":"running"' \$$J/*.job 2>/dev/null | wc -l || echo 0)
306+
307+
if [ \$$R -eq 0 ] && [ \$$I -gt \$$G ]; then
308+
log "TERMINATING: idle \$$I > grace \$$G"
309309
# Deregister all runners
310-
for RUNNER_DIR in $homedir/runner-*; do
311-
if [ -d "$$RUNNER_DIR" ] && [ -f "$$RUNNER_DIR/config.sh" ]; then
312-
log "Deregistering runner in $$RUNNER_DIR"
313-
cd "$$RUNNER_DIR"
314-
pkill -INT -f "$$RUNNER_DIR/run.sh" 2>/dev/null || true
310+
for RUNNER_DIR in $$homedir/runner-*; do
311+
if [ -d "\$$RUNNER_DIR" ] && [ -f "\$$RUNNER_DIR/config.sh" ]; then
312+
log "Deregistering runner in \$$RUNNER_DIR"
313+
cd "\$$RUNNER_DIR"
314+
pkill -INT -f "\$$RUNNER_DIR/run.sh" 2>/dev/null || true
315315
sleep 1
316316
# Read token from stored file
317-
if [ -f "$$RUNNER_DIR/.runner-token" ]; then
318-
TOKEN=$$(cat "$$RUNNER_DIR/.runner-token")
319-
RUNNER_ALLOW_RUNASROOT=1 ./config.sh remove --token $$TOKEN 2>&1
320-
log "Deregistration exit: $$?"
317+
if [ -f "\$$RUNNER_DIR/.runner-token" ]; then
318+
TOKEN=\$$(cat "\$$RUNNER_DIR/.runner-token")
319+
RUNNER_ALLOW_RUNASROOT=1 ./config.sh remove --token \$$TOKEN 2>&1
320+
log "Deregistration exit: \$$?"
321321
fi
322322
fi
323323
done
324324
flush_cloudwatch_logs
325325
log "Shutting down"
326326
sudo shutdown -h now
327327
else
328-
[ $$R -gt 0 ] && log "$$R job(s) running" || log "Idle $$I/$$G sec"
328+
[ \$$R -gt 0 ] && log "\$$R job(s) running" || log "Idle \$$I/\$$G sec"
329329
fi
330330
EOFT
331331

0 commit comments

Comments
 (0)