How to pass jira issue ids to bamboo build plan variable?
Asked by Konrad Gancarz:

I'm looking for a way to integrate linked jira issues into build workflow.  
For every bamboo build I can browse linked jira issues in GUI. How can I access this information in build scripts?

I also tried creating release from jira, manually linking issues to release and then triggering build from jira. Unfortunately still I don't see how to gather related ticket keys.


Answers:
author: Daniel Brown
body: We had to create a script in the end as a workaround. We use the ${bamboo.planRepository.revision} variable to give us the related Bitbucket commit. All of our branches in bitbucket contain the linked JIRA ticket number within the commit details returned from the Bitbucket API. Our solution connects to the Bitbucket API and uses the above build variable to return all of the commit details, once returned we grep the response to identify the JIRA ticket number linked with the commit. You'll have to update the \<JIRA_ticket_prefix\> below to use your project's ticket prefix, in our case this is followed by 4 numbers at the moment, but if you have more you'll have to update the {4} below too.

Once we have the JIRA ticket number returned, we then save it to a file located: **/var/lib/bamboo-home/artifacts/variable_injection** but again, you can change this to whatever you wish.

```
#!/bin/bash
COMMIT=${bamboo.planRepository.revision}
BITBUCKET_CLIENT_ID=<enter_client_id>
BITBUCKET_CLIENT_SECRET=<enter_bitbucket_secret_key>
BITBUCKET_USERNAME=<enter_bitbucket_username>
BITBUCKET_PASSWORD=<enter_bitbucket_password>

OUTPUT_DIRECTORY=/var/lib/bamboo-home/artifacts

# Sign in to Bitbucket and get a token
ACCESS_TOKEN=$(curl -s -X POST -u "$BITBUCKET_CLIENT_ID:$BITBUCKET_CLIENT_SECRET" https://bitbucket.org/site/oauth2/access_token -d grant_type=password -d username=$BITBUCKET_USERNAME -d password=$BITBUCKET_PASSWORD | jq '.access_token')

# Get the JIRA ticket number from the commit
TICKET=$(curl -s https://api.bitbucket.org/2.0/repositories/<company_name>/<repo>/commit/$COMMIT?access_token=$ACCESS_TOKEN | jq '.rendered.message.raw' | grep -P '(<JIRA_ticket_prefix>.[0-9]{4})' -o | uniq)

echo "** Found JIRA ticket: $TICKET from commit: $COMMIT"

# Write the feature tag to a file for Bamboo
if [ -d $OUTPUT_DIRECTORY ]
then
echo "jiraticket=$TICKET" > "$OUTPUT_DIRECTORY/variable_injection"
else
echo "ERR - INVALID FILE LOCATION"
fi
```

Once all of this has finished, you should have a JIRA ticket number stored in a file **/var/lib/bamboo-home/artifacts/variable_injection**

Next, you can use the bamboo **Inject Variable** task to read the file **../../../artifacts/variable_injection** which contains your JIRA ticket number and creates a variable of **${bamboo.inject.jiraticket}**to then be used elsewhere in your build.

It seems pretty long-winded, but it's the only thing we could think of for the time being as a workaround until Bamboo creates its own JIRA ticket variable.

Hope this helps people,

Dan

comments: author: Darrin Khan
body: Hey Dan,

Thanks for sharing.

Answers my query also.

Cheers,

Darrin


author: Jeyanthan I
body: Hey [@Konrad Gancarz](/t5/user/viewprofilepage/user-id/3731474) ,

If I understand correctly, you wanted to refer the Jira isssue ID in Bamboo builds. I was going to suggest one of the default Bamboo variables for Jira. I then realised there is no variable to collect Issue ID that triggered the build.

Available JIRA variables (as of Bamboo 6.10.4) are as follows:

* ${bamboo.jira.baseUrl} - The URL of your JIRA application server.
* ${bamboo.jira.projectKey} - The key of the triggering JIRA application project.
* ${bamboo.jira.projectName} - The name of the triggering JIRA application project.
* ${bamboo.jira.username} - The username of the user who triggered the release build.
* ${bamboo.jira.version} - The release version of the triggering Jira application project.

I have therefore created a feature request on your behalf. Please upvote and comment.

* [BAM-20815](https://jira.atlassian.com/browse/BAM-20815)

As a workaround, I propose

1. Create a script Task within the plan.
2. In the script, make a REST call to Jira to fetch the corresponding Issue ID.

References to REST documentation:

* [Bamboo REST API](https://developer.atlassian.com/bamboodev/rest-apis)
* [Jira REST API](https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis)

Hope that helps!

Cheers,  
Jey

comments: author: Daniel Brown
body: Hi Jey,

How would we make a REST call to Jira to fetch the corresponding issue ID when the details aren't stored in Bamboo? Looking at the Jira REST API docs, we'd need to provide the issue ID in the call?

Please could you give an example?

Thanks,

Dan


Tags: issue

variable