How to get BITBUCKET_BRANCH in a step within tags?

sayantan.roy October 22, 2019

I have a tag defined as following. And I want to retrieve the BRANCH name from where this pipeline is triggered. As per the documentation, the $BITBUCKET_BRANCH variable is only available for branches. 

I need to get BRANCH name for the step "Send Slack". Any suggestion would be appreciated.

tags:
'dev/trigger':
- step:
name: Deploying to Dev
deployment: dev
script:
- git branch
- pipe: docker://<pipe_name>
variables:
NAMESPACE: $NAMESPACE
BUILD_HELM: $BUILD_HELM
- step:
name: Send Slack
script:
- declare -x VERSION=$(cat version.txt)
- pipe: atlassian/slack-notify:0.3.2
variables:
WEBHOOK_URL: $SLACK_URL
MESSAGE: "Deployed from Pipeline to env:DEV project:${PROJECT} version:${VERSION} "

 

1 answer

0 votes
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 23, 2019

Hi @sayantan.roy

Pipelines don't provide the branch name variable because there is a conceptual problem here. A tag can be part of multiple branches. It references directly a commit and this commit can be part of multiple branches.

If you want to know what are the branches that contain a given tag, you can use the following git command:

git branch --contains tags/<TAG_NAME>

Depending on the tag that command will return one of the multiple branches and you will need to parse it to decide which branch will be the one feeding your script.

I hope that helps yo clarify the issue. 

Jeff Chard
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 2, 2020

Hi @Daniel Santos -

I tried doing exactly this, but it didn't give the expected result:

git branch --contains tags/patch-1263-5
Tag patch-1263-5 is in branch ['* (no branch)']

 

The same command in a local console (after the pipeline has completed and I do a git pull does give the expected result:

$ git branch --contains tags/patch-1263-5
* ISW-1263

 

So, why is it different from within the pipeline?  And is there some trick to get the pipeline to return the same branch(es) that I see in a local console?

 

-Jeff

Like # people like this
Patrick Nelson
Contributor
December 1, 2023

Did you ever figure this out?

I'm getting the same exact result and I'm having a hard time trying to figure out how to restrict tags like `0.0.0` to only be on `master` 😓

Patrick Nelson
Contributor
December 1, 2023

For me, the fix was a very special/secret incantation of both the following in a step (like this)

clone: 
depth: full

And then in the "script"

- git checkout master 

 

This was to ensure that I can run the following just to validate that a tag was in the master branch.

- export TAG_INFO=`git branch --contains tags/$BITBUCKET_TAG 2>/dev/null`

And wow... that was painful! Looking back I pushed 18 tags over an hour or so just to figure it out.

Like Matthias Maier likes this
Matthias Maier
Contributor
December 5, 2024

thanks for the hint with:

clone: 
depth: full

 it solved the problem for me too.

BTW: "git checkout" is not required for "git branch --contains"
For me it worked to use:

git branch -a --contains tags/${BITBUCKET_TAG}

 or

git branch -r --contains tags/${BITBUCKET_TAG}

because there are no local branches the -r (remote branches) or -a (all branches) helps

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events