Create
cancel
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 reuse steps for different branches

I've recently started using Pipelines for my project and I solved most of my problems from testing to deployment with Rails. 

When I used Pipelines for my feature and unit tests only I have handled everything in my default step because I wanted to know if my feature branches broke things. Since I want to use Pipelines as continuous deployment too I've found out that my default step is not invoked as soon as you specify something for a specific branch. The question is if there is a way to reuse code within the YAML file as my file has a lot of duplicated lines already. Or is there maybe a better way as what I do?

This is my current pipeline file, I have changed some values for privacy reasons.

 

image: ruby:2.5.0

pipelines:
default:
- step:
caches:
- bundler
script:
- curl -sL https://deb.nodesource.com/setup_8.x | bash -
- apt-get update && apt-get install -y nodejs apt-transport-https
- apt-get install -y build-essential libpq-dev cron vim imagemagick mysql-client
- export DATABASE_URL=mysql2://test_user:test_user_password@127.0.0.1/example_ci_test
- cp config/database.ci.yml config/database.yml && cp config/secrets.ci.yml config/secrets.yml
- bundle install --retry 5 --path vendor
- RAILS_ENV=test bundle exec rake db:drop db:create db:migrate
- bundle exec rspec --format documentation
services:
- redis
- mysql
master:
- step:
caches:
- bundler
script:
- curl -sL https://deb.nodesource.com/setup_8.x | bash -
- apt-get update && apt-get install -y nodejs apt-transport-https
- apt-get install -y build-essential libpq-dev cron vim imagemagick mysql-client
- export DATABASE_URL=mysql2://test_user:test_user_password@127.0.0.1/example_ci_test
- cp config/database.ci.yml config/database.yml && cp config/secrets.ci.yml config/secrets.yml
- bundle install --retry 5 --path vendor
- RAILS_ENV=test bundle exec rake db:drop db:create db:migrate
- bundle exec rspec --format documentation
services:
- redis
- mysql
- step:
caches:
- bundler
name: Deploy to integration
deployment: staging
script:
- curl -sL https://deb.nodesource.com/setup_8.x | bash -
- apt-get update && apt-get install -y nodejs apt-transport-https
- cp config/database.ci.yml config/database.yml && cp config/secrets.ci.yml config/secrets.yml
- gem install bundler
- RAILS_ENV=production bundle install --path vendor
- export DEPLOY_PATH=/home/x/integration.intern.x.com
- export SERVER_HOST=88.x.x.x
- export SERVER_PORT=22
- export SERVER_USER=x
- RAILS_ENV=production bundle exec rake docker:deploy
- step:
caches:
- bundler
name: Deploy to production
deployment: production
trigger: manual
script:
- curl -sL https://deb.nodesource.com/setup_8.x | bash -
- apt-get update && apt-get install -y nodejs apt-transport-https
- cp config/database.ci.yml config/database.yml && cp config/secrets.ci.yml config/secrets.yml
- gem install bundler
- RAILS_ENV=production bundle install --path vendor
- export DEPLOY_PATH=/home/x/intern.x.com
- export SERVER_HOST=88.x.x.x
- export SERVER_PORT=22
- export SERVER_USER=x
- RAILS_ENV=production bundle exec rake docker:deploy

definitions:
caches:
bundler: ./vendor
services:
redis:
image: redis
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: example_ci_test
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
MYSQL_USER: test_user
MYSQL_PASSWORD: test_user_password

 

Thanks in advance!

1 answer

1 accepted

8 votes
Answer accepted
Graham Gatus Atlassian Team Aug 30, 2018

@fuxx, you can use YAML anchors and references to reduce duplication in your pipelines yaml file, which are part of the YAML specification.

e.g Below I've step up 2 steps, with both steps re-using whats been defined in 'commonStep'.

commonStep: &commonStep 
step:
script:
-
echo "hello"
-
echo "world"

pipelines
:
default:
-
<<: *commonStep
-
<<: *commonStep

The first block of yaml steps up a step and assigns an anchor to it. We can then reference the anchor and inline the yaml associated with that anchor using '<<:'.

I just want to add that we have the following page as a reference on this topic:

Like # people like this

It seems like if I use an anchor to create a Maven settings.xml file via a script in a step that it doesn't get found in the subsequent steps. Is this expected? Is there a workaround?

Like raymond Kelly likes this

@Daniel Santos 

Maybe add some more keywords like "reusable steps" or "step as variables"

I was searching for this document for hours, but passed by YAML anchors many times

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

📣 Calling Bitbucket Data Center customers to participate in research

Hi everyone, Are you Bitbucket DC customer? If so, we'd love to talk to you! Our team wants to dive deep to understand your long-term plans regarding Bitbucket DC and Atlassian Cloud. Do you plan...

79 views 2 3
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