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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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

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

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

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events