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:
- 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
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 commands finished running, your app will be running.