Skip to content

Commit 1b754b5

Browse files
authored
saner
1 parent f957d05 commit 1b754b5

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

Github/Runners/manage_linux.sh

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,34 @@ generate_token() {
133133
export -f generate_token
134134
#------------------------------------------------------------------------------------#
135135

136+
#------------------------------------------------------------------------------------#
137+
##Remove Runner Upon Completion
138+
remove_runner() {
139+
generate_token
140+
echo -e "\n[+] Removing Runners ...\n"
141+
#Remove Offline Runners
142+
if [ -n "${GITHUB_REPOSITORY}" ]; then
143+
OFFLINE_RUNNERS="$(curl -qfsSL "https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPOSITORY}/actions/runners?per_page=100" -H "Authorization: Bearer ${GITHUB_PERSONAL_TOKEN}" -H 'Accept: application/vnd.github+json' | jq -r '.runners[] | select(.status == "offline") | .id')" && export OFFLINE_RUNNERS="${OFFLINE_RUNNERS}"
144+
readarray -t OFFLINE_RUNNERS_ID <<< "${OFFLINE_RUNNERS}"
145+
for R_ID in "${OFFLINE_RUNNERS_ID[@]}"; do
146+
curl -qfsSL -X 'DELETE' "https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPOSITORY}/actions/runners/${R_ID}" -H "Authorization: Bearer ${GITHUB_PERSONAL_TOKEN}" -H 'Accept: application/vnd.github+json'
147+
done
148+
elif [ -n "${GITHUB_ORG}" ]; then
149+
OFFLINE_RUNNERS="$(curl -qfsSL "https://api.github.com/orgs/${GITHUB_OWNER}/actions/runners?per_page=100" -H "Authorization: Bearer ${GITHUB_PERSONAL_TOKEN}" -H 'Accept: application/vnd.github+json' | jq -r '.runners[] | select(.status == "offline") | .id')" && export OFFLINE_RUNNERS="${OFFLINE_RUNNERS}"
150+
readarray -t OFFLINE_RUNNERS_ID <<< "${OFFLINE_RUNNERS}"
151+
for R_ID in "${OFFLINE_RUNNERS_ID[@]}"; do
152+
curl -qfsSL -X 'DELETE' "https://api.github.com/orgs/${GITHUB_OWNER}/actions/runners/${R_ID}" -H "Authorization: Bearer ${GITHUB_PERSONAL_TOKEN}" -H 'Accept: application/vnd.github+json'
153+
done
154+
fi
155+
#Remove Self
156+
"/runner-init/config.sh" remove --unattended --token "${RUNNER_TOKEN}"
157+
#Cleanup
158+
unset API_RESPONSE AUTH_URL OFFLINE_RUNNERS_ID REG_URL RUNNERS_ID R_ID RUNNER_ID RUNNER_LABELS RUNNER_TOKEN
159+
kill -9 $$
160+
}
161+
export -f remove_runner
162+
#------------------------------------------------------------------------------------#
163+
136164
#------------------------------------------------------------------------------------#
137165
##Register Runner
138166
cd "/runner-init"
@@ -145,6 +173,7 @@ if [ -n "${RUNNER_TOKEN}" ]; then
145173
echo "[+] (LABELS: ${RUNNER_LABELS})"
146174
echo "[+] (TOKEN: ${RUNNER_TOKEN})"
147175
echo "[+] (URL: ${REG_URL})"
176+
##Run
148177
"/runner-init/config.sh" \
149178
--name "${RUNNER_ID}" \
150179
--labels "${RUNNER_LABELS}" \
@@ -153,6 +182,13 @@ if [ -n "${RUNNER_TOKEN}" ]; then
153182
--unattended \
154183
--replace \
155184
--ephemeral
185+
##Write Status
186+
[[ -f "/tmp/GHA_CI_STATUS" && -w "/tmp/GHA_CI_STATUS" ]] && echo "" > "/tmp/GHA_CI_STATUS"
187+
if [[ -d "/tmp" && -w "/tmp" ]]; then
188+
echo "EXITED" | tee "/tmp/GHA_CI_STATUS"
189+
fi
190+
##Remove
191+
remove_runner
156192
else
157193
echo "[-] Failed to Generate Token..."
158194
echo -e "\n[+] GITHUB_PERSONAL_TOKEN: ${GITHUB_PERSONAL_TOKEN}\n"
@@ -162,33 +198,6 @@ fi
162198
#------------------------------------------------------------------------------------#
163199

164200
#------------------------------------------------------------------------------------#
165-
##Remove Runner Upon Completion
166-
remove_runner() {
167-
generate_token
168-
echo -e "\n[+] Removing Runners ...\n"
169-
#Remove Offline Runners
170-
if [ -n "${GITHUB_REPOSITORY}" ]; then
171-
OFFLINE_RUNNERS="$(curl -qfsSL "https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPOSITORY}/actions/runners?per_page=100" -H "Authorization: Bearer ${GITHUB_PERSONAL_TOKEN}" -H 'Accept: application/vnd.github+json' | jq -r '.runners[] | select(.status == "offline") | .id')" && export OFFLINE_RUNNERS="${OFFLINE_RUNNERS}"
172-
readarray -t OFFLINE_RUNNERS_ID <<< "${OFFLINE_RUNNERS}"
173-
for R_ID in "${OFFLINE_RUNNERS_ID[@]}"; do
174-
curl -qfsSL -X 'DELETE' "https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPOSITORY}/actions/runners/${R_ID}" -H "Authorization: Bearer ${GITHUB_PERSONAL_TOKEN}" -H 'Accept: application/vnd.github+json'
175-
done
176-
elif [ -n "${GITHUB_ORG}" ]; then
177-
OFFLINE_RUNNERS="$(curl -qfsSL "https://api.github.com/orgs/${GITHUB_OWNER}/actions/runners?per_page=100" -H "Authorization: Bearer ${GITHUB_PERSONAL_TOKEN}" -H 'Accept: application/vnd.github+json' | jq -r '.runners[] | select(.status == "offline") | .id')" && export OFFLINE_RUNNERS="${OFFLINE_RUNNERS}"
178-
readarray -t OFFLINE_RUNNERS_ID <<< "${OFFLINE_RUNNERS}"
179-
for R_ID in "${OFFLINE_RUNNERS_ID[@]}"; do
180-
curl -qfsSL -X 'DELETE' "https://api.github.com/orgs/${GITHUB_OWNER}/actions/runners/${R_ID}" -H "Authorization: Bearer ${GITHUB_PERSONAL_TOKEN}" -H 'Accept: application/vnd.github+json'
181-
done
182-
fi
183-
#Remove Self
184-
"/runner-init/config.sh" remove --unattended --token "${RUNNER_TOKEN}"
185-
#Write Status
186-
[[ -d "/tmp" && -w "/tmp" ]] && echo "EXITED" | tee "/tmp/GHA_CI_STATUS"
187-
#Cleanup
188-
unset API_RESPONSE AUTH_URL OFFLINE_RUNNERS_ID REG_URL RUNNERS_ID R_ID RUNNER_ID RUNNER_LABELS RUNNER_TOKEN
189-
kill -9 $$
190-
}
191-
export -f remove_runner
192201
#exit if ctrl + c
193202
trap 'remove_runner; exit 130' SIGINT
194203
#exit if kill|pkill -9

0 commit comments

Comments
 (0)