diff --git a/manifest-integration.yml b/manifest-integration.yml index c91e175b10dc9732894d8f65ede928347bf8207b..52ced2886d1212c2eb21ed132271cd3c0c97ad3e 100644 --- a/manifest-integration.yml +++ b/manifest-integration.yml @@ -125,12 +125,22 @@ cancel-previous-pipelines: - if: $CI_MERGE_REQUEST_IID allow_failure: true script: + # Cancel pipelines in all states up to and including "running". + # Also cancel canceled pipelines, because Gitlab might have already canceled them + # without canceling their child pipelines. See Gitlab issue: + # https://gitlab.com/gitlab-org/gitlab/-/issues/390453 - .gitlab-ci/scripts/cancel_pipelines.py --gitlab-url=${CI_SERVER_URL} --token=${GITBOT_TOKEN} --project=${CI_PROJECT_PATH} --ref=${CI_MERGE_REQUEST_REF_PATH} --below-pipeline-id=${CI_PIPELINE_ID} + --status created + --status scheduled + --status pending + --status preparing + --status running + --status canceled yamllint: extends: .yamllint diff --git a/scripts/cancel_pipelines.py b/scripts/cancel_pipelines.py index acea143ee6fd2e0479be2b445214555d91223a26..f4fed2b6615b105651886d3a3fdc04c89f1749c0 100755 --- a/scripts/cancel_pipelines.py +++ b/scripts/cancel_pipelines.py @@ -44,13 +44,15 @@ def cancel_pipeline_including_children( def cancel_pipelines( project: Project, ref: str = "", + status: str = "running", below_pipeline_id: int = sys.maxsize, ) -> list[ProjectPipeline]: - """Cancel currently running pipelines. + """Cancel Gitlab pipelines. Args: project: GitLab project the pipeline belongs to ref: Git ref (branch or tag) the pipeline is running on + status: status the pipeline has below_pipeline_id: cancel only pipelines with ID below this Returns: @@ -58,7 +60,7 @@ def cancel_pipelines( """ pipelines = project.pipelines.list( - ref=ref, status="running", as_list=False, retry_transient_errors=True + ref=ref, status=status, as_list=False, retry_transient_errors=True ) cancelled_pipelines = [] @@ -96,6 +98,13 @@ def main(): dest="ref", default="", ) + parser.add_argument( + "--status", + help="""Status the pipeline has""", + dest="status", + action="append", + required=True, + ) parser.add_argument( "--below-pipeline-id", help="""Cancel only pipelines with IDs lower than this""", @@ -109,20 +118,23 @@ def main(): gitlab = Gitlab(args.gitlab_url, private_token=args.token) project = common.get_project(gitlab, args.project) - print( - f"Searching for pipelines in project '{args.project}' on ref '{args.ref}' " - f"with IDs below {args.below_pipeline_id}" - ) + for status in args.status: + print( + f"Searching for pipelines in project '{args.project}' on ref '{args.ref}' " + f"with IDs below {args.below_pipeline_id} and status {status}" + ) - cancelled_pipelines = cancel_pipelines(project, args.ref, args.below_pipeline_id) + cancelled_pipelines = cancel_pipelines( + project, args.ref, status, args.below_pipeline_id + ) - if not cancelled_pipelines: - print("No running pipelines found.") - sys.exit(0) + if not cancelled_pipelines: + print("No pipelines found.") + continue - print("Cancelled pipelines:") - for pipeline in cancelled_pipelines: - print(pipeline.web_url) + print("Cancelled pipelines:") + for pipeline in cancelled_pipelines: + print(pipeline.web_url) if __name__ == "__main__":