4646export homedir
4747
4848# Set common paths
49- B=/usr/local/bin
50- V=/var/run/github-runner
49+ BIN_DIR=/usr/local/bin
50+ RUNNER_STATE_DIR=/var/run/github-runner
51+ mkdir -p $RUNNER_STATE_DIR
5152
5253# Fetch shared functions from GitHub
5354echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] Fetching shared functions from GitHub (SHA: ${action_sha} )" | tee -a /var/log/runner-setup.log
@@ -59,44 +60,45 @@ if ! curl -sSL "$FUNCTIONS_URL" -o /tmp/shared-functions.sh && ! wget -q "$FUNCT
5960fi
6061
6162# Write shared functions that will be used by multiple scripts
62- cat > $B /runner-common.sh << EOSF
63+ cat > $BIN_DIR /runner-common.sh << EOSF
6364# Auto-generated shared functions and variables
6465# Set homedir for scripts that source this file
6566homedir="$homedir "
6667debug="$debug "
67- export homedir debug
68+ RUNNER_STATE_DIR="$RUNNER_STATE_DIR "
69+ export homedir debug RUNNER_STATE_DIR
6870
6971EOSF
7072
7173# Append the downloaded shared functions
72- cat /tmp/shared-functions.sh >> $B /runner-common.sh
74+ cat /tmp/shared-functions.sh >> $BIN_DIR /runner-common.sh
7375
74- chmod +x $B /runner-common.sh
75- source $B /runner-common.sh
76+ chmod +x $BIN_DIR /runner-common.sh
77+ source $BIN_DIR /runner-common.sh
7678
7779logger " EC2-GHA: Starting userdata script"
7880trap ' logger "EC2-GHA: Script failed at line $LINENO with exit code $?"' ERR
7981trap ' terminate_instance "Setup script failed with error on line $LINENO"' ERR
8082# Handle watchdog termination signal
81- trap ' if [ -f $V- watchdog-terminate ]; then terminate_instance "No runners registered within timeout"; else terminate_instance "Script terminated"; fi' TERM
83+ trap ' if [ -f $RUNNER_STATE_DIR/ watchdog-terminate ]; then terminate_instance "No runners registered within timeout"; else terminate_instance "Script terminated"; fi' TERM
8284
8385# Set up registration timeout failsafe - terminate if runner doesn't register in time
8486REGISTRATION_TIMEOUT=" $runner_registration_timeout "
8587if ! [[ " $REGISTRATION_TIMEOUT " =~ ^[0-9]+$ ]]; then
8688 REGISTRATION_TIMEOUT=300
8789fi
8890# Create a marker file for watchdog termination request
89- touch $V - watchdog-active
91+ touch $RUNNER_STATE_DIR / watchdog-active
9092(
9193 sleep $REGISTRATION_TIMEOUT
92- if [ ! -f $V - registered ]; then
93- touch $V - watchdog-terminate
94+ if [ ! -f $RUNNER_STATE_DIR / registered ]; then
95+ touch $RUNNER_STATE_DIR / watchdog-terminate
9496 kill -TERM $$ 2> /dev/null || true
9597 fi
96- rm -f $V - watchdog-active
98+ rm -f $RUNNER_STATE_DIR / watchdog-active
9799) &
98100REGISTRATION_WATCHDOG_PID=$!
99- echo $REGISTRATION_WATCHDOG_PID > $V - watchdog.pid
101+ echo $REGISTRATION_WATCHDOG_PID > $RUNNER_STATE_DIR / watchdog.pid
100102
101103# Run any custom user data script provided by the user
102104if [ -n " $userdata " ]; then
@@ -151,21 +153,29 @@ if [ "$cloudwatch_logs_group" != "" ]; then
151153 rm amazon-cloudwatch-agent.rpm
152154 fi
153155
154- # Build CloudWatch config with factored strings
155- G=' ,"log_group_name":"' $cloudwatch_logs_group ' ","log_stream_name":"{instance_id}/'
156- Z=' ","timezone":"UTC"}'
157- H=" $homedir "
156+ # Build CloudWatch config
158157 cat > /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json << EOF
159- {"agent":{"run_as_user":"cwagent"},"logs":{"logs_collected":{"files":{"collect_list":[
160- {"file_path":"/var/log/runner-setup.log${G} runner-setup$Z "},
161- {"file_path":"/var/log/runner-debug.log${G} runner-debug$Z "},
162- {"file_path":"/tmp/job-started-hook.log${G} job-started$Z "},
163- {"file_path":"/tmp/job-completed-hook.log${G} job-completed$Z "},
164- {"file_path":"/tmp/termination-check.log${G} termination$Z "},
165- {"file_path":"/tmp/runner-*-config.log${G} runner-config$Z "},
166- {"file_path":"$H /_diag/Runner_**.log${G} runner-diag$Z "},
167- {"file_path":"$H /_diag/Worker_**.log${G} worker-diag$Z "}
168- ]}}}}
158+ {
159+ "agent": {
160+ "run_as_user": "cwagent"
161+ },
162+ "logs": {
163+ "logs_collected": {
164+ "files": {
165+ "collect_list": [
166+ { "file_path": "/var/log/runner-setup.log" , "log_group_name": "$cloudwatch_logs_group ", "log_stream_name": "{instance_id}/runner-setup" , "timezone": "UTC" },
167+ { "file_path": "/var/log/runner-debug.log" , "log_group_name": "$cloudwatch_logs_group ", "log_stream_name": "{instance_id}/runner-debug" , "timezone": "UTC" },
168+ { "file_path": "/tmp/job-started-hook.log" , "log_group_name": "$cloudwatch_logs_group ", "log_stream_name": "{instance_id}/job-started" , "timezone": "UTC" },
169+ { "file_path": "/tmp/job-completed-hook.log" , "log_group_name": "$cloudwatch_logs_group ", "log_stream_name": "{instance_id}/job-completed", "timezone": "UTC" },
170+ { "file_path": "/tmp/termination-check.log" , "log_group_name": "$cloudwatch_logs_group ", "log_stream_name": "{instance_id}/termination" , "timezone": "UTC" },
171+ { "file_path": "/tmp/runner-*-config.log" , "log_group_name": "$cloudwatch_logs_group ", "log_stream_name": "{instance_id}/runner-config", "timezone": "UTC" },
172+ { "file_path": "$homedir /_diag/Runner_**.log", "log_group_name": "$cloudwatch_logs_group ", "log_stream_name": "{instance_id}/runner-diag" , "timezone": "UTC" },
173+ { "file_path": "$homedir /_diag/Worker_**.log", "log_group_name": "$cloudwatch_logs_group ", "log_stream_name": "{instance_id}/worker-diag" , "timezone": "UTC" }
174+ ]
175+ }
176+ }
177+ }
178+ }
169179EOF
170180
171181 if ! /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s; then
@@ -229,7 +239,7 @@ log "Downloaded runner binary"
229239fetch_script () {
230240 local script_name=" $1 "
231241 local url=" ${BASE_URL} /${script_name} "
232- local dest=" ${B } /${script_name} "
242+ local dest=" ${BIN_DIR } /${script_name} "
233243
234244 if command -v curl > /dev/null 2>&1 ; then
235245 curl -fsSL " $url " -o " $dest " || {
@@ -257,14 +267,14 @@ fetch_script "job-completed-hook.sh"
257267fetch_script " check-runner-termination.sh"
258268
259269# Replace log prefix placeholders with actual values
260- sed -i " s/LOG_PREFIX_JOB_STARTED/${log_prefix_job_started} /g" $B /job-started-hook.sh
261- sed -i " s/LOG_PREFIX_JOB_COMPLETED/${log_prefix_job_completed} /g" $B /job-completed-hook.sh
270+ sed -i " s/LOG_PREFIX_JOB_STARTED/${log_prefix_job_started} /g" $BIN_DIR /job-started-hook.sh
271+ sed -i " s/LOG_PREFIX_JOB_COMPLETED/${log_prefix_job_completed} /g" $BIN_DIR /job-completed-hook.sh
262272
263- chmod +x $B /job-started-hook.sh $B /job-completed-hook.sh $B /check-runner-termination.sh
273+ chmod +x $BIN_DIR /job-started-hook.sh $BIN_DIR /job-completed-hook.sh $BIN_DIR /check-runner-termination.sh
264274
265275# Set up job tracking directory
266- mkdir -p $V - jobs
267- touch $V - last-activity
276+ mkdir -p $RUNNER_STATE_DIR / jobs
277+ touch $RUNNER_STATE_DIR / last-activity
268278
269279# Set up periodic termination check using systemd
270280cat > /etc/systemd/system/runner-termination-check.service << EOF
@@ -276,7 +286,7 @@ Type=oneshot
276286Environment="RUNNER_GRACE_PERIOD=$runner_grace_period "
277287Environment="RUNNER_INITIAL_GRACE_PERIOD=$runner_initial_grace_period "
278288Environment="RUNNER_POLL_INTERVAL=$runner_poll_interval "
279- ExecStart=$B /check-runner-termination.sh
289+ ExecStart=$BIN_DIR /check-runner-termination.sh
280290EOF
281291
282292cat > /etc/systemd/system/runner-termination-check.timer << EOF
367377
368378if [ $succeeded -gt 0 ]; then
369379 log " $succeeded runner(s) registered and started successfully"
370- touch $V - registered
380+ touch $RUNNER_STATE_DIR / registered
371381else
372382 log_error " No runners registered successfully"
373383 terminate_instance " No runners registered successfully"
374384fi
375385
376386# Kill registration watchdog now that runners are registered
377- if [ -f $V - watchdog.pid ]; then
378- WATCHDOG_PID=$( cat $V - watchdog.pid)
387+ if [ -f $RUNNER_STATE_DIR / watchdog.pid ]; then
388+ WATCHDOG_PID=$( cat $RUNNER_STATE_DIR / watchdog.pid)
379389 kill $WATCHDOG_PID 2> /dev/null || true
380- rm -f $V - watchdog.pid
390+ rm -f $RUNNER_STATE_DIR / watchdog.pid
381391fi
382392
383393# Final setup - ensure runner directories are accessible for debugging
384- touch $V - started
394+ touch $RUNNER_STATE_DIR / started
385395chmod o+x $homedir
386396for RUNNER_DIR in $homedir /runner-* ; do
387397 [ -d " $RUNNER_DIR /_diag" ] && chmod 755 " $RUNNER_DIR /_diag"
0 commit comments