A Cloud Architect Company
bitbucket pipeline trigger
DevOps

Bitbucket pipeline trigger only when changes made in a particular folder

Requirement

We had a very custom use case requirement from our client asking to trigger only when there is a change in a specific folder. We checked over the internet and couldn’t find any proper document so we decided to make one that may help others on the internet.

Prerequisites 

  • Bitbucket account
  • Yaml file (bitbucket-pipelines.yml)

 

Scenario 1

We want to execute build whenever any change is done to the files or folders inside a particular directory. In the use case, we can use conditions in the bitbucket-pipelines.yml file, which allows us to execute when certain conditions or rules are satisfied. Use changesets to execute a step only if one of the files modified matches the expression in include paths.

In our repo we have one folder named newbuild and bitbucket-pipeline.yml file. We want to ensure that pipeline is triggered only when the changes are made to the newbuild folder and not outside it.

Now in the repo, the CI/CD pipeline should trigger only when I make changes to newbuild folder and if I make changes to any other file the repo then the CICD pipeline should not trigger, the below code section shows the content of my bitbucket-pipelines.yml file which includes the condition block in the pipeline.yml file.

image : docker:latest
definitions:
steps:
- step: &build-test-trigger
name: custom trigger in bitbucket
condition:
changesets:
includePaths:
#Code block for changes to trigger on newbuild folder.
- "newbuild/**"
script:
- echo "test"
pipelines:
branches:
master:
- step: *build-test-trigger

Below is the screenshot of the pipeline that ran the build step when I committed changes to newbuild folder.

Note: If the newbuild folder files have no changes, then that step is skipped and the pipeline prints success message but with no skipped action. As shown in the below screenshot.

Scenario 2

Separate build steps to run for the file changes in separate folders in the same repository.

Let’s say you have two folders like newbuild1 and newbuild2 and you want to execute separate build commands for newbuild1 folder and newbuild2 folders respectively.

You can reference the below commands of bitbucket-pipelines.yml files. Depending on which folder is committed the respective bitbucket pipeline is triggered. The below pipeline file will print test1 when the changes to newbuild1 folder is commited. The same pipeline file will print test2 when the changes to newbuild2 folder is committed, it will skip both the pipelines when a change is committed to folders other than newbuild1 and newbuild2.

image : docker:latest

definitions:
 steps:
 - step: &build-test-trigger
name: custom trigger in bitbucket newbuild1 folder
 condition:
 changesets:
 includePaths:
# code to monitor changes on newbuild1 folder
- "newbuild1/**"
 script:
 - echo "test1"
 - step: &build-test-trigger2
name: custom trigger in bitbucket newbuild2 folder
 condition:
 changesets:
 includePaths:
# code to monitor in newbuild2 folder
- "newbuild2/**"
 script:
 - echo "test2"
pipelines:
 branches:
 master:
 - step: *build-test-trigger
 - step: *build-test-trigger2

The below screenshot is example of pipeline output when the changes to newbuild2 folder is committed.

Note: If there are some other changes done to the repo apart from the newbuild1 and newbuild2 folder then both the steps are skipped and the pipeline will show as successful. As shown below.

Please change the echo command in the pipeline codes with your build commands to run the pipelines. Thank you for visiting our blog post. You can contact us anytime if you have any custom use case and we will be able to help.

 

Feel free to share it with your colleagues.

Join the discussion

  1. rohan

    I am using the same script but for me pipeline is not detecting, and i validated ,mine is valid yml

    • Siranjeevi R

      Sorry about the delayed response. We are not very active in checking our blog comments, can you share details on your error message rohan ?

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top
advanced-floating-content-close-btn

Contact Us to save your AWS bill by 40%

X