Trigger Bamboo Plan based on Commit message in Bitbucket
Asked by Nic Wainwright:

\[UPDATE Dec 14, 2020\] - please see my comment to see how I worked around this.

Hey guys, thanks for the help.

I have 2 plans.

1st plan is triggered by any commits to my development branch. It checks for changes and changes version numbers in a version file and then commits the new version numbers back up to Bitbucket.

2nd plan checks out all the code (including new version number file) and builds and deploys my code bundle.

I have the first plan set to trigger on any commit EXCEPT commits that contain "\[ci\]" in the commit message, which is what I use to commit the version number changes. This is to prevent infinite looping. This is using Plan Config -\> Repositories -\> "Change Detection Options" -\> "Exclude Changesets" -\> with the regex .\*\\\[ci\\\].\*

I want the second plan to trigger only when commits to the same repo CONTAIN "\[ci\]" in the commit message (ex: "\[ci\] revving version numbers"). Note, I can't do this all in one Plan because then the re-checkout uses the initially checked commit hashes, not the updated one. And I can't use Plan dependencies to trigger the second build because it also will use the same commit hashes as first plan. I tried using the negative/inverse regex in the "Exclude Changesets" section by writing .\*\\\[ci\\\].\*\\n but this also does not work.

As it is right now, my second plan gets triggered twice, once on the normal commit to the dev branch, and again, when my first plan commits to dev branch. I need it to only trigger that second time.

I have seen people mention inverse regex, but I tried this and it does not work.

I have seen people recommend Script Runner, but I'd prefer to avoid buying extra licenses if possible. Has anyone else had success with this plugin though? If so, maybe I will look into it again if we can't solve this another way.

Please advise. Cheers


Answers:
author: Jonathan Etchepare Frino
body: A bit late but... I found a solution. You need to put a "negative" regular expression in the "Exclude changesets" section in the repository configuration, that include the word that you want.  
The regular expression is like :  

\^(?!.\*test).\*$  

with this, bamboo will only build commits with this word in the message

<br />

![bamboo.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/165895i55D86AEC17869361/image-size/large?v=v2&px=999 "bamboo.PNG")

comments: author: Nic Wainwright
body: Ah that's a fun workaround too. Thanks!


author: Nic Wainwright
body: So, I was able to get 'solve' this problem.

* I stopped using the Bitbucket interface to do my repository checkouts
  * Except for my pipeline-scripts repo, which I checkout manually at the beginning of plan so I can execute all my other stuff, including checkout script
* I now only checkout my pipeline-scripts repo using the bitbucket checkout.
* I have a script now that does the rest of the checkouts at the beginning of plan.
* My first plan still triggers, though, using Bitbucket trigger setup on all my repos I want to track/trigger on. I still use the exclude regex of: **.\*\\\[ci\\\].\***
* At the end of the first 'versioning' plan, I have a script that commits the changed repos
* I have the second plan set as a Child Plan of the first plan, using the 'Dependencies' tab in the first plan.
* The second plan has its own checkout script that checks out the needed repos (this will fetch changes from recent CI-related commits.

This setup no longer runs the second plan twice, and I don't have to worry about negative regex because I am using the Child Plan setup. Because I am using custom scripts to checkout code, I am not using Bitbucket's repository caching, which is why everything works now.

Hope this helps guys!


author: Aron Gombas _Midori_
body: Better DevOps Automation gives an elegant solution to this.

Create an automation rule:

* Add the [Commit Created trigger.](https://www.midori-global.com/products/better-devops-automation-for-jira/data-center/documentation/automation-triggers#commit-created-trigger) It makes the commit message available as the smart value [{{devops.commit.message}}](https://www.midori-global.com/products/better-devops-automation-for-jira/data-center/documentation/smart-values#commit).
* Add a condition that checks if the commit message contains "ci". (There is a condition that can match a regex to the smart value.)
  * If yes, start *Plan 2* . (Here is the recipe that shows [how to trigger a Bamboo build](https://www.midori-global.com/products/better-devops-automation-for-jira/data-center/documentation/continuous-integration/bamboo#after-changeset).)
  * Else start *Plan 1*.

It's rather straightforward.

comments: author: Nic Wainwright
body: Hey Aron, this looks like an awesome option if I was allowed to install extensions! Unfortunately I cannot do that.

Thanks for sharing this though :)


Tags: bitbucket

commit

trigger