remove_gitlab_workflow_runs.sh
· 1.9 KiB · Bash
Raw
#!/usr/bin/env bash
#============================================================
# File: remove_gitlab_workflow_runs.sh
# Description: 批量删除 GitLab CI 流水线
# URL: https://fx4.cn/
# ORIGIN: https://gist.asfd.cn/jetsung/gitlabci/raw/HEAD/remove_gitlab_workflow_runs.sh
# Author: Jetsung Chan <[email protected]>
# Version: 0.1.0
# CreatedAt: 2025-08-18
# UpdatedAt: 2025-08-18
#============================================================
if [[ -n "${DEBUG:-}" ]]; then
set -eux
else
set -euo pipefail
fi
ORG_NAME=${1:?ORG_NAME is required}
REPO_NAME=${2:?REPO_NAME is required}
project_path="$ORG_NAME/$REPO_NAME"
encoded_path=$(echo -n "$project_path" | jq -sRr @uri)
url="projects/$encoded_path/pipelines"
total_deleted=0
#GITLAB_HOST=""
delete_pipeline() {
local id=$1
local result=""
echo "Deleting pipeline ID: $id"
if glab api --method DELETE "$url/$id" --silent; then
result="✅ Deleted pipeline '$id'"
total_deleted=$((total_deleted + 1))
else
result="❌ Failed to delete pipeline '$id'"
echo "$result"
echo "An error occurred while deleting pipeline ID '$id'. Press Enter to exit."
echo "Total pipelines deleted: $total_deleted"
read -n 1 -s -r -p ""
exit 1
fi
printf "%s\n" "$result"
}
while true; do
# 获取流水线列表(默认每页20条)
response=$(glab api "$url" --paginate 2>/dev/null || echo "[]")
total_pipelines=$(echo "$response" | jq '. | length')
if [[ $total_pipelines -eq 0 ]]; then
echo "No more pipelines to delete. Press Enter to exit."
echo "Total pipelines deleted: $total_deleted"
read -n 1 -s -r -p ""
break
fi
# 使用 process substitution 避免子 shell
while read -r id; do
id="${id//$'\r'/}" # 去掉回车符
delete_pipeline "$id"
done < <(echo "$response" | jq -r '.[].id')
# 可选:等待几秒再继续循环,避免频繁调用 API
# sleep 2
done
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | #============================================================ |
| 4 | # File: remove_gitlab_workflow_runs.sh |
| 5 | # Description: 批量删除 GitLab CI 流水线 |
| 6 | # URL: https://fx4.cn/ |
| 7 | # ORIGIN: https://gist.asfd.cn/jetsung/gitlabci/raw/HEAD/remove_gitlab_workflow_runs.sh |
| 8 | # Author: Jetsung Chan <[email protected]> |
| 9 | # Version: 0.1.0 |
| 10 | # CreatedAt: 2025-08-18 |
| 11 | # UpdatedAt: 2025-08-18 |
| 12 | #============================================================ |
| 13 | |
| 14 | |
| 15 | if [[ -n "${DEBUG:-}" ]]; then |
| 16 | set -eux |
| 17 | else |
| 18 | set -euo pipefail |
| 19 | fi |
| 20 | |
| 21 | ORG_NAME=${1:?ORG_NAME is required} |
| 22 | REPO_NAME=${2:?REPO_NAME is required} |
| 23 | |
| 24 | project_path="$ORG_NAME/$REPO_NAME" |
| 25 | encoded_path=$(echo -n "$project_path" | jq -sRr @uri) |
| 26 | url="projects/$encoded_path/pipelines" |
| 27 | |
| 28 | total_deleted=0 |
| 29 | |
| 30 | #GITLAB_HOST="" |
| 31 | |
| 32 | delete_pipeline() { |
| 33 | local id=$1 |
| 34 | local result="" |
| 35 | |
| 36 | echo "Deleting pipeline ID: $id" |
| 37 | if glab api --method DELETE "$url/$id" --silent; then |
| 38 | result="✅ Deleted pipeline '$id'" |
| 39 | total_deleted=$((total_deleted + 1)) |
| 40 | else |
| 41 | result="❌ Failed to delete pipeline '$id'" |
| 42 | echo "$result" |
| 43 | echo "An error occurred while deleting pipeline ID '$id'. Press Enter to exit." |
| 44 | echo "Total pipelines deleted: $total_deleted" |
| 45 | read -n 1 -s -r -p "" |
| 46 | exit 1 |
| 47 | fi |
| 48 | |
| 49 | printf "%s\n" "$result" |
| 50 | } |
| 51 | |
| 52 | while true; do |
| 53 | # 获取流水线列表(默认每页20条) |
| 54 | response=$(glab api "$url" --paginate 2>/dev/null || echo "[]") |
| 55 | total_pipelines=$(echo "$response" | jq '. | length') |
| 56 | |
| 57 | if [[ $total_pipelines -eq 0 ]]; then |
| 58 | echo "No more pipelines to delete. Press Enter to exit." |
| 59 | echo "Total pipelines deleted: $total_deleted" |
| 60 | read -n 1 -s -r -p "" |
| 61 | break |
| 62 | fi |
| 63 | |
| 64 | # 使用 process substitution 避免子 shell |
| 65 | while read -r id; do |
| 66 | id="${id//$'\r'/}" # 去掉回车符 |
| 67 | delete_pipeline "$id" |
| 68 | done < <(echo "$response" | jq -r '.[].id') |
| 69 | |
| 70 | # 可选:等待几秒再继续循环,避免频繁调用 API |
| 71 | # sleep 2 |
| 72 | done |
| 73 |