Prevent branch creation from triggering a Bitbucket Pipelines build
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Summary
The purpose of this how-to article is to explain why the creation of a branch via the UI/GIT would automatically trigger a Bitbucket Pipelines build, and describe potential approaches to prevent this from happening
Environment
Creating a branch in a Bitbucket Cloud repository either via the UI or a GIT client
Solution
This article is strongly related to Bitbucket Pipelines YML configuration, please refer to our YML configuration guide for further information and assistance with this topic.
The creation of a new branch inherently involves a GIT push, so any YML tags related to GIT push will cause builds to trigger
If you have a default tag configured in your Bitbucket Pipelines YML configuration, this will always trigger a build when there is a push to the repository. Similar behaviour will be encountered if the name of the new branch is captured by the branches tag in your YML configuration - for example:
1 2 3 4 5 6 7 8 9 10 11 12 13
# The default tag will capture all pushes to the repository pipelines: default: - step: script: - echo "Hello world" # If the name of your new branch is called "feature/feature1" for example, this will be captured by the below definition and trigger a build: branches: feature/*: - step: name: 'Echo Message' script: - echo "Hello world!"
Solution 1
The simplest method of avoiding builds during the creation of a new branch is to push the new branch locally rather than via the UI
By pushing the new branch from your local repository - this will allow you to specify a commit message to skip build triggers so that the build does not occur
To do so, you will need to execute the following commands as part of a regular branch push procedure with an added skip ci commit message:
1 2 3 4 5 6 7 8
# The following commands create a new branch, switch to that branch, commit any changes with [skip ci] message, switch back to the main branch and finally push the new branch to your # remote repository git branch newbranchname git checkout newbranchname git add . git commit -m "[skip ci] Adding new branch" git checkout main git push origin newbranchname
Solution 2
If the local creation of branches does not fit within your existing workflow, you can simply remove the default tag or any other branches tag (that matches the name of the new branch) from your YML configuration to avoid a build from triggering during branch creation - this will likely not be appropriate for most use cases as it will also prevent builds for any scenario that is not explicitly defined in your YML configuration
Solution 3
The script provided below is only intended to serve as a guide, you will need to make modifications to suit your individual use case. As scripting support falls outside of our Atlassian Cloud technical support scope - you may reach out to our Atlassian community for further scripting assistance.
As you cannot add a commit message when creating a new branch via the UI, you cannot completely avoid triggering a build using skip ci - but you can control the behaviour of build steps for the creation of a new branch vs a regular GIT push
When creating a new branch via the UI, you must select an existing branch as the source for the new branch - this will mean that the head commit of the new branch is always identical to the source branch and therefore you may not want to trigger a long build process unnecessarily if this is the case
Something like the below command may be added to the beginning of your build step definition to compare the head commit of the new branch with the source branch it was created from and either do nothing if they match (branch creation) or execute some code (if it's a regular branch push where the head commit is different).
NOTE: This requires a full clone to compare branches and may not be appropriate for every use case
1 2 3 4 5 6 7 8 9 10 11 12
# The example below is configured within a branch definition - if you have created a new feature branch from main for example - it will check to see if it's a new branch creation # (matching head commit) and exit if it is, or if it's a regular branch push - it will echo "Hello world!" - you can modify this in any way you like # NOTE: As you are comparing with the source branch, this will require a full clone of all branches and will cause increased build times - this may not be appropriate for every use case pipelines: branches: feature/*: - step: clone: depth: full script: - 'git fetch origin main && MAIN_COMMIT=$(git rev-parse origin/main) && CURRENT_COMMIT=$(git rev-parse HEAD) && if [ "$MAIN_COMMIT" = "$CURRENT_COMMIT" ]; then echo "Build skipped: Same commit as main branch"; exit 0; else echo "Hello world"; fi'
If all of the above solutions are not appropriate for your use case, we have an existing feature request logged with our developers to include an option to avoid triggering builds upon branch creation, you can Watch this for future updates and Vote for it to improve its visibility with regard to customer demand:
If you are encountering issues when following the steps contained within this troubleshooting article, please feel free to raise a support ticket or raise a community support ticket for further assistance.
Was this helpful?