25

I'm trying to tag the git repo of a ruby gem in a Bamboo build. I thought doing something like this in ruby would do the job

`git tag v#{current_version}`
`git push --tags`

But the problem is that the repo does not have the origin. somehow Bamboo is getting rid of the origin Any clue?

Allen Bargi
  • 14,674
  • 9
  • 59
  • 58

3 Answers3

54

Yes, if you navigate to the job workspace, you will find that Bamboo does not do a straightforward git clone "under the hood", and the the remote is set to an internal file path.

Fortunately, Bamboo does store the original repository URL as ${bamboo.repository.git.repositoryUrl}, so all you need to do is set a remote pointing back at the original and push to there. This is what I've been using with both basic Git repositories and Stash, creating a tag based on the build number.

git tag -f -a ${bamboo.buildNumber} -m "${bamboo.planName} build number ${bamboo.buildNumber} passed automated acceptance testing." ${bamboo.planRepository.revision}
git remote add central ${bamboo.planRepository.repositoryUrl}
git push central ${bamboo.buildNumber}
git ls-remote --exit-code --tags central ${bamboo.buildNumber} 

The final line is simply to cause the task to fail if the newly created tag cannot be read back.

EDIT: Do not be tempted to use the variable ${bamboo.repository.git.repositoryUrl}, as this will not necessarily point to the repo checked out in your job.

Also bear in mind that if you're checking out from multiple sources, ${bamboo.planRepository.repositoryUrl} points to the first repo in your "Source Code Checkout" task. The more specific URLs are referenced via:

${bamboo.planRepository.1.repositoryUrl}
${bamboo.planRepository.2.repositoryUrl}
...

and so on.

RCross
  • 4,919
  • 4
  • 44
  • 42
  • 3
    It doesn't seem that Bamboo gives you the password for any of the repositories. How are you handling that? – z4ce Jan 28 '15 at 21:23
  • 3
    You need to setup public key for the user bamboo is running as on the build server. – Alex Barker Apr 10 '15 at 17:57
  • I am using github repository with onDemand bamboo. How do you setup this public key? Further more each time I'm trying to biuld there is an information that remote branch already exists – Rafał Łyczkowski May 18 '16 at 16:54
  • That last point about planRepository is correct; but how does my script know which planRepository (number) is the one currently being built? Bamboo seems to provide environment vars for all of them, not just the one being built. – jonny99 Sep 22 '16 at 18:36
  • 1
    All of the repositories listed under the repositories tab on the plan configuration are given numbers in the order they appear starting with the number 1. – Bikerdan Sep 28 '16 at 22:31
  • 3
    To get this to work is a lot more involving, it requires setting up SSH and getting Bamboo to use the right SSH Key, here's how https://almfirst.wordpress.com/tagging-from-bamboo/ – James Z. Nov 30 '16 at 13:06
  • 1
    When I run this it seems to work fine up until `git push central ${bamboo.buildNumber}` I get that "Repository not found The requested repository does not exist, or you do not have permission to access it". Bamboo seems to have added all the ssh keys correctly for my repo. Have any ideas for this error? – ThrowsException Mar 22 '17 at 15:32
  • @ThrowsException It must be something missing in your setup. After a failed build, log on to the relevant Bamboo Agent, become the user that runs the Bamboo Agent process, navigate to the workspace of your recent build and troubleshoot from there. – RCross Feb 14 '18 at 13:47
6

I know this is an old thread, however, I thought of adding this info.

From Bamboo version 6.7 onwards, it has the Git repository tagging feature Repository Tag.

You can add a repository tagging task to the job and the Bamboo variable as tag name. You must have Bamboo-Bitbucket integrated via the application link.

Jijo John
  • 1,368
  • 2
  • 17
  • 31
  • I think this is the ideal answer for Bamboo 6.7+, but as of Bamboo 9.0, their Repository Tag task doesn't support `git tag -f` to force push a tag (https://jira.atlassian.com/browse/BAM-20331). So if you need to force push a tag, you're stuck with using the script that @rcross posted. – Jeff P Nov 16 '22 at 16:32
1

It seems that after a checkout by the bamboo agent, the remote repository url for origin is set as file://nothing

[remote "origin"]
url = file://nothing
fetch = +refs/heads/*:refs/remotes/origin/*

That's why we can either update the url using git remote set-url or in my case I just created a new alias so it does not break the existing behavior. There must be a good reason why it is set this way.

[remote "build-origin"]
url = <remote url>
fetch = +refs/heads/*:refs/remotes/build-origin/*

I also noticed that using ${bamboo.planRepository.<position>.repositoryUrl} did not work for me since it was defined in my plan as https. Switching to ssh worked.

Paul
  • 588
  • 1
  • 4
  • 16