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

Can a default variable be used as repository user variable?

AZZ October 31, 2022

Hello,

 the idea is to define an IMAGE repository variable as a combination of default variables so that all steps in a pipeline can refer to it without redefining:

on the repo level in the "REpository variables":

IMAGE -

$BITBUCKET_REPO_SLUG_$BITBUCKET_BRANCH

 

in the pipeline:

steps:
- step: &Build_And_Scan
name: Build & Run Static Security Code Dependency Test
script:
- echo $IMAGE
- export DOCKER_FILE="dockerfile"
- docker builder build -t $IMAGE -f $DOCKER_FILE .

1 answer

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 2, 2022

Hi @AZZ,

I'm afraid that it is not possible to use the value of default variables when defining a repository variable. We have a feature request about this in our issue tracker:

I would suggest adding your vote to it (by selecting the Vote for this issue link) and leaving a comment to express your interest. You can also add yourself as a watcher (by selecting the Start watching this issue link) if you'd like to get notified via email on updates.

In the meantime, a way to work around this issue would be by defining the variable in the script of the steps that are using it. For example, in the step you posted here you can do the following:

steps:
- step: &Build_And_Scan
name: Build & Run Static Security Code Dependency Test
script:
- IMAGE=${BITBUCKET_REPO_SLUG}_${BITBUCKET_BRANCH}
- echo $IMAGE
- export DOCKER_FILE="dockerfile"
- docker builder build -t $IMAGE -f $DOCKER_FILE .

Please keep in mind that $BITBUCKET_BRANCH is only available on branches, it is not available for builds against tags, or custom pipelines.

If you have any questions, please feel free to let me know.

Kind regards,
Theodora

AZZ November 2, 2022

@Theodora Boudale,

 thank you for the answer. I added my voice to the ticket.

 

In the above situation - is there a way to avoid defining the same variable for each step?

The end goal is to do this - https://community.atlassian.com/t5/Bitbucket-questions/Pipelines-Image-name-as-an-artifact/qaq-p/2179719

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 4, 2022

You are very welcome @AZZ.

If you want to pass the value of the variable, this is possible by adding some additional commands in your bitbucket-pipelines.yml file that write the value to a file, pass the file as an artifact, and then source the file in subsequent steps.

Please see an example below:

pipelines:
default:
- step:
script:
- IMAGE=${BITBUCKET_REPO_SLUG}_${BITBUCKET_BRANCH}
- echo export IMAGE=${BITBUCKET_REPO_SLUG}_${BITBUCKET_BRANCH} >> build.env
- echo $IMAGE
artifacts:
- build.env
- step:
script:
- source build.env
- echo $IMAGE

Please feel free to let me know if that works for you and if you have any questions.

Kind regards,
Theodora

Suggest an answer

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

Atlassian Community Events