Support workflows that run during pipeline finalization / after cancellation
Use case
I have a pipeline which provisions external resources and later has to clean them up.
The cleanup needs to be a separate workflow, not just a final step of another workflow. It may need different agent labels, permissions, secrets, etc.
Conceptually, the pipeline looks like this:
provision ──> test ──> ...
\ /
└──> cleanup <─┘The cleanup workflow should run once the other relevant workflows have finished, regardless of whether the pipeline:
- succeeds,
- fails, or
- is cancelled / killed.
The most important case for me is automatic cancellation of a pipeline because it has been superseded by a newer pipeline.
For example, when several commits are pushed in quick succession, an older pipeline may be cancelled while it still owns externally provisioned resources. The newer pipeline does not necessarily know about those resources and therefore cannot clean them up.
Manual cancellation has the same underlying problem, but is a less common case for my use case.
Today, a workflow can handle success and failure using:
depends_on:
- provision
- test
when:
- status: [success, failure]
steps:
- name: cleanup
...However, cancellation is different: cancelling or superseding the pipeline also cancels or prevents execution of the workflow which would perform the cleanup.
For resources created outside Woodpecker, this can leave resources behind indefinitely.
I would be interested in implementing support for this, but before starting I would like to get feedback on the intended semantics and API.
Possible approach A: add killed to when.status
One possibility would be to extend the existing status condition:
depends_on:
- provision
- test
when:
- status: [success, failure, killed]
steps:
- name: cleanup
...The meaning would be that, after a pipeline is killed — for example because it was superseded by a newer pipeline — a workflow whose when.status matches killed may still become runnable once its dependencies are in a terminal state.
Advantages
- Extends an existing concept instead of adding new syntax.
- Allows precise selection of the states in which the workflow should run.
success,failure, andkilledappear conceptually symmetrical from the configuration side.- Existing cleanup/notification workflows could simply add another status.
Questions / disadvantages
A when condition normally determines whether something is eligible to run. It does not necessarily imply that the workflow is exempt from cancellation.
For example, if an older pipeline is automatically killed because a newer one supersedes it, should this:
when:
- status: [killed]implicitly mean:
Do not kill this workflow together with the rest of the superseded pipeline, and schedule it after the other workflows have been stopped.
That is considerably stronger semantics than an ordinary condition.
It could therefore be surprising for when.status to control both conditional execution and cancellation lifetime.
Possible approach B: finalizer: true
Another possibility would be to introduce an explicit workflow property:
finalizer: true
depends_on:
- provision
- test
steps:
- name: cleanup
...A finalizer workflow would not be cancelled as part of normal pipeline cancellation. Instead, cancellation would stop ordinary workflows, and finalizer workflows would then be allowed to run.
Conceptually:
pipeline running
|
| superseded / cancellation requested
v
cancel ordinary workflows
|
v
wait until dependencies are terminal
|
v
run finalizer workflows
|
v
pipeline becomes killedAdvantages
- Makes the unusual lifecycle semantics explicit.
- Separates "when should this workflow run?" from "does cancellation apply to this workflow?"
- Could potentially be extended later with finalizer-specific behavior such as a timeout.
- The scheduler can identify finalizers directly instead of inferring them from a status condition.
Questions / disadvantages
- Introduces another pipeline concept and another configuration property.
- It is less obvious how it should interact with
when.status. - Does
finalizer: truemean "always run", or only "do not cancel me"? - If status filtering is still needed, users might end up writing something like:
finalizer: true
when:
- status: [success, failure, killed]which may feel redundant.
Possible combination
Maybe the two concepts are actually orthogonal:
finalizer: true
when:
- status: [failure, killed]where:
finalizer: truecontrols the lifecycle: this workflow survives pipeline cancellation;when.statuscontrols the condition under which it actually executes.
That is more explicit, but also adds more configuration.
Semantics that probably need to be defined
Regardless of syntax, I think a few behavioral questions need to be answered.
Pipeline state during cancellation
Should a superseded pipeline remain in some kind of "cancelling/finalizing" state while finalizer workflows are running, and only become terminal killed once they finish?
The same question would apply to a manually cancelled pipeline.
Dependencies
For a finalizer workflow:
depends_on:
- build
- deployI would expect build and deploy being killed to count as terminal dependencies, so the finalizer can start once both have stopped.
Finalizer failure
If a superseded pipeline was killed and cleanup subsequently fails, I would expect:
- pipeline status to remain
killed; - cleanup workflow status to be
failure.
That preserves both pieces of information.
Cancelling a finalizer
Manual cancellation introduces an additional question: what should happen if a user explicitly presses Cancel while a finalizer is already running?
Possible options include:
- normal cancellation does not affect finalizers;
- a second cancellation force-kills them;
- finalizers can always be manually cancelled individually.
This is less important for the automatic superseding case, but probably still needs defined semantics.
Timeouts
A finalizer should probably not be able to keep a superseded or cancelled pipeline alive indefinitely.
Would it make sense for it to have a separate timeout, either existing workflow timeout semantics or a dedicated finalization timeout?
Pipeline timeout is another interesting case: if the pipeline timeout itself causes the pipeline to be killed, should finalizers still get some additional time to clean up?
Automatic superseding
This is the primary use case I would like to solve.
If Woodpecker automatically kills an older pipeline because a newer pipeline supersedes it, I would expect the old pipeline's finalizers to still execute.
For example:
pipeline A
provision external resource
tests running
|
| new commit arrives
v
pipeline B starts
pipeline A is killed
|
v
pipeline A cleanup still runs
|
v
external resource from A is removedWithout this, automatic cancellation can leave resources created by pipeline A behind.
Multiple finalizers
Ideally finalizer workflows could use normal depends_on relationships themselves, allowing something like:
build ──────────┐
test ───────────┼──> cleanup-resources ──> cleanup-report
deploy ─────────┘Scope / guarantees
I don't think this feature could or should guarantee cleanup in every failure scenario.
For example, if the agent host disappears completely, no in-pipeline mechanism can guarantee that cleanup code runs.
The goal would instead be to guarantee finalization for orderly Woodpecker pipeline cancellation, in particular automatic cancellation of superseded pipelines, where the server and an appropriate agent are still available.
For truly critical external resources, an external TTL/janitor mechanism would still be advisable as a last resort.
Related work
There has been some related discussion around work which needs to happen after a pipeline result is known, for example #4337.
Workflow-level when.status also already provides similar behavior for success/failure, so extending that mechanism may be one possible direction.
There is also ongoing work around pipeline/workflow cancellation behavior, so I would especially appreciate feedback from maintainers on whether finalization belongs in the status-condition mechanism or should be represented explicitly in the scheduler.
Questions
Before attempting an implementation, I would especially like feedback on:
- Does supporting finalization of superseded pipelines make sense for Woodpecker?
- Would extending
when.statuswithkilledbe preferable? - Would an explicit concept such as
finalizer: truefit the architecture better? - Should these be two separate concepts: one controlling lifecycle and one controlling conditions?
- Are there cancellation/scheduler changes already planned that an implementation should build upon?
If there is agreement on the desired semantics, I would be interested in working on an implementation.
Source: woodpecker-ci/woodpecker