main. Bitbucket Pipelines has no “pull request
merged or declined” trigger, so destroying a preview is a custom pipeline you run (below).
Prerequisites
- Bitbucket Cloud with Pipelines enabled (Repository settings → Pipelines → Settings).
- A
release.plan.yamlat the repository root (sponson initwrites one). - The image
node:22.
Setup
- Copy bitbucket-pipelines.yml to the repository root, or merge its pipelines into yours.
If Pipelines deploys the app, put your deploy steps between
sponson applyandsponson finish. - Replace
0.5.0with the Sponson version you have reviewed. - Create the receipts token and the variables below.
- In Repository settings → Deployments, keep the production tokens in the
Productionenvironment’s variables and, on plans that offer it, restrict who may deploy to it.
Variables
Every adapter’s variable is listed under built-in ops.
Receipts: push credentials
Sponson writes its receipts to orphan branches of the repository,sponson-receipts/<env>/<scope>, from a clone of
its own. The pipeline’s clone credentials are configured for the build’s checkout only, so the template points
Sponson’s clone at a token that can push:
- Repository settings → Security → Access tokens → Create access token: name
sponson-receipts, scope Repositories: Write. - Save it as the secured repository variable
SPONSON_GIT_TOKEN. - Each step exports
SPONSON_RECEIPTS_REMOTE=https://x-token-auth:${SPONSON_GIT_TOKEN}@bitbucket.org/${BITBUCKET_REPO_FULL_NAME}.git(the&receipts-remoteanchor). Sponson strips credentials from every git error it prints.
sponson-receipts/*, let the access token push to it. The receipts branches carry no
bitbucket-pipelines.yml, so pushing them starts no pipeline.
Destroying a closed pull request’s preview
Pipelines → Run pipeline, branchmain, pipeline custom: sponson-destroy, then enter SPONSON_PR_ID (the PR’s
number) and SPONSON_PR_BRANCH (its source branch). The scope is pr-<id> whatever branch the pipeline runs on, so
this works after the PR’s branch is deleted. The same pipeline can be started through the Bitbucket API with the two
variables.
How the flags are filled
Sponson 0.5.0 and later read
BITBUCKET_PR_ID, BITBUCKET_BRANCH and BITBUCKET_COMMIT themselves when
BITBUCKET_BUILD_NUMBER is set, so those flags may then be dropped. --approved-by always stays explicit.
Notes
- The approver is recorded as an account UUID, the only identity of the person who started the step that Pipelines exposes. Look it up in the workspace’s members if you need the name.
clone: depth: fullfetches the whole history; Sponson uses it to recognise a late build of an older commit.- Pipelines rejects a pipeline whose first step is manual, so
branches: mainstarts with a step that only says what to run next; put your own build or deploy steps there instead if you have them. - What has been run (
scripts/try-ci-templates.sh bitbucket): every step’s script verbatim innode:22, in pipeline order, against the fake cloud: the pull request pipeline (apply stopping at the deploy barrier, finish waiting for it), both steps ofbranches: mainwith the triggerer recorded as the approver, andcustom: sponson-destroyonmainwith the PR’s id and branch. The clone and the predefined, custom and deployment variables are emulated; not run on Bitbucket itself. See how the templates are checked.
The template: bitbucket-pipelines.yml
bitbucket-pipelines.yml