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

How to deploy to remote server after building assets in pipeline?

Matthew Hailwood November 11, 2018

Right now our deployments are done manually, we'd love to move these into bitbucket pipelines.

Here's what I need to do for our deployments:

  1. npm install
  2. npm run prod
  3. git pull on remote server
  4. run another bash command on the remote server
  5. scp specific files (built assets via the npm run prod) to remote server

 

Other potential important information:

We use different branches for staging/development, so would like to trigger the deploy to the staging server on push to the "staging" branch, and the deploy to the production server on push to the "master" branch.

/cc @Linette

1 answer

1 accepted

2 votes
Answer accepted
StannousBaratheon
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 14, 2018

Hi @hailwoodnz

Bitbucket pipelines supports branch deployment workflows natively so this is fairly straight forward. You want to define a separate pipeline in your bitbucket-pipelines.yml file for each of your deployment branches, for example:

pipelines:
default: # the default pipeline will just run your build and test commands
- step:
name: Build and test
script:
- npm install
- npm test
branches:
staging: # the staging branch pipeline will deploy to the staging environment
- step:
name: Build and test
script:
- npm install
- npm test
- step:
name: Deploy to staging
deployment: staging
script:
- npm run staging
- git pull ...
master: # the master branch pipeline will deploy to the production environment
- step:
name: Build and test
script:
- npm install
- npm test
- step:
name: Deploy to production
deployment: production
script:
- npm run prod
- git pull ...

With this configuration:

  • pushes to master will run the master branch pipeline and deploy to production
  • pushes to staging will run the staging branch pipeline and deploy to staging
  • pushes to any other branch will run the default pipeline which only runs the build and test step

Because the deployment steps have been marked with their respective environments you'll also be able to track the status of those environments on the deployments dashboard in Bitbucket.

The yml file is read from the branch on which the push occurs so be sure to merge this configuration to both staging and master branches.

There's also some duplicate step definitions in there that can be cleaned up using shared scripts or yml anchors.

I hope this helps!

Sam

Matthew Hailwood November 14, 2018

Thanks for the detailed answer @StannousBaratheon,

This looks like it should cover everything nicely,

I was just reading through about the deployments and note it says

Currently Bitbucket Deployments supports deploying to teststaging,and production environments and they must be listed in this order in the yml file.

Given that we have only staging and production, and given your example above I assume that if we don't have a given environment it doesn't need to be listed?

If this is the case perhaps the note could be updated to

Currently Bitbucket Deployments supports deploying to teststaging,and production environments and if deploying to these environments they must be listed in this order in the yml file.

Like Surya Van Lierde likes this
StannousBaratheon
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 14, 2018

That's correct @hailwoodnz, the environments must be listed in that order if they're being used in the same pipeline but it's not required that you use all 3 environments.

In this example you actually have environments defined in different pipelines because you have a branch deployment workflow so the order requirement is also irrelevant.

I'll pass on your suggestion to our documentation team. Thanks for the feedback :)

Like Surya Van Lierde likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events