By Jan Guardian, senior Web developer, Itransition
Do you ever feel like there’s too much unnecessary routine when it comes to changing the status of those nasty JIRA tickets when deploying your code for testing? We all know it’s not that big of a deal going to JIRA and pressing one button, but we also know everyone loves automation and hates repetitive manual operations.
Luckily for developers, Atlassian was kind enough to integrate Bamboo builds with JIRA issues workflow. As Atlassian puts it, “…you can configure a workflow in JIRA applications, so that the workflow is actioned when a build completes successfully.” This means that your JIRA issue can automatically be transitioned to another step in your workflow, depending on whether your build was successful. To do that, you need to use the native JIRA ‘Builds Workflow’ (whether default or modified) and specify the issue key in your commit message using smart commits:
Figure 1: Bamboo’s structure
JIRA-34 #wait-for-build
In theory, assuming you have JIRA and Bamboo integrated with one another, such a commit message should lead to issue JIRA-34 being actioned to follow transition with the name ‘wait-for-build,’ ultimately changing its status from ‘Open’/’Reopened’/’Build Broken’ to ‘Building.’
The Problem
This integration has so many limitations that, in fact, they render it completely useless. First of all, the workflow is somewhat a mess in the sense that it has too many transitions and lacks some crucial stages; for example, ‘Testing’ and respectively, ‘Reopened’/’For fix.’ The latter provides better progress transparency and allows you to prioritize work. The most frustrating issue, however, is that you might end up configuring this integration for days before you realize that it is not working at all. Then, you contact Atlassian support and receive the following reply:
Atlassian support’s reply |
“I’m afraid the functionality, to autotransition an issue based on the Bamboo build result, is only available if you have Bamboo Cloud + JIRA Cloud. We do have requests open to have this feature with Bamboo server https://jira.atlassian.com/browse/BAM-1362 and https://jira.atlassian.com/browse/BAM-14044 I encourage you to vote or comment on these requests to increase their visibility.” |
You read it right. Integration of Bamboo builds with JIRA issues workflow works only if you have JIRA Cloud and Bamboo Cloud. In regards to the suggestion to upvote issues, one should bear in mind this may take some time:
Figure 2: People complain about the situation
Perhaps it’s time to take matters into our own hands.
The Solution: Prerequisites
Note: The following guide assumes that your Bamboo runs on Linux and that your repository of choice is Bitbucket. |
1. Custom Workflow
The first thing you need to do is plan and draft your issue workflow so it corresponds to the needs of your team. When it comes to smaller Web app projects, I usually opt for the following workflow:
Figure 3: Charting a workflow
Technicalities aside, such a workflow provides for maximum transparency, enabling the team to progress each issue quickly and see exactly why things fail if they do.
2. Pre-Post Build Command Runner
Once you have your custom workflow in place, you need to install Pre-Post Build Command Runner on Bamboo:
- Log on to your Bamboo server and navigate to Bamboo plugins directory:
cd ~/atlassian-bamboo-x.x.x/atlassian-bamboo/WEB-INF/lib
- Download ‘Pre-Post Build Command Runner:’
wget https://marketplace-cdn.atlassian.com/files/artifact/3f77baa2- 8624-4a80-9b5d-1f6029fc1133/prepost-build-command-x.x.x.jar
- Restart Bamboo
cd ~/atlassian-bamboo-x.x.x/bin ./stop-bamboo.sh ./start-bamboo.sh
- Go to ‘Bamboo Administration/Add-ons’ and click ‘Pre-Post Build Command Runner’ under ‘User-installed add-ons.’
- Check that all its modules are enabled (it should say ‘X of X modules enabled’).
Figure 4: Checking that all modules are enabled
3. Configuring Build Stages in Bamboo
- Add two stages to your build: a ‘build stage and a ‘deployment’ stage:
Figure 5: Adding two stages to the buildNote: We include the deployment stage in the build plan intentionally. Rather than using the integrated deployment scenario offered by Bamboo, we opt for a custom deployment stage within a build for a sound reason. Bamboo treats its native deployment as a final step and does not allow you to perform any further actions. Automated tests, in turn, can only be part of the build plan in Bamboo. Hence, you would not be able to run automated tests on code deployed to your environment of choice with Bamboo deployment. In this sense, having a custom deployment stage as part of your build plan in conjunction with Pre-Post Build Command Runner allows for much more flexibility. - Add a job to the ‘build’ stage with tasks that package the files you want to deploy to an artifact.
- Add a job to the ‘deployment’ stage with tasks that send the artifact to your server of choice and unpack it there.
4. Writing Scripts
Open JIRA, go to your workflows, open the desired workflow in text mode, and note/write down the respective transition ID (these are the digits that you can find in brackets to the right of the transition name).
Figure 6: Noting the respective transition ID
- Install jq.
sudo apt-get install jq
Note: Further instructions will let you trigger a build by including a respective JIRA issue key in your commit message (for example, JIRA-123). - Next, we need to write a script that polls your repository, gets the message of the last commit you made to a desired branch, extracts the respective JIRA issue key from it, and writes it to a .txt file. Save it on your Bamboo server with a distinct name.
#!/bin/bash message=$(curl --user <bitbucket-username>:<bitbucket-password> https://bitbucket.org/api/1.0/repositories/<bitbucket-username>/ <bitbucket-repository>/branches/ | jq '.<branch-name>.message') message="${message%"} message="${message#"}" IFS=' ' read -a array <<< $message issue=${array[0]} echo $issue > issue.txt
- Now, we need to write a script that reads the .txt file, extracts issue key from there, and sends a POST request to JIRA API with respective transition id and issue key. Make sure to run ‘chmod +X ‘ on both scripts.
#!/bin/bash issue=$(<issue.txt) curl -D- -u <jira-username>:<jira-password> -X POST --data '{"transition":{"id":"<transition-id>"}}' -H "Content-Type: application/json" https://<jira-base-url> /rest/api/latest/issue/$issue/transitions?expand=transitions.fields rm issue.txt
Once done, save the script on your Bamboo server with a distinct name in the same place where you saved the previous script.
- Go to your Bamboo build configuration, open the ‘build’ stage, open the ‘miscellaneous’ tab, enter the location of the first script in the ‘Command’ line of the ‘Pre Build Commands’ section, tick ‘Run on Bamboo Server,’ and press ‘Save.’
Figure 7: Ultimately, pressing ‘Save’ - Finally, go to your Bamboo build configuration, open the ‘deployment’ stage, open the ‘miscellaneous’ tab, enter the location of the second script in the ‘Success Command’ line of the ‘Post Build Commands’ section, tick ‘Run on Bamboo Server,’ and press ‘Save.’
Figure 8: Pressing ‘Save’ againNote: Make sure that Bamboo, JIRA, and Bitbucket are authenticated to interact with one another. You can set up the credentials in JIRA, Bamboo, and Bitbucket settings under ‘Application links.’ Once you make a commit, include the JIRA issue key first in the commit message; for example, ‘JIRA-123 added some items.’ - Disable concurrent builds in Bamboo in case there’s a chance another developer might push a commit to the repository when your build is running.
Conclusion
The aforementioned setup is quite flexible in the sense that it also enables you to have a custom issue workflow, which accounts for different events that happen within your builds and deploys. Be it a corrupted connectivity to your repository, unsuccessful deployment, or failed automated tests, your custom shell scripts run at the beginning and/or end of each stage can transition your JIRA issue to any status and even leave respective comments:
Figure 9: You can leave comments
Flexibility is key, and we sincerely hope that Atlassian will look into this issue in the near future, enabling native support for builds workflow integration between JIRA and Bamboo Server. In the meantime, enjoy using this temporary, yet sound, solution.
About the Author
Jan Guardian graduated from the United Nations Interregional Crime and Justice Research Institute cum laude with an LL.M in International Crime and Justice. Although a lawyer by virtue of education, Jan has always been passionate about programming and Web development, having begun his endeavors in this field back in 2000. Since then, he has developed several dozen Web sites and mobile apps, with one of them being awarded by the United Nations for its contribution to human rights and resolution of justice problems. He joined Itransition as a senior Web developer and has become a valuable member of the team, bringing many creative and inspiring ideas to life.
This article was contributed for exclusive use on Developer.com. All rights reserved.