How to automate tests and deployments of Node.js apps with Buddy

How to automate tests and deployments of Node.js apps with Buddy

We designed Buddy to be a powerful tool that helps developers gain the upper hand within the ever-changing web environment by automating and streamlining development process of their web apps to the maximum. This guide will tell how to achieve that by introducing the continuous approach to your workflow, so that you'll be able to deliver better Node.js apps more often with as little hassle as possible.

Objectives of this guide

Upon completing this guide you will be able to:

  • Understand the idea of Continuous Integration and Delivery
  • Configure tests for your Node.js app
  • Automate package deliveries upon every change to code
Hint

Actions used in this guide:

How to automate releases with CI/CD?

The concept of Continuous Integration and Delivery assumes that the source code of your web application is always tested and ready to deploy with every release. Buddy supports a wide variety of tools and frameworks that let you achieve that by constructing your own delivery workflow. So here's how you can test out Buddy and Node Js in action:

Employing Continuous Integration will allow you to:

  • produce reliable web apps in short iterations
  • receive feedback faster any time a change is made to the system
  • ensure all tests are performed in one consistent environment across all developers
  • facilitate manual QA processes by applying automatic continuous testing on the build server
  • avoid last-minute panic in the office before deployments to production
  • automate repeatable activities and focus on actual coding
  • easily deploy across different environments
  • effortlessly send your code to various cloud hosting services or your own servers

Ready to begin? Go!

Prepare the Git repository

It's not possible to apply Continuous Integration without first putting your files safe and sound under version control.

Begin with forking our demo project from GitHub: github.com/buddy-works/node-demo

Image loading...GitHub panel

The project is Node-based, and feel free to fork it from our public git repo, or just use any JavaScript project instead if you like.

Synchronize the Git project

Fire up Buddy and create a fresh project. Select GitHub as your provider and repository with our project forked and click the Sync button.

Image loading...Creating a new project

Hint
If you're not familiar with Git, this beginner's guide to Git will get you started.

Of course, Buddy supports other git platforms, but for purposes of this tutorial we will stick to GitHub. If you are not sure how to add the integration, check out our documentation on the topic.

How to create a delivery pipeline

Pipelines let you plan and execute actions that will build, run automated tests of your app and deploy it to the server. This way you're able to release stable software more frequently which is one of the main goals of Continuous Integration. Let's create a pipeline that will test and deliver your node application on every push to branch in the repository:

  • Add a new pipeline, set the trigger mode to On events and choose "Git push" event to a branch you would like to run the tests on. For now we will do this on the master branch. Depending on your needs, you can choose smaller resources to limit the costs of every run.

Image loading...Adding a new pipeline

Hint
You can also set the trigger mode to manual or recurrent, which is useful for stage and production servers, or set a wildcard if you like to trigger the release from more than one branch. You can read more about pipelines and their settings here.

How to configure automated testing for your Node.js app

Now we need to annd configure an action in our pipeline, that will run the tests. Buddy utilizes Docker containers with pre-installed tools (apps, frameworks, etc.) that you can modify to best fit your testing process.

Success
You can different types of test tools in the "Test" section of the action selection
  1. Select Node from the action list:

    Image loading...Actions List

  2. First tab that you will see is the Run tab. It allows you to modify the command line of the action. By default, Node action comes with two commands:

    • npm install for the installation of dependencies
    • npm test for running tests
  3. For the purposes of this guide, the default run commands are all we need. Click Add action when you're ready to save all changes.

Success

NOTES

  • You can change the version of Node and install missing packages in the Environment tab
  • If you want to test your app in different instances, just add more actions with different Node versions
  • If your tests, like integration tests, require a database to run (eg. MongoDB), you can set it up in the Services tab

Automate deployment to the server

Once your tests have passed, it is time to deploy the app to the server. There you can, eg. run integration tests or preview it to your client. Automating these activities is the core of the Continuous Delivery approach.

  1. Add another action and select the transfer method for your server type. In this example we'll use SFTP:

    Image loading...SFTP action

  2. In the Transfer tab, select the Repository as the source, that way Buddy will only deploy the tested app without any packages generated by the NPM. In this example, we don't want to deploy any packages because their shape depends on the system where the npm was executed. That's we'll first upload the files and run npm install on the server afterward.

  3. Select the authentication mode and provide login details to your server. Clik Test to check if everything's correct, and add the action to the pipeline:

    Image loading...Overview details

Success
To enhance deploy times, it is a good practice to add /node_modules/ to ignore paths

Run npm install on the server

To ensure everything will work as it should, we now have to run npm install on your server. The action can be automated with an SSH script:

  1. Add another action and select the SSH action:

    Image loading...SSH action

  2. Provide the authorization details to your server s you did with the SFTP action and enter npm install in the command field:

    Image loading...Action details

NOTES:

You can use environment variables to store sensitive data to your servers.

Run the pipeline

Congratulations! Your pipeline is configured and waiting for the execution. Make a push to the selected branch and watch Buddy automatically test and deliver your app:

Summary

The pipeline described represents a small portion of Buddy's capabilities. You can also add conditional notifications to keep you aware when a test fails, monitoring actions that check if your website is up and running, more deployment methods to update assets on IaaS platforms like AWS S3, or restart your server with an SSH script once the deployment is over.

Hint
If you need any help with setting up your workflow, drop a line to support@buddy.works with a short description of your development process and we'll tell you how to make it better with Buddy.
Jarek Dylewski

Jarek Dylewski

Customer Support

A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.