Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How can i save the output of a GIT command into a variable?

Mattia Montani
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!
July 25, 2022

Hello folks, i have to retrieve the git tag of the branch and save it to a variable, so i can name an artifact with thata value.

 

here is my pipeline step:

 

image: mcr.microsoft.com/dotnet/sdk:5.0
pipelines:
pull-requests:
'**':
- step:
script: # Modify the commands below to build your repository.
- git status
- git fetch --prune
- export PROJECT_NAME=ProvaPipelineDotNet.sln
- export TEST_NAME=TestProject1
- export GIT_TAG=`git describe --tags --always --abbrev=0`
- echo ${GIT_TAG}
- dotnet restore
- dotnet build $PROJECT_NAME
- dotnet test --logger "html;logfilename=$BITBUCKET_CLONE_DIR/testResults.html"
- dotnet publish --no-restore -c Release -o $BITBUCKET_CLONE_DIR/dist
- apt-get update
- apt-get -qq install zip curl
- zip -r $GIT_TAG.zip $BITBUCKET_CLONE_DIR/dist/
artifacts:
- ${GIT_TAG}.zip
- testResults.html

1 answer

0 votes
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 27, 2022

Hello @Mattia Montani ,

Welcome to Atlassian Community!

Checking your yml file I think the syntax you have used is slightly wrong. Please refer to the example below on how to save the output of a command into an environment variable :

export GIT_TAG="$(git describe --tags --always --abbrev=0)"

You can replace your current command with the one above and the GIT_TAG env variable should have its value updated with the string returned by the git command.

Hope that helps! Let me know in case you run into any issues.

Thank you, @Mattia Montani .

Kind regards,

Patrik S

Mattia Montani
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!
August 1, 2022

Hi Patrick, tnx replying. It works but i still have an issue related to naming the zip file as the output of the git command.

It give me this error:

the file ".zip" doesnt exist.

but when I try to echo the env variable it has the correct value. 

Can you please tell me if the syntax is correct?

Thank you and Kind regards

Mattia

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 2, 2022

Hello Mattia,

The syntax for creating the naming of the zip file is correct, however I'm afraid environment variables are currently not yet supported to be referenced in the artifacts section, as you did in :

artifacts:

${GIT_TAG}.zip

We have a feature request to implement this functionality though, which you can find in the following link : 

I would suggest you to add your vote there, since this helps both developers and product managers to understand the interest. Also, make sure you add yourself as a watcher in case you want to receive first-hand updates from that ticket. Please note that all features are implemented with this policy in mind.

While this feature is not implemented, you can reference the file using glob patterns, like the below example : 

artifacts:

*.zip

This would get any file with the extension .zip in the current directory and make it an artifact. If you have more than one zip file in that path, they would all be included in the artifact.

If you want just one specific zip file as your artifact, you can create a new folder and put the zip file in there, and reference this folder in the artifacts section :

artifacts:

- my_custom_folder/*.zip

 Let me know in case you have any questions.

Kind regards,

Patrik S

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events