Last active 1 month ago

GitLab CI

remove_gitlab_workflow_runs.sh Raw
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
15if [[ -n "${DEBUG:-}" ]]; then
16 set -eux
17else
18 set -euo pipefail
19fi
20
21ORG_NAME=${1:?ORG_NAME is required}
22REPO_NAME=${2:?REPO_NAME is required}
23
24project_path="$ORG_NAME/$REPO_NAME"
25encoded_path=$(echo -n "$project_path" | jq -sRr @uri)
26url="projects/$encoded_path/pipelines"
27
28total_deleted=0
29
30#GITLAB_HOST=""
31
32delete_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
52while 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
72done
73