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

How to use YML anchors with dynamic variables / parts?

Ben June 27, 2020

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
caches:
- node
script:
- 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!

Thanks.

2 answers

1 vote
ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 27, 2020

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.

https://support.atlassian.com/bitbucket-cloud/docs/variables-in-pipelines/#:~:text=Deployment%20variables,are%20unique%20to%20each%20environment.

Ben June 27, 2020

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 # people like this
ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 28, 2020

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

0 votes
ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 5, 2020

@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:

custom:
lint-php-5.3:
- step: &lint-php53
image: php:5.3
script:
- lib/pipelines/lint.sh
# ...
lint-php-7.4:
- step: &lint-php74
<<: *lint-php53
image: php:7.4
lint-php:
- 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: https://github.com/ktomk/pipelines/blob/master/bitbucket-pipelines.yml#L14-L40 (the highlighting will change over time and appear misplaced in the future).

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events