Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to specify a common behavior for multiple branches and after that run branch specific behavior

Lucas Ventura
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!
January 29, 2018

Hi, I have a pipeline that some stages are the same for multiple branches, like compile and tests, but when I need to deploy using the pipeline I want to specify what environment will receive the deploy, now Im doing conditions inside the script like: When branch is production, deploy to production, when branch is staging, deploy to staging.


I want to do something like this, is it possible?

pipelines:
branches:
'{staging, production}':
- step:
name: Build, Tests
caches:
- pip
script:
- build something
- test something
staging:
- step:
name: Deploy
script:
- Deploy to staging

production:
- step:
name: Deploy
script:
- Deploy to production

When I run the pipeline like this just scripts for "{staging and production}" runs.

Thanks for help.

 

1 answer

0 votes
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 27, 2018

Hello Lucas,

Currently there's no way to share steps between pipelines, so you'll need to define duplicate behaviour in each pipeline.

For example:

pipelines:
branches:
production:
- step:
name: build & test
script:
- ./build-app
- ./run-tests
- step:
name: Deploy to production
script:
- ./deploy-to-production
staging:
- step:
name: build & test
script:
- ./build-app
- ./run-tests
- step:
name: Deploy to staging
script:
- ./deploy-to-staging

To remove command duplication, you can store the commands you need to run inside of a script file and reference that in your bitbucket-pipelines.yml instead. Like so:

You can also use default to define behaviour that will run on all branches that don't match any other rules.

So overall, you could end up with a configuration like:

pipelines:
default: # runs on all branches except production or staging.
- step:
name: build & test
script:
- ./build-app
- ./run-tests
branches:
production:
- step:
name: build & test
script:
- ./build-app
- ./run-tests
- step:
name: Deploy to production
script:
- ./deploy-to-production
staging:
- step:
name: build & test
script:
- ./build-app
- ./run-tests
- step:
name: Deploy to staging
script:
- ./deploy-to-staging

Thanks,

Phil

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events