LoginSignup
14
7

More than 1 year has passed since last update.

GitHub Actionsの実行履歴をまとめて削除する / How to delete workflow run histories at once

Last updated at Posted at 2021-09-10

TL;DR

owner="Organization名 または User名"
repo="リポジトリ名"
file_name="ファイル名"

gh api "repos/${owner}/${repo}/actions/workflows/${file_name}/runs?per_page=100" \
| jq -r '.workflow_runs[].id' \
| xargs -P4 -I{} gh api repos/${owner}/${repo}/actions/runs/{} -X DELETE

やりたいこと

▼ 左側のWorkflow一覧から不要なWorkflowを削除したい。

Workflow一覧

▼ ちなみに1回のrunはweb上で消すこともできる。しかし大量にある場合はまとめて削除したい。
手動で削除する場合

方法

GitHub CLIとjqでいい感じにする。

▼ Homebrewでインストール

brew install gh jq

#1 取得

gh api "repos/${owner}/${repo}/actions/workflows/${file_name}/runs?per_page=100"

公式リンク: List workflow runs

冒頭に貼ったスクショの画面ではURLが次のようになっている。末尾がファイル名(拡張子はyaml or yml)

https://github.com/${owner}/${repo}/actions/workflows/${file_name}

#2 jqでフィルター

| jq -r '.workflow_runs[].id'

全件なら上記。例えばブランチで絞りたい場合、以下のようにする。

| jq -r '.workflow_runs[] | select(.head_branch = "master") | "\(.id)"'

#3 削除

| xargs -P4 -I{} gh api repos/${owner}/${repo}/actions/runs/{} -X DELETE

公式リンク: Delete a workflow run

-P4は4並列。APIの叩き過ぎで怒られた場合は減らしてください。

まとめ(再掲)

owner="Organization名 または User名"
repo="リポジトリ名"
file_name="ファイル名"

gh api "repos/${owner}/${repo}/actions/workflows/${file_name}/runs?per_page=100" \
| jq -r '.workflow_runs[].id' \
| xargs -P4 -I{} gh api repos/{owner}/{repo}/actions/runs/{} -X DELETE
14
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
7