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

How to reuse steps for different branches

fuxx August 25, 2018

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

10 votes
Answer accepted
Graham Gatus
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 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 '<<:'.

Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 22, 2019

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

Like # people like this
Kevin Coy February 19, 2020

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
Rohan Jaiswal April 27, 2021

@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

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events