Bamboo release create Git tag
Asked by Ondrej Medek:

When I create a new release in my Bamboo Deployment project, is it possible to automatically create `git tag` with the release label in all the git repositories related to the release?


Answers:
author: Tim Crall
body: Bamboo does not have a way to automatically tag a commit in Git, but it is possible to do this with a little scripting.

Setup SSH Certificates {#toc-hId-479861301}
===========================================

One problem is that the Bamboo build machines (agents) do not have the same SSH certificates set up as does the Bamboo server itself. Therefore in order to authenticate from the Bamboo agent, you will have to create a pair of SSH keys and add them to your repository.

Clone your repository onto a linux machine or a Macintosh and follow the steps shown below.

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      % git clone ssh://git@stash.mycompany.com/project/helloworld.git % cd helloworld % mkdir ssh_keys % cd ssh_keys % ssh-keygen -t rsa -b 4096 -C "bamboo@mycompany.com" Enter file in which to save the key (/Users/crallt/.ssh/id_rsa): ./id_rsa ***(do not accept the default value or it will place your files in \~/.ssh and overwrite any keys already there!)*** Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in ./id_rsa. Your public key has been saved in ./id_rsa.pub.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Now create a file named **ssh_proxy.sh** within your **ssh_keys** directory | % cd ssh_keys % vi ssh_proxy.sh ***(or editor of your choice)*** | |------------------------------------------------------------------| paste in the following text and save the file. | #!/bin/bash THISDIR="\`dirname "$0"\`" set -vx ssh -o StrictHostKeyChecking=no -i $THISDIR/id_rsa $1 $2 $3 $4 | |---------------------------------------------------------------------------------------------------------------| This creates a shell script that will launch the SSH command using the provided public key. Add this new directory to your repository. | % git add . % git commit -m "Added SSH Keys" % git push | |---------------------------------------------------------| Now cat the contents of the id_rsa.pub file, select, and copy it to your clipboard with CNTRL-C or CMD-C as appropriate. ![](https://confluence.cdk.com/download/attachments/51752963/Screen%20Shot%202015-09-08%20at%203.18.41%20PM.png?version=1&modificationDate=1441752241734&api=v2) Go to the web interface for your Project in Stash. Click on **Settings** (note: you must have Administrator access on the repo to do this - if you do not, please consult a Project Administrator). Select **Access Keys** . Click "**Add key**". **![](https://confluence.cdk.com/download/attachments/51752963/Screen%20Shot%202015-09-08%20at%203.28.12%20PM.png?version=1&modificationDate=1441752241763&api=v2)** Select **Read/Write** and paste in the contents of the id_rsa.pub file which you previously copied onto your clipboard. Click "**Add key**" ![](https://confluence.cdk.com/download/attachments/51752963/Screen%20Shot%202015-09-08%20at%203.27.56%20PM.png?version=1&modificationDate=1441752241755&api=v2) Script the Build Plan {#toc-hId-1367364982} =========================================== Now that your SSH keys have been set up, it's time to add a script to your Bamboo build plan. Browse to your Build Plan within Bamboo and select Actions-\>Configure Plan. Select the stage where you want to add the tagging and click Add task. Scroll down and click on "Script" Our script has to do three basic tasks. First, it has to configure itself to use our provided SSH keys. Then it has to apply the desired tag on the local clone of the git repository. Finally, it has to push the change back to Stash. This is complicated slightly by the fact that Bamboo does a weird kind of cloning that not retain the "origin" information. So we have to create a new remote and assign it the correct URL to push back to. ### **For a Linux Build Agent:** {#toc-hId--1360040263} Click **Add task** and select "Script" ![](https://confluence.cdk.com/download/attachments/51752963/Screen%20Shot%202015-09-10%20at%2012.23.16%20PM.png?version=1&modificationDate=1441913051078&api=v2) For the script body, enter something like this: ![](https://confluence.cdk.com/download/attachments/51752963/Screen%20Shot%202015-09-08%20at%203.40.02%20PM.png?version=1&modificationDate=1441752241770&api=v2) ### For a Windows Build Agent: {#toc-hId--472536582} Click **Add task** and select "PowerShell Task" ![](https://confluence.cdk.com/download/attachments/51752963/Screen%20Shot%202015-09-10%20at%2012.23.42%20PM.png?version=1&modificationDate=1441913136862&api=v2) For the script body, add something like this: ![](https://confluence.cdk.com/download/attachments/51752963/Screen%20Shot%202015-09-10%20at%2012.27.47%20PM.png?version=1&modificationDate=1441913305089&api=v2) ### Conclusion {#toc-hId-414967099} You will want to change line 12 ("git tag") in either script with something that puts the desired tag onto the commit - probably pulling it out an environmental variable that you've defined for that purpose. The above script also assumes that it is the master branch that you are wanting to tag - modify accordingly otherwise. Once you've done this, you should be able to run your build plan and tag the latest commit. |

comments: author: Ondrej Medek
body: Thanks for the comprehensive answer. I just cannot see the pictures.

Anyway, as I understand it, you advice me to tag every build by a *Build Plan Task* . However, I would like to tag the repository, when a *Deployment Release* is created. E.g. `release-1.2`. I see I can add a task to the *Deployment Environment* . So maybe I can create a fake Deployment Environment `Git Tag` with a task to tag the repository by `${bamboo.deploy.release}`.


author: Tim Crall
body: sorry, I copied and pasted from our own wiki, looked to me like the pictures came through

also sorry if I misread your question slightly. I think tagging the git repo from the deployment task will be harder because at that point the agent doesn't even have the git repo in its working directory. You could create g it tag task but you'd need a way for it to know the URL of the git repo. You might have to create that as an artifact in the build stage, or something like that. That's off the top of my head.


author: Tim Crall
body: I've added my answer above, with the images, here, in case it helps: <https://almfirst.wordpress.com/tagging-from-bamboo/>


author: Bill Wang
body: [@Tim Crall1](/t5/user/viewprofilepage/user-id/781561)

If you saved the ssh key in this repo directly, that means the ssh key is easily exposed, anyone who get the ssh private key/public key, he/she can clone it.

That's not secure enough.


author: PJ Champion
body: Pictures are broken


author: Jobin Kuruvilla [Adaptavist]
body: You will have to add a script task that will check out the latest release, create the tag and push it. You can use the release number to tag the release.


Tags: cloud

git