You’ve likely found yourself staring at this article because you’ve looked at your development process and had a “There’s got to be a better way!” moment.

Whether you have a form of Continuous Integration or something less involved that doesn’t require automated testing, you’ll eventually have to decide on how your team deploys new code into staging and production.

Lately, at Zion & Zion, we’ve been doing a lot more development with Nuxt. Nuxt is a Javascript framework powered by Node and Vue. You can choose between single page applications (SPAs), universal server-side rendered applications, or even statically generated / pre-rendered applications. Each type has their own set of strengths and weaknesses, but each of them share one common step—the build process. The code samples you’ll find in this article are for deploying a Nuxt 2.x application from a BitBucket repository to Heroku hosting.

When using Nuxt, your project files will have to go through a build or generate process in order to prepare site files for use. During this process, your routes are generated, and all of your client-side and server-side files are compiled and compressed by Webpack into smaller bundles for quicker load times. This adds an additional level of complexity to our deployments as we will be required to run a CLI command immediately after deployment to begin the build process. Luckily, BitBucket and Heroku have us covered and give us the ability to handle this automatically from a properly established pipeline.

BitBucket Pipelines For The Win

There are only a few moving pieces to establishing a working BitBucket pipeline. You have a configuration file that tells the pipeline exactly what you want it to do and a few configuration variables set on your BitBucket repo that tells it where it’s going and how to authenticate with your host. That’s…about it.

Heroku has a pipeline solution as well, which works very similarly. The benefits and setup time are about the same as BitBucket pipeline except for one very big factor. BitBucket pipelines can be set up to clone your repo, download all your dependencies, and make sure your build succeeds without errors before ever attempting to deploy it to your host. We’ve all had something unexpectedly fail to compile for some reason and the worst time for that to happen is while you’re attempting to deploy. This does increase your deployment time because you’re essentially going through the entire process twice but adding a few minutes to a deployment far outweighs the ramifications of having something go awry in production. It is for this reason we also decided not to deploy using Heroku CLI.

Let’s Begin

1. Create Staging And Production Servers On Heroku

You’ll want to start here because we’ll need to use the names of your applications in the following step. Heroku’s admin portal is one of the best in the business so after a few clicks, you should be on your way.

2. Create And Configure Your BitBucket Repo

A.) Enable Pipelines for this repository (BB Repo > Settings > Pipelines > Settings)

B.) Set up Repository Variables (BB Repo > Settings > Pipelines > Repository Variables)

This area allows you to establish values for variables used further in the process. For this example, these variables are necessary in order to establish a connection between BitBucket and Heroku. Take note of the little lock icon next to the Value field. Click that before you create a new variable you consider to be sensitive. You can see from my screenshot below that I did that for the HEROKU_API_KEY.

3. Set Up Your BitBucket Pipeline Config File

At the root of your project you’re going to create a new file named “bitbucket-pipelines.yml”. This file will contain all the instructions BitBucket will use to establish a pipeline.

bitbucket-pipelines.yml

So, let’s break down the configuration here. We tell BitBucket what version of node we want to use, we tell them to do a full clone of the repo because that’s what Heroku requires, and we tell it to do so when there are pushes to branches named “production” and “staging.”

  • “npm install” will install all of the packages listed as dependencies from your package.json file
  • “npm run build” is a Nuxt command also defined in package.json
  • “git push…” if no error occurs we tell BitBucket we’re ready to push to Heroku.
  • A “heroku-postbuild” command defined in our package.json tells Heroku to run the same build CLI command that we just tested

4. Deploy And Watch The Magic

Once everything’s been configured and tested, you can push an update to either the “staging” or “production” branch and watch BitBucket take it from there.

The above screenshot shows all the steps involved in deploying your code to Heroku. Each step can be expanded to reveal all the logs surrounding the actions taken. If an error did occur, you’d be able to see what caused it.

BitBucket also stores the entire deployment history for all tracked branches to give your development team the visibility it needs.

Conclusion

You have a lot of options when it comes to setting up Continuous Deployment. Finding the right solution for your team can take a decent amount of research as well as some trial and error. For us at Zion & Zion, this works pretty darn well. At the cost of about 10-15 minutes of set up, you get an automated process that your entire team can use that is safe, reliable, and worry-free.