Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hub
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
thomasDOTwtf
hub
Commits
b9f017d3
Commit
b9f017d3
authored
7 months ago
by
Roang
Browse files
Options
Downloads
Patches
Plain Diff
Add pipeline cleanup script
parent
6a412a0b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
deployment/piplines/clean_pipelines.py
+57
-0
57 additions, 0 deletions
deployment/piplines/clean_pipelines.py
with
57 additions
and
0 deletions
deployment/piplines/clean_pipelines.py
0 → 100644
+
57
−
0
View file @
b9f017d3
"""
Simple script to clean up old pipelines from the GitLab project.
"""
from
datetime
import
UTC
,
datetime
,
timedelta
import
requests
from
environ
import
environ
from
rich.progress
import
Progress
env
=
environ
.
FileAwareEnv
(
API_TOKEN
=
(
str
,
None
),
THRESHOLD_ALL
=
(
int
,
52
),
THRESHOLD_FAILED
=
(
int
,
26
),
CI_API_V4_URL
=
(
str
,
None
),
CI_PROJECT_ID
=
(
int
,
None
),
)
TOKEN
=
env
(
'
API_TOKEN
'
)
PER_PAGE
=
100
DELETE_ALL
=
datetime
.
now
(
tz
=
UTC
)
-
timedelta
(
weeks
=
env
(
'
THRESHOLD_ALL
'
))
DELETE_FAILED
=
datetime
.
now
(
tz
=
UTC
)
-
timedelta
(
weeks
=
env
(
'
THRESHOLD_FAILED
'
))
BASE_URL
=
f
'
{
env
(
'
CI_API_V4_URL
'
)
}
/projects/
{
env
(
"
CI_PROJECT_ID
"
)
}
'
if
TOKEN
is
None
:
raise
ValueError
(
'
API_TOKEN is required
'
)
while
True
:
response
=
requests
.
get
(
f
'
{
BASE_URL
}
/pipelines?per_page=
{
PER_PAGE
}
&updated_before=
{
DELETE_ALL
.
strftime
(
"
%Y-%m-%dT%H
:
%
M
:
%
SZ
"
)
}
&sort=asc&order_by=updated_at&page=1
'
,
headers
=
{
'
PRIVATE-TOKEN
'
:
TOKEN
},
)
pipelines
=
response
.
json
()
if
not
pipelines
:
break
with
Progress
()
as
progress
:
task1
=
progress
.
add_task
(
'
[red]Deleting...
'
,
total
=
len
(
pipelines
))
for
pipeline
in
pipelines
:
delete_response
=
requests
.
delete
(
f
'
{
BASE_URL
}
/pipelines/
{
pipeline
[
"
id
"
]
}
'
,
headers
=
{
'
PRIVATE-TOKEN
'
:
TOKEN
})
assert
delete_response
.
status_code
==
204
progress
.
update
(
task1
,
advance
=
1
)
while
True
:
response
=
requests
.
get
(
f
'
{
BASE_URL
}
/pipelines?per_page=
{
PER_PAGE
}
&updated_before=
{
DELETE_FAILED
.
strftime
(
"
%Y-%m-%dT%H
:
%
M
:
%
SZ
"
)
}
&status=failed&sort=asc&order_by=updated_at&page=1
'
,
headers
=
{
'
PRIVATE-TOKEN
'
:
TOKEN
},
)
pipelines
=
response
.
json
()
if
not
pipelines
:
break
with
Progress
()
as
progress
:
task1
=
progress
.
add_task
(
'
[red]Deleting...
'
,
total
=
len
(
pipelines
))
for
pipeline
in
pipelines
:
delete_response
=
requests
.
delete
(
f
'
{
BASE_URL
}
/pipelines/
{
pipeline
[
"
id
"
]
}
'
,
headers
=
{
'
PRIVATE-TOKEN
'
:
TOKEN
})
assert
delete_response
.
status_code
==
204
progress
.
update
(
task1
,
advance
=
1
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment