Tamer Saleh
Tamer Saleh

Reputation: 503

How to trigger bitbucket pipeline in multibranch

I have a single repository under bit-bucket account and this repository has this 4 branches :

- master 
- API
- Admin
- Web

and I have only one bitbucket-pipelines.yml under master branch and its like that :

image: maven:3.5.0-jdk-7

pipelines:
  branches:
    API:
     - step:
        caches:
          - maven
        script: 

           - mvn install

so I expect that whenever any push happened to API branch will trigger this pipeline, bit in fact it didn't happen unless the push is done to the master branch, the only way out is to put this same file also under API branch as well, although the doc says its only one copy located under repo root master branch, So how can I achieve that plz?

and if I need to put same pipeline file in all branches I see it completely ridicules, so can any one plz clarify that? Thanks in advance!

Upvotes: 0

Views: 4772

Answers (2)

00willo
00willo

Reputation: 68

UPDATED

Without the documentation reference, it's hard to determine, but either the documentation was in error at the time (possibly now corrected) or you misread the "only one copy" statement in regards to the master branch.

Bitbucket will trigger a build in pipelines if both of the two following conditions are met:

  1. a bitbucket-pipelines.yml file needs to exist in the specific branch being pushed to bitbucket, and
  2. either a nested block under the branches: node matches that branch name or a default: node block is defined in the bitbucket-pipelines.yml file.

The steps defined under the default node block will be executed for all branches that do not match any of the "branches" defined beneath the branches: node block. So to show an example using your branches, the below yaml file would execute the default steps for the API branch (including any subsequent branch you create and don't explicitly define) assuming that the above condition #1 is still met for that branch.

pipelines:
  default:
    - step:
      script:
        - echo $BITBUCKET_BRANCH
  branches:
    master:
      - step:
        script:
          - echo $BITBUCKET_BRANCH
  branches:
    '{Admin,Web}':
      - step:
        script:
          - echo $BITBUCKET_BRANCH

You'll find a nice little animated gif in the official documentation which further illustrates how it works.

Upvotes: 3

BlueM
BlueM

Reputation: 3861

If you only have a bitbucket-pipelines.yml in master, then there is no bitbucket-pipelines.yml when you push to API, and therefore no pipeline will run. It’s as simple as that. The “only one copy” statement from the docs which you refer to doesn’t make too much sense to me, as the YAML file just is in Git, and therefore is branch-dependend, i.e. bitbucket-pipelines.yml is a file like any other file and there is nothing “special” about it when it comes to branches.

However, I see why you are confused – why have a single config file that specifies behavior for each branch, while one could just have a branch-specific config in each branch? I assume the rationale behind this is that usually the configurations will be very similar. For example, most repos will have branches such as develop and master which might only differ in where they are deployed. Having distinct files in the branches would be more awkward to work with.

And before you ask: yes, you can configure 2+ branches to perform the same action(s). There are at least 2 possible syntaxes, and the easiest one is this:

pipelines:
  branches:
    '{develop,master,release/*}':
      - step:
          script:
            - echo $BITBUCKET_BRANCH

Upvotes: 5

Related Questions