skip to content

In a bitbucket-pipelines.yml with default, branches, pull-requests, tags and custom sections, which pipeline does each kind of event start?

level: middleimportance: should knowfreq 30%

answer

  1. one selector wins per push
  2. exact branch name beats a glob
  3. tags never fall back
  4. pull requests run alongside
  5. custom waits for a person or schedule

basics

~20 s

In Bitbucket Pipelines a branch push runs the most specific matching branches entry, else default; a tag push runs only a matching tags entry. Pull-requests pipelines run in addition, on the merge result, and custom ones run only manually or on a schedule.

solid answer

~50 s

Under `pipelines`, the selectors pick one pipeline per event. A push to a branch runs the matching entry under `branches`, where an exact name beats a glob such as `feature/*`; only if nothing matches does `default` run. `default` never runs for tag pushes: a tag runs a matching `tags` pattern or nothing. `pull-requests` is keyed by the **source** branch glob and runs *in addition to* the branch or default pipeline, after merging the destination branch into the source; pull requests from forks do not trigger it, and overlapping definitions mean two pipelines for one push. `custom` pipelines never run on a commit; someone starts them by hand, or a schedule does, and they can prompt for variables. A commit message containing `[skip ci]` starts nothing, and a push that updates more than five branches or tags runs no pipelines at all.

code

yaml · 23 lines
yaml
pipelines:
  default:
    - step:
        script: [./run-unit-tests.sh]
  branches:
    main:
      - step:
          script: [./run-unit-tests.sh, ./package.sh]
    'release/*':
      - step:
          script: [./package.sh]
  pull-requests:
    '**':
      - step:
          script: [./run-integration-tests.sh]
  tags:
    'v*':
      - step:
          script: [./publish.sh]
  custom:
    reseed-test-database:
      - step:
          script: [./reseed.sh]

go deeper

for a junior

Recall the five sections under pipelines and that custom pipelines only run when someone or a schedule starts them.

for a middle

Explain how one pipeline is picked per push, why tags never fall back to default, and how pull-requests pipelines merge the destination and run alongside branch pipelines.

for a senior

Show how you restructure selectors to stop double runs, skip unchanged steps with changesets conditions, and keep fork contributions from running privileged pipelines.

for a principal

Weigh predictable single-pipeline selectors against condition-based triggers that start several pipelines per event, and what each does to cost and reviewability.

## The five selectors under `pipelines` In Bitbucket Pipelines, the top-level `pipelines` key of `bitbucket-pipelines.yml` must contain at least one of five sections. Each one is a **selector**: a rule that decides which list of steps runs for an event. | Section | Matched against | Starts when | |---|---|---| | `default` | every branch with no other match | a branch push that no `branches` entry matches; never on a tag push | | `branches` | branch names, glob patterns allowed | a push to a matching branch | | `pull-requests` | the pull request's **source** branch, globs allowed | a pull request is created or updated | | `tags` | tag names, globs allowed | a matching tag is pushed | | `custom` | a name you choose | someone runs it manually, or a schedule does | ## How a push picks its pipeline For the traditional selectors, the documentation is blunt: *only the first matching pipeline runs*, one per event. In practice: 1. A **tag push** is compared with the `tags` patterns. If none match, no selector pipeline starts; the documentation describes `default` as running on every push *excluding tag pushes*. 2. A **branch push** is compared with the `branches` patterns. When an exact name and a glob both match, the more specific one wins: with `feature/*` and `feature/bb-123-fix-links` defined, a push to `feature/bb-123-fix-links` runs only the latter. 3. Only when no `branches` entry matches does the **`default`** pipeline run. Two safety valves sit in front of all of this: - a commit message containing `[skip ci]` or `[ci skip]` starts no pipeline; - a single push that involves more than five branches, tags or bookmarks runs no pipelines at all, which protects build minutes from an accidental mass push. ## Pull-request pipelines run alongside The `pull-requests` section is the one that surprises teams, because it does not replace the branch selector: - it is keyed by the **source** branch of the pull request, so `'**'` means "every pull request"; - before the steps run, Bitbucket **merges the destination branch into the source**, so tests see the result of the merge; if that merge fails, the pipeline stops; - pull requests **from forked repositories do not trigger it**; - it runs **in addition to** any branch or default pipeline, so with overlapping definitions one push to a branch with an open pull request starts two pipelines. The documented fix for the double run, when a team only wants CI on pull requests, is to rename `branches` to `pull-requests` rather than keep both. ## Custom pipelines A `custom` pipeline never starts on a commit. It is started by hand from the Bitbucket UI (from the branches, commits or pipelines views) or by a schedule, which makes it the home for maintenance jobs such as reseeding a test database or a nightly long-running suite. When it is run by hand, the UI prompts for values of the variables its steps reference. ## Reading the example Take the configuration shown with this question: - a push to `feature/login` with no pull request runs **`default`**; - a push to `main` runs **`branches: main`** only, not `default` as well; - a push to `release/2.0` runs the **`release/*`** entry; - a push to `feature/login` while a pull request into `main` is open runs **`default` and `pull-requests: '**'`**, the second on the merge result; - pushing the tag `v1.4.0` runs **`tags: 'v*'`**, and pushing the tag `nightly-3` runs nothing; - `reseed-test-database` runs only when someone starts it. ## Condition-based triggers The configuration schema also accepts a top-level `triggers` property. Instead of picking a single pipeline, it evaluates logical conditions over variables such as `BITBUCKET_BRANCH` and `BITBUCKET_PR_DESTINATION_BRANCH` and starts **every** pipeline whose condition matches, in parallel. It is useful when one event genuinely should start several pipelines, but it also removes the "one pipeline per event" guarantee that keeps the selectors predictable. At step level, `condition: changesets` with `includePaths` or `excludePaths` is the finer tool: the pipeline still starts, but steps whose paths did not change are skipped, which saves minutes without restructuring the selectors. ## Mistakes teams make when they move to these selectors - **Expecting `default` to run everywhere.** Steps that every branch needs must be repeated, or shared with YAML anchors, in each `branches` entry, because a branch match replaces `default` instead of adding to it. - **Putting the release in `default`.** A deployment that should happen only from `main` or from a tag belongs under that selector, not in the fallback that every feature branch runs. - **Assuming tags inherit branch pipelines.** A release tag runs only a matching `tags` entry, so a tag-driven release with no `tags` section silently builds nothing. - **Testing contributions from forks in CI.** Fork pull requests do not trigger the `pull-requests` pipeline, so changes from outside contributors need another review path before they reach a protected branch.

  • Why might a team replace its branches section with pull-requests?
    Because `pull-requests` runs in addition to branch and default pipelines, keeping both doubles the runs for every push to a branch with an open pull request. If the team only needs CI on pull requests, renaming `branches` to `pull-requests` removes the duplicate runs, tests the merge result rather than the bare branch, and saves build minutes.
  • Both feature/* and feature/bb-123-* are defined under branches. Which runs for feature/bb-123-fix-links?
    Only `feature/bb-123-*`. Bitbucket Pipelines treats the more specific pattern, or an exact branch name, as overriding the more generic one, and the selectors run a single pipeline per push, so the `feature/*` steps do not run for that branch.

  • The default pipeline also runs for branches that have their own pipeline.
  • A tag push falls back to the default pipeline when no tag pattern matches.
  • A pull-requests pipeline replaces the branch pipeline, so runs never double.
  • Pull requests from forks run the pull-requests pipeline like any other.
  • Custom pipelines run on every push to the main branch.