Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Aborting/cancel previous Bitbucket pipeline execution on new Trigger.

komalashokkumar_jain
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 5, 2023

Hi Team, 

 

I have a unique problem to solve. We are trying to save precious execution time. Issue is -  We have pipeline job that triggers on creating PR, and re-triggers on every new commit to branch. We want a way to cancel old execution if a new pipeline job has been triggered for the same branch / PR. 

Is there a way to achieve this?

1 answer

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 7, 2023

Hi @komalashokkumar_jain,

I'm afraid that there is no automated way to do that.

You could write a script that uses our APIs to do the following, and then use that script at the beginning of each pull requests Pipelines build:

  • get all Pipelines builds for the repo
  • filter the results to get pipelines that are currently running
  • filter further the results to get the pull request pipelines
  • filter further the results to get pipelines that run on the same PR as the current pipeline
  • exclude the current pipeline from the results
  • use our API to stop these pipelines

You can check below the script another customer developed to do something similar:

Please note that their use case was different and it did not concern builds that run on PRs but builds that run on branches, so some adjustments would be needed to achieve what you are looking for.

Kind regards,
Theodora

enrique piera serra June 19, 2023

That solution still doesn't work if the latest pipeline is just on queue waiting for the previous one to end.  We need a solution for the newest pipeline to not go on queue, but stop all other pipelines that have been triggered from the same branch or PR. 

 

If there was a new commit onto a branch, all other pipelines from that same branch are not needed. 

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 19, 2023

Hi Enrique,

Just to understand your use case better, are you using one of our self-hosted Runners for your builds? Pipelines builds that run in our own infrastructure are not queued, so I just want to understand if you are using Runners instead.

Kind regards,
Theodora

enrique piera serra June 19, 2023

That is right, I have self-hosted Runners

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 20, 2023

Hi Enrique,

Thank you for your reply. With self-hosted runners, this is indeed not possible if you use only one runner, as a runner can only run one build at a time.

I went ahead and created a feature request for what you are asking: https://jira.atlassian.com/browse/BCLOUD-22726

Please make sure to add your vote to it and add yourself as a watcher if you'd like to get notified via email on updates.

In the meantime, you can look into using custom pipelines to replace the branch pipelines. Custom pipelines do not run automatically on every push. A user can trigger a custom pipeline manually from our website, or you can create a schedule to run it at specific times:

You could also use more than one runner with the same label, then certain builds would go to one runner while others will go to the other available runner. This should reduce the wait time. Please keep in mind that we don't recommend having multiple runners in a single machine for Linux Shell, Windows, and MacOS Runners, as this may lead to issues related to resource sharing/usage conflicts due to the shared build environment.

Kind regards,
Theodora

Like enrique piera serra likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events