This repository was archived by the owner on Oct 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremotePipelinesCleanup.sh
More file actions
103 lines (89 loc) · 3.84 KB
/
Copy pathremotePipelinesCleanup.sh
File metadata and controls
103 lines (89 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
source $SCRIPT_DIR/include.sh
MAX_FAILED_PIPES=10
get_project_ids_under_group() {
GRP_ID=$1
resp=$(curl --request GET --silent --insecure \
"$GITLAB_URL/api/v4/groups/$GRP_ID/projects?include_subgroups=True&per_page=100" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
project_ids=$(echo "$resp" | jq ".[] | .id")
echo $project_ids
}
delete_invalid_skipped_canceled() {
PROJ_ID=$1
# invalid
resp=$(curl --request GET --silent --insecure \
"$GITLAB_URL/api/v4/projects/$PROJ_ID/pipelines?yaml_errors=true&per_page=100" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
pipe_ids=$(echo "$resp" | jq -c "[.[] | .id]")
pipe_id_len=$(echo "$pipe_ids" | jq length)
if [[ $pipe_id_len -gt 0 ]]; then
echo "$PROJ_ID: Deleting $pipe_id_len invalid pipelines"
for pipe_id in $(echo "$pipe_ids" | jq -c ".[]"); do
echo $(curl --request DELETE --silent --insecure \
"$GITLAB_URL/api/v4/projects/$PROJ_ID/pipelines/$pipe_id" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
done
fi
# canceled
resp=$(curl --request GET --silent --insecure \
"$GITLAB_URL/api/v4/projects/$PROJ_ID/pipelines?status=canceled&per_page=100" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
pipe_ids=$(echo "$resp" | jq -c "[.[] | .id]")
pipe_id_len=$(echo "$pipe_ids" | jq length)
if [[ $pipe_id_len -gt 0 ]]; then
echo "$PROJ_ID: Deleting $pipe_id_len canceled pipelines"
for pipe_id in $(echo "$pipe_ids" | jq -c ".[]"); do
echo $(curl --request DELETE --silent --insecure \
"$GITLAB_URL/api/v4/projects/$PROJ_ID/pipelines/$pipe_id" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
done
fi
# skipped
resp=$(curl --request GET --silent --insecure \
"$GITLAB_URL/api/v4/projects/$PROJ_ID/pipelines?status=skipped&per_page=100" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
pipe_ids=$(echo "$resp" | jq -c "[.[] | .id]")
pipe_id_len=$(echo "$pipe_ids" | jq length)
if [[ $pipe_id_len -gt 0 ]]; then
echo "$PROJ_ID: Deleting $pipe_id_len skipped pipelines"
for pipe_id in $(echo "$pipe_ids" | jq -c ".[]"); do
echo $(curl --request DELETE --silent --insecure \
"$GITLAB_URL/api/v4/projects/$PROJ_ID/pipelines/$pipe_id" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
done
fi
}
delete_failed() {
PROJ_ID=$1
resp=$(curl --request GET --silent --insecure \
"$GITLAB_URL/api/v4/projects/$PROJ_ID/pipelines?order_by=updated_at&sort=asc&status=failed&per_page=100" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
pipe_ids=$(echo "$resp" | jq -c ".[:-$MAX_FAILED_PIPES] | [.[] | .id]")
pipe_id_len=$(echo "$pipe_ids" | jq length)
if [[ $pipe_id_len -gt 0 ]]; then
echo "$PROJ_ID: Deleting $pipe_id_len failed pipelines"
for pipe_id in $(echo "$pipe_ids" | jq -c ".[]"); do
echo $(curl --request DELETE --silent --insecure \
"$GITLAB_URL/api/v4/projects/$PROJ_ID/pipelines/$pipe_id" \
--header "Authorization: Bearer $TOKEN_PERSONAL")
done
fi
}
per_group() {
GRP_ID=$1
GRP_NAME=$2
project_ids="$(get_project_ids_under_group $GRP_ID)"
echo "========================================"
echo "Deleting Invalid/Skipped/Canceled Pipelines for Group $GRP_NAME..."
for project_id in $project_ids; do
echo "$(delete_invalid_skipped_canceled $project_id)"
echo "$(delete_failed $project_id)"
done
}
for list in "${TOKENS_CICD[@]}";
do
IFS=' ' read -r -a item <<< "$list"
echo "$(per_group "${item[1]}" "${item[0]}")"
done