BITBUCKET_COMMIT variable - short (7digits) in pipelines

dev-mp
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!
March 17, 2018

I would like to use 'BITBUCKET_COMMIT' variable in pipelines to create unique file name. The problem is, that 40-digits SHA is not convinient to use in file names.

 

Is it possible to use 7-digits SHA environment variables? Or any way to process it in YAML?

4 answers

10 votes
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 27, 2018

Hi Milosz,

You can truncate the BITBUCKET_COMMIT variable inside of your pipeline script.

For example:

pipelines:
default:
- step:
script:
- export BITBUCKET_COMMIT_SHORT=$(echo $BITBUCKET_COMMIT | cut -c1-7)
- ...

You can see other examples of how to do this here: https://stackoverflow.com/questions/8928224/trying-to-retrieve-first-5-characters-from-string-in-bash-error

Thanks,

Phil

Alexander Weickmann
Contributor
July 1, 2019

Would be easier if pipelines would that work already for us. Also, for pipelines it would be easy to use git commands instead of bash tricks. Git has a "safety" mechanism that will make the short commit hashes it hands out longer if the default 7 chars are not unique.

Like # people like this
Joshua Behrens
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!
January 5, 2023

Yes, and no. We use BITBUCKET_COMMIT to compare it with git rev-parse. Now half a decade later I land here is because the variable randomly got shortened and does not equal to git rev-parse anymore :(

4 votes
Mike_Stead
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!
February 18, 2020

As a quick ref, this works too for at least bash

export BITBUCKET_COMMIT_SHORT="${BITBUCKET_COMMIT::7}"

Details http://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion 

Agree with @Alexander Weickmann 

Mateusz Przybyłek
Contributor
January 24, 2022

Best answer, in sh works too

0 votes
Jeroen Lodder August 16, 2023

What would be an equivalent for this in Windows Runners?

0 votes
Ahmad Siar Mehri
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 8, 2020

You can get your git commit pretty format with the following:

- export COMMIT=$(git log --pretty=format:'%h' -n 1)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events