Missed Team ’24? Catch up on announcements here.

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

Pipelines Anchors/Aliases for Script Command Snippets

logsdonj April 5, 2019

I've seen answers for reusing steps, but are there any for certain script commands to run?

I'm looking to do something along the lines of this:

commonScriptLinesAnchor: &myCommonScriptLinesAnchor
- echo "hello world from myCommonScriptLinesAnchor"
- echo "hello world again from myCommonScriptLinesAnchor"

pipelines:

custom: # Pipelines that are triggered manually via the Bitbucket GUI

anchor-test: # Use anchors and references in various ways
- step:
name: Common Script Lines
script:
- echo "hello world before from myCommonScriptLinesAnchor"
*myCommonScriptLinesAnchor
- echo "hello world after from myCommonScriptLinesAnchor"

 

The kind of resources I've come across:

https://community.atlassian.com/t5/Bitbucket-questions/How-to-reuse-steps-for-different-branches/qaq-p/875615

https://confluence.atlassian.com/bitbucket/yaml-anchors-960154027.html

 

Thanks!

2 answers

1 accepted

4 votes
Answer accepted
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 11, 2019

Hi Joshua,

I'm not sure if you can interleave anchors and other items into the YAML like that.

If you remove the hello world before and after your myCommonScriptLinesAnchor then it will work fine.

As far as I can find, there isn't a way to merge arrays in YAML using anchors. https://stackoverflow.com/questions/24090177/how-to-merge-yaml-arrays/30770740#30770740

All the things I tried also had a similar result where it was a list of list of commands, rather than a list of commands. Which our parser doesn't support.

Instead you could try use a script file to pull out common functionality between pipeline definitions.

Thanks,

Phil

0 votes
A360 Bitbucket Admin November 28, 2019

You can anchor a script the same way you anchor a step (I discovered this by accident -could not find any documentation for it):

definitions: 
script: &My-Script-A
- ...
script: &My-Script-B
- ...
pipelines:
custom:
my-custom-pipeline:
- step:
script: *My-Script-A
script: *My-Script-B

Full disclose, the above used to work just fine until a couple of days ago. For some reason when multiple scripts are referenced (like above) only the last one (My-Script-B) gets executed. See related post: https://community.atlassian.com/t5/Bitbucket-questions/Pipeline-with-multiple-scripts-runs-only-the-last-one-broken-bug/qaq-p/1239759#M49684

mjpieters
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!
June 8, 2020

This doesn't work, because the step value is a mapping, and keys are unique. The second `script` key replaced the first, which is why only the last one is being executed.

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events