Skip to content

Instantly share code, notes, and snippets.

@Atsumi3
Created October 11, 2023 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Atsumi3/e5e66498f20e36b24f7a733a519452ff to your computer and use it in GitHub Desktop.
Save Atsumi3/e5e66498f20e36b24f7a733a519452ff to your computer and use it in GitHub Desktop.
Github Actions の不要なAction履歴削除
# based: https://stackoverflow.com/a/67000032
# ghコマンドのセッションチェック
gh auth status >/dev/null
if [ $? -ne 0 ]; then
echo "Please login to GitHub with gh command."
exit 1
fi
# org, repo を引数から取得する
if [ $# -ne 2 ]; then
echo "Usage: $0 <org> <repo>"
exit 1
fi
org=$1
repo=$2
# ワークフロー一覧を取得
response=$(gh api repos/$org/$repo/actions/workflows --paginate)
workflow_names=$(echo $response | jq '.workflows[] | select(.["state"]) | .name')
# 削除対象の選択
oldIFS=$IFS
IFS=$'\n'
echo "Select workflow to delete:"
selected_workflow_name=""
select workflow_name in $workflow_names
do
selected_workflow_name=$workflow_name
break
done
# \" を削除
selected_workflow_name=$(echo $selected_workflow_name | sed -e 's/\"//g')
echo "Selected workflow: $selected_workflow_name"
# 履歴を取得
workflow_id=$(echo $response | jq -r --arg name $selected_workflow_name '.workflows[] | select(.name==$name) | .id')
IFS=$oldIFS
run_ids=( $(gh api repos/$org/$repo/actions/workflows/$workflow_id/runs --paginate | jq '.workflow_runs[].id') )
run_ids_count=${#run_ids[@]}
# 履歴 を消していいのか尋ねる
echo "Delete $run_ids_count workflow runs? (y/n)"
read answer
if [ "$answer" != "y" ]; then
echo "Aborted."
exit 1
fi
# もう一度確認
echo "Are you sure? (y/n): repo: $repo, workflow: $selected_workflow_name"
read answer
if [ "$answer" != "y" ]; then
echo "Aborted."
exit 1
fi
# 履歴を一つ一つ消す
echo "Deleting workflow runs..."
for run_id in "${run_ids[@]}"
do
echo "Deleting Run ID $run_id"
gh api repos/$org/$repo/actions/runs/$run_id -X DELETE >/dev/null
done
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment