Is it possible to limit the number of pending builds?

Hi all

First of all thanks for a great product!

I’m currently using drone to build a static website and deploy to kubernetes. Drone is also deployed on kubernetes.

Whenever an admin of the website makes a change in the CMS a webhook posts to the drone api which creates a new build. I’m using concurrency: { limit: 1 } to make sure I only deploy one thing at a time. If the admin makes a bunch of changes, a queue of pending builds are created. I actually only care about the latest build so I’m trying to find if there’s a way to automatically cancel a pending build if a new build is created.

To summarise, I’m looking to have the queue look something like

Build 5: Pending
Build 4: Cancelled
Build 3: Cancelled
Build 2: Running
Build 1: Completed

Any help is greatly appreciated :slight_smile:

You do have the option to auto-cancel pending pipelines. See https://github.com/drone/drone/issues/1980#issuecomment-537567118

1 Like

Well that’s the best answer I could have hoped for. Thank you!

Hmm… I just tried this and it doesn’t work as expected. I did

drone repo update --auto-cancel-pushes=true <org>/<repo>
# => Successfully updated repository <org>/<repo>

And then made 3 edits in the CMS which fires a webhook that hits

https://<host>/api/repos/<org>/<repo>/builds?branch=master

The result is 1 running build and 2 pending builds.

I expect the auto-cancel configuration does not apply to builds created via the api(?)

correct, when you create a build using the API it has event type custom. The --auto-cancel-pushes flag enables automatic canceling when the event type is push.

And I don’t expect there’s a --auto-cancel-customs ? :slight_smile:

Can you think of other ways to achieve my goal?

hmm, this is the only immediate solution I can think of …

Drone supports creating system-wide webhooks [1]. You could setup a small microservice that receives webhooks and, on build creation, uses the API to list [2] and cancel [3] pending builds for the same repository.

I would have to consider the implications a bit further, but we may accept a pull request that modifies the logic so that --auto-cancel-push also includes custom events [4][5]. We would need to ensure this change in behavior would not cause any regressions or unexpected behavior for our existing users.

[1] Overview | Drone
[2] Build List | Drone
[3] Build Stop | Drone
[4] https://github.com/drone/drone/blob/master/service/canceler/canceler.go#L93
[5] https://github.com/drone/drone/blob/master/trigger/trigger.go#L480