How to Deploy Node.js Web App to Heroku from Bitbucket

Heroku is a cloud provider that makes it easy to host web applications for a good price. It supports deployment by pushing to Git. It also supports deploying from GitHub repos directly. However, there is no support for deploying directly from Bitbucket repos.

To do this, we have to use Bitbucket’s build pipeline feature. Given that we have a Node.js web app using the Express framework, we can make a YAML file to specify how to push our code to our Heroku repo, which will then trigger Heroku’s automatic build and deployment steps.

In our repo, we add a new file called bitbuket-pipelines.yml in the root directly of our repo. Add the following in the file:

image: node:10.16
clone:
depth: full
pipelines:
branches:
master:
- step:
script:
- git remote set-url origin https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git
- git push origin `git subtree split --prefix backend master`:master --force

where $HEROKU_API_KEY and $HEROKU_APP_NAME are specified in the ‘Repository variables’ in the Settings section of your repo:

Image for post
Image for post

Note that the Git command is pushing a subfolder to Heroku, but you can push the root folder as well.

Then we to make a Procfile in the root folder of the back end app.

release: npx sequelize db:migrate
release: npm install -g sequelize-cli forever
release: npm i
web: node app.js

In this example, migrations are run, then packages are installed and the app is restarted.

Once you push your code in the branch you specified in the BITBUCKET_BRANCH variable, then all the commands you specified in the Procfile will run. The build steps are specified in the release: lines and the line running the web app is specified in the web: line of the Procfile .

Once the Procfile commands finished running, your app will be running.

Image for post
Image for post

Written by

Web developer. Subscribe to my email list now at http://jauyeung.net/subscribe/. Email me at hohanga@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store