Skip to content

Instantly share code, notes, and snippets.

@jijiechen
Last active October 30, 2024 06:34
Show Gist options
  • Save jijiechen/b5ae7379ac408abc87d66d99784f7fc9 to your computer and use it in GitHub Desktop.
Save jijiechen/b5ae7379ac408abc87d66d99784f7fc9 to your computer and use it in GitHub Desktop.
Stat runner usage of GitHub actions runners in a repo
#!/bin/bash
# set -x
ORG=kumahq
REPO=kuma
SELF_HOSTED_TAG=kong
if [[ ! -z "$1" ]]; then
ORG=$1
fi
if [[ ! -z "$2" ]]; then
REPO=$2
fi
if [[ ! -z "$3" ]]; then
SELF_HOSTED_TAG=$3
fi
############ RUNNING #####################
RUNNING_JOBS=()
for RUN_ID in $(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/$ORG/$REPO/actions/runs?status=in_progress" | jq '.workflow_runs.[].id'); do
while IFS=$'\n' read -r LINE; do
if [[ "$LINE" != "" ]];then
RUNNING_JOBS+=("$LINE")
fi
done <<< "$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/$ORG/$REPO/actions/runs/$RUN_ID/jobs | jq -r '.jobs[] | select(.status == "in_progress") | .runner_name + "|" + (.run_id|tostring) + "|" + (.id|tostring) ')"
done
for RUN_ID in $(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/$ORG/$REPO/actions/runs?status=queued" | jq '.workflow_runs.[].id'); do
while IFS=$'\n' read -r LINE; do
if [[ "$LINE" != "" ]];then
RUNNING_JOBS+=("$LINE")
fi
done <<< "$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/$ORG/$REPO/actions/runs/$RUN_ID/jobs | jq -r '.jobs[] | select(.status == "in_progress") | .runner_name + "|" + (.run_id|tostring) + "|" + (.id|tostring) ')"
done
RUNNING_SELF_HOSTED=0
RUNNING_GH=0
for RUNNING in "${RUNNING_JOBS[@]}"; do
RUNNER=$(echo $RUNNING | cut -d '|' -f 1)
if [[ ! -z "$(echo $RUNNING | grep "self-hosted")" ]]; then
RUNNING_SELF_HOSTED=$((RUNNING_SELF_HOSTED + 1))
else
RUNNING_GH=$((RUNNING_GH + 1))
fi
done
echo "RUNNING runner numbers:"
echo " GitHub: $RUNNING_GH"
echo " Self hosted: $RUNNING_SELF_HOSTED"
echo "---"
GH_RUNNING_HEADER=0
for RUNNING in "${RUNNING_JOBS[@]}"; do
RUN_ID=$(echo $RUNNING | cut -d '|' -f 2)
JOB_ID=$(echo $RUNNING | cut -d '|' -f 3)
if [[ ! -z "$(echo $RUNNING | grep -v "self-hosted")" ]]; then
if [[ "$GH_RUNNING_HEADER" == "0" ]]; then
echo "GitHub runners:"
GH_RUNNING_HEADER=1
fi
echo " https://github.com/$ORG/$REPO/actions/runs/$RUN_ID/job/$JOB_ID"
fi
done
SELF_HOSTED_AMD_RUNNING_HEADER=0
SELF_HOSTED_ARM_RUNNING_HEADER=0
for RUNNING in "${RUNNING_JOBS[@]}"; do
RUNNER=$(echo $RUNNING | cut -d '|' -f 1)
RUN_ID=$(echo $RUNNING | cut -d '|' -f 2)
JOB_ID=$(echo $RUNNING | cut -d '|' -f 3)
if [[ ! -z "$(echo $RUNNING | grep "self-hosted" | grep -v "arm")" ]]; then
if [[ "$SELF_HOSTED_AMD_RUNNING_HEADER" == "0" ]]; then
echo "Self hosted runners (AMD):"
SELF_HOSTED_AMD_RUNNING_HEADER=1
fi
echo " ${RUNNER}: https://github.com/$ORG/$REPO/actions/runs/$RUN_ID/job/$JOB_ID"
fi
if [[ ! -z "$(echo $RUNNING | grep "self-hosted" | grep "arm")" ]]; then
echo "RUNNING: $RUNNING"
if [[ "$SELF_HOSTED_ARM_RUNNING_HEADER" == "0" ]]; then
echo "Self hosted runners (ARM):"
SELF_HOSTED_ARM_RUNNING_HEADER=1
fi
echo " ${RUNNER}: https://github.com/$ORG/$REPO/actions/runs/$RUN_ID/job/$JOB_ID"
fi
done
echo
echo
############ QUEUED #############
QUEUED_JOBS=()
QUEUED_GH=0
QUEUED_SELF_HOSTED=0
echo "QUEUED jobs:"
for RUN_ID in $(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/$ORG/$REPO/actions/runs?status=queued" | jq '.workflow_runs.[].id'); do
for JOB in $(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/$ORG/$REPO/actions/runs/$RUN_ID/jobs | jq -r '.jobs[] | select(.status == "queued") | (.labels | join(",")) + "|" + (.run_id|tostring) + "|" + (.id|tostring) '); do
QUEUED_JOBS+=("$JOB")
done
done
GH_QUEUED_HEADER=0
for QUEUED in "${QUEUED_JOBS[@]}"; do
LABELS=$(echo $QUEUED | cut -d '|' -f 1)
RUN_ID=$(echo $QUEUED | cut -d '|' -f 2)
JOB_ID=$(echo $QUEUED | cut -d '|' -f 3)
if [[ ! -z "$(echo $LABELS | grep -v "$SELF_HOSTED_TAG")" ]]; then
if [[ "$GH_QUEUED_HEADER" == "0" ]]; then
echo "Insufficient GitHub runners:"
GH_QUEUED_HEADER=1
fi
echo " https://github.com/$ORG/$REPO/actions/runs/$RUN_ID/job/$JOB_ID"
fi
done
echo "---"
SELF_HOSTED_QUEUED_HEADER=0
for QUEUED in "${QUEUED_JOBS[@]}"; do
LABELS=$(echo $QUEUED | cut -d '|' -f 1)
RUN_ID=$(echo $QUEUED | cut -d '|' -f 2)
JOB_ID=$(echo $QUEUED | cut -d '|' -f 3)
if [[ ! -z "$(echo $LABELS | grep "$SELF_HOSTED_TAG")" ]]; then
if [[ "$SELF_HOSTED_QUEUED_HEADER" == "0" ]]; then
echo "Insufficient self hosted runners:"
SELF_HOSTED_QUEUED_HEADER=1
fi
ARCH=AMD
if [[ ! -z "$(echo $LABELS | grep arm)" ]]; then
ARCH=ARM
fi
echo " ${LABELS} ($ARCH): https://github.com/$ORG/$REPO/actions/runs/$RUN_ID/job/$JOB_ID"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment