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

Setting variables from within pipeline step

Andrei Dascalu June 7, 2022

Hello,

 

I have the following use case for bitbucket pipelines:

- i am building tags using a convention glob pattern, like "v?.?-*"

- my build scripts need to extract the first part as well as the second part of the glob (eg: the "?.?" as well as the "*")

 

I'm not sure if Pipelines provides a way to extract glob patterns as provided into variables available at run time, however I can do that manually via "sed". In thise case though, I would like to execute an initial step that performs splitting the current tag into its components and setting them into variables to be available for the following steps.

 

I know that Github actions allows one to set variables at runtime in a step so that they are available to subsequent steps. How can this be done with pipelines?

 

thanks

1 answer

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

Hi Andrei and welcome to the community!

It is possible to set variables at runtime for custom pipelines only. The variables you set for a custom pipeline will be available for all steps of that pipeline.

You can see an example in our documentation:

The custom pipeline in the example has only one step, but its variables will be available to any other steps you add to that pipeline.

Custom pipelines do not run automatically, they can only be triggered manually or scheduled from the Bitbucket Cloud interface.

 

If you don't want to use custom pipelines and you use ones running e.g. on tags, then you can pass variables from one step to the next one as follows:

In the first step, save these variables in an env file and define this file as an artifact:

- step:
script:
- echo export BuildTag="v1.2-1" > build.env
artifacts:
- build.env

In the next step, you can source this file using a command like the following:

- source build.env

and then you'll be able to use any variables you have defined in that file. With this solution you cannot provide the value of the variable at runtime before the build starts, this is going to work only if you can determine the value programmatically in the script and then assign it to the variable.

We have a feature request to allow users to define variables that can be shared across all steps in the yml file:

You can vote for that feature request if it's something you'd be interested in and add yourself as a watcher if you'd like to get notified via email on updates.

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

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events