Aborting/cancel previous Bitbucket pipeline execution on new Trigger.
Asked by komalashokkumar_jain:

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?


Answers:
author: Theodora Boudale
body: Hi [@komalashokkumar_jain](/t5/user/viewprofilepage/user-id/3865506),

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](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-get) 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](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-pipeline-uuid-stoppipeline-post)

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

* <https://community.atlassian.com/t5/Bitbucket-questions/Pipeline-triggered-from-different-repos/qaq-p/2158907#M86715>

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

comments: author: enrique piera serra
body: 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.


author: Theodora Boudale
body: Hi Enrique,

Just to understand your use case better, are you using one of our self-hosted [Runners](https://support.atlassian.com/bitbucket-cloud/docs/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


author: enrique piera serra
body: That is right, I have self-hosted Runners


author: Theodora Boudale
body: 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](https://support.atlassian.com/bitbucket-cloud/docs/pipeline-start-conditions/#Custom--manual--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:

* <https://support.atlassian.com/bitbucket-cloud/docs/pipeline-triggers/>

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


Tags: bitbucket-cloud

cloud

pull-request

trigger