Skip to content

Commit

Permalink
Fix pipeline cancel retries (DataDog#31869)
Browse files Browse the repository at this point in the history
  • Loading branch information
CelianR authored Dec 9, 2024
1 parent 3bdaa27 commit feb37a1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
7 changes: 7 additions & 0 deletions tasks/libs/ciproviders/gitlab_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ def refresh_pipeline(pipeline: ProjectPipeline):
pipeline.refresh()


@retry_function('cancel pipeline #{0.id}', retry_delay=5)
def cancel_pipeline(pipeline: ProjectPipeline):
"""Cancels a pipeline, retries if there is an error."""

pipeline.cancel()


class GitlabCIDiff:
def __init__(
self,
Expand Down
4 changes: 2 additions & 2 deletions tasks/libs/pipeline/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from gitlab.exceptions import GitlabJobPlayError
from gitlab.v4.objects import Project, ProjectJob, ProjectPipeline

from tasks.libs.ciproviders.gitlab_api import refresh_pipeline
from tasks.libs.ciproviders.gitlab_api import cancel_pipeline, refresh_pipeline
from tasks.libs.common.color import Color, color_message
from tasks.libs.common.git import get_default_branch
from tasks.libs.common.user_interactions import yes_no_question
Expand Down Expand Up @@ -64,7 +64,7 @@ def cancel_pipelines_with_confirmation(repo: Project, pipelines: list[ProjectPip
)

if yes_no_question("Do you want to cancel this pipeline?", color="orange", default=True):
pipeline.cancel()
cancel_pipeline(pipeline)
print(f"Pipeline {color_message(pipeline.id, 'bold')} has been cancelled.\n")
else:
print(f"Pipeline {color_message(pipeline.id, 'bold')} will keep running.\n")
Expand Down
7 changes: 4 additions & 3 deletions tasks/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from tasks.libs.ciproviders.github_api import GithubAPI
from tasks.libs.ciproviders.gitlab_api import (
cancel_pipeline,
get_gitlab_bot_token,
get_gitlab_repo,
gitlab_configuration_is_modified,
Expand Down Expand Up @@ -865,7 +866,7 @@ def test_merge_queue(ctx):
# Clean up
print("Cleaning up")
if success:
pipeline.cancel()
cancel_pipeline(pipeline)
pr.edit(state="closed")
ctx.run(f"git checkout {current_branch}", hide=True)
ctx.run(f"git branch -D {test_default}", hide=True)
Expand Down Expand Up @@ -929,7 +930,7 @@ def compare_to_itself(ctx):
if attempt == max_attempts - 1:
# Clean up the branch and possible pipelines
for pipeline in pipelines:
pipeline.cancel()
cancel_pipeline(pipeline)
ctx.run(f"git checkout {current_branch}", hide=True)
ctx.run(f"git branch -D {new_branch}", hide=True)
ctx.run(f"git push origin :{new_branch}", hide=True)
Expand All @@ -946,7 +947,7 @@ def compare_to_itself(ctx):
# Clean up
print("Cleaning up the pipelines")
for pipeline in pipelines:
pipeline.cancel()
cancel_pipeline(pipeline)
print("Cleaning up git")
ctx.run(f"git checkout {current_branch}", hide=True)
ctx.run(f"git branch -D {new_branch}", hide=True)
Expand Down

0 comments on commit feb37a1

Please sign in to comment.