Skip to content
Snippets Groups Projects
Commit cb002f41 authored by Dylan Aïssi's avatar Dylan Aïssi
Browse files

Add a new tool gitlab-cancel-outdated-pipelines to cancel outdated running pipelines


This is only a workaround for missing features in GitLab to disable
some unneeded pipelines.

Signed-off-by: default avatarDylan Aïssi <dylan.aissi@collabora.com>
parent 26e5a263
No related branches found
No related tags found
2 merge requests!89Draft: Backport v2024 <- v2025pre: Release apertis-dev-tools version 0.2024.10,!87Add a new tool gitlab-cancel-outdated-pipelines to cancel outdated running pipelines
......@@ -18,6 +18,7 @@ TOOLS = \
ci-license-scan \
deb-git-version-gen \
devroot-enter \
gitlab-cancel-outdated-pipelines \
import-debian-package \
mountimage \
routine-update-apertis \
......
#!/usr/bin/env python3
# SPDX-License-Identifier: MPL-2.0
#
# Copyright © 2024 Collabora Ltd
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import argparse
import sys
import gitlab
STATUS_INACTIVE = ["success", "failed", "canceled", "skipped"]
STATUS_ACTIVE = ["created", "waiting_for_resource", "preparing", "pending", "running"]
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="gitlab-cancel-outdated-pipelines")
parser.add_argument("--gitlab-server-url", type=str, help="the GitLab server url")
parser.add_argument("--gitlab-api-token", type=str, help="the GitLab API token")
parser.add_argument("--project-id", type=str, help="the GitLab project ID")
parser.add_argument(
"--current-pipeline-id", type=str, help="the current pipeline ID"
)
parser.add_argument("--current-branch", type=str, help="the current branch")
args = parser.parse_args()
gl = gitlab.Gitlab(args.gitlab_server_url, args.gitlab_api_token)
gl.auth()
print(
f"Checking if pipelines for project {args.project_id} need to be cancelled..."
)
project = gl.projects.get(args.project_id)
list_pipelines = project.pipelines.list()
current_pipeline = project.pipelines.get(args.current_pipeline_id)
print(f"Running pipeline: {current_pipeline.id}")
list_active_pipelines = [
p
for p in list_pipelines
if p.status in STATUS_ACTIVE
if p.id != current_pipeline.id
]
print(f"Additional Active pipelines: {list_active_pipelines}")
if not list_active_pipelines:
print("No active pipeline, bye!")
sys.exit()
if current_pipeline.source == "merge_request_event":
print("Running in a merge request context")
context = "merge_request"
else:
print("Running in a branch context")
context = "branch"
print("Cancelling all previous pipelines running from the same:")
print(f' - branch: "{args.current_branch}"')
list_ref_to_cancel = [args.current_branch]
if context == "merge_request":
print(f' - merge request: "{current_pipeline.ref}"')
list_ref_to_cancel.append(current_pipeline.ref)
for pipeline in list_active_pipelines:
pipeline = project.pipelines.get(pipeline.id)
if pipeline.ref in list_ref_to_cancel:
# Just in case GitLab starts pipelines in a non chronological order...
if pipeline.id < current_pipeline.id:
print(f" {pipeline.id} is running on the same branch, so cancelling...")
pipeline.cancel()
print(f" {pipeline.id} was cancelled!")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment