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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to use YML anchors with dynamic variables / parts?

Hello people!

I'm using yml anchors in my pipelines already and wanted to clean it up even more.

Right now I've an anchor step like that:

- step: &build-frontend
name: Build application and generate artifacts
- node
- echo $DOTENV_STAGING | base64 --decode > .env
- yarn
- yarn build

This step just builds my frontend code with a given .env file I have saved as base64 encoded string on my pipeline deployment variables.

The problem now is, I want to use the exact same step with a different variable on production as well, in that case it must be $DOTENV_PRODUCTION. How can I accomplish this somehow?

I know I could overwrite the whole step but then I also do not have to use the anchors. ;-)

What would also work is using the deployment name in an if condition but unfortunately it looks like there is no variable in the default variables where I can get it from?

I lost my mind for some reason and can't find any good solution.

Or maybe there is something more elegant?

Nice to hear from you!


2 answers

@Ben as an alternative answer next to YAML anchors is to combine it with Merge Key Language-Independent Type for YAML™ Version 1.1 which allows to mix-in only the changing mapping with a base (definition).

Not so sure how well it works with arrays as it is about maps, here is an example which is working on Bitbucket Pipelines where I don't change a variable between two "same" steps, but the image name. That is linting the source-code with PHP 5.3 and then with PHP 7.4. Both steps are the same and only the image differs:

- step: &lint-php53
image: php:5.3
- lib/pipelines/
# ...
- step: &lint-php74
<<: *lint-php53
image: php:7.4
- step: *lint-php53
- step: *lint-php74

Note the `<<:`  part. Ignore in this example it's not anchoring from `definitions`, it should be similarly possible, here in the example it's just all self-contained within the custom pipelines.

This is a real-life example, you can find the full file here: (the highlighting will change over time and appear misplaced in the future).

You can set variables per deployment environments in Bitbucket Pipelines. This might do it already in your case.

Deployment variables

You can also define variables so that they can only be used in a specific deployment environment.

You can manage deployment variables in Settings > Pipelines > Deployments.

Deployment variables override both team and repository variables, and are unique to each environment.,are%20unique%20to%20each%20environment.

Unfortunately this is not possible in my case. The problem is that I've more than one step within a set of parallel steps where I would have to use the deployment: thing in pipelines.

And using it is only supported once for a pipeline.

Multiple usage is not supported.

Like Mateusz Przybyłek likes this

Is it an option for you to (better/more) divide the build of the package from the deployment itself?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

322 views 3 14
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you