Skip to content

Commit 449d45e

Browse files
authored
feat(cloud-init): add pre/post-scripts to retrieve the job repository (#17)
* feat(cloud-init): add pre/post-scripts to retrieve the job repository KAG-7656 * use heredoc syntax * remove some tags
1 parent 8752f76 commit 449d45e

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

cloud-init.sh.tmpl

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ if [[ $ARCH == "x86_64" ]]; then ARCH=x64
9696
else ARCH=arm64
9797
fi
9898

99+
cat << 'EOF' > start.sh
100+
#!/bin/bash
101+
date +%s%3N > /tmp/job_start.ts
102+
EOF
103+
104+
cat << EOF > stop.sh
105+
#!/bin/bash
106+
now_ts=\$(date +%s%3N)
107+
start_ts=\$(cat /tmp/job_start.ts)
108+
job_duration_ms=\$((now_ts - start_ts))
109+
function send_metrics() {
110+
metrics="github.actions.\$1:\$${2:-1}|\$${3:-c}|#runner_group:${RUNNERGROUP}"
111+
metrics=\$metrics,#repo:\$GITHUB_REPOSITORY,#workflow:\$GITHUB_WORKFLOW,#job:\$GITHUB_JOB,#arch:$ARCH
112+
echo "Send metrics \$metrics to $dd_host"
113+
echo -n "\$metrics" > /dev/udp/$dd_host/8125
114+
}
115+
send_metrics jobs.repo
116+
send_metrics jobs.duration \$job_duration_ms "ms|@1"
117+
EOF
118+
99119
curl -o actions-runner-linux-$${ARCH}-${RUNNER_VER}.tar.gz -L https://github.com/actions/runner/releases/download/v${RUNNER_VER}/actions-runner-linux-$${ARCH}-${RUNNER_VER}.tar.gz
100120

101121
tar xzf ./actions-runner-linux-$${ARCH}-${RUNNER_VER}.tar.gz
@@ -159,16 +179,13 @@ provision_duration_ms=$(awk '{print $1*1000}' /proc/uptime)
159179
send_metrics runner.provision_duration $provision_duration_ms "ms|@1"
160180

161181
IFS=$'\r\n'
182+
export ACTIONS_RUNNER_HOOK_JOB_STARTED=/home/ubuntu/actions-runner/start.sh
183+
export ACTIONS_RUNNER_HOOK_JOB_COMPLETED=/home/ubuntu/actions-runner/stop.sh
162184
./run.sh | grep -vE "^[ \t|/_\\()',\.\-]*$"| grep -vF 'Self-hosted runner registration' | while read line; do
163185
echo $line
164186
if echo $line | grep -q "Running job: "; then
165187
ts=$(echo $line | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}Z')
166188
start_time=$(date -u -d "$ts" +%s)
167-
elif echo $line | grep -q "completed with result: "; then
168-
ts=$(echo $line | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}Z')
169-
job_duration_ms=$(( ( $(date -u -d "$ts" +%s) - start_time ) * 1000 ))
170-
send_metrics jobs.duration $job_duration_ms "ms|@1"
171-
send_logs job_duration $job_duration_ms
172189
fi
173190
send_logs runner_log $line
174191
done

0 commit comments

Comments
 (0)