Real-World Example: CI/CD Automation for Node.js Microservices with Bitbucket Pipelines

Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development. They enable teams to deliver features quickly and reliably. In this article, we explore a real-world example of automating CI/CD for Node.js microservices using Bitbucket Pipelines.

Overview of the Setup

The goal is to automate the testing, building, and deployment of multiple Node.js microservices. Bitbucket Pipelines provides a seamless way to define these workflows directly within the repository. This setup ensures that every code change is validated and deployed automatically.

Prerequisites

  • Bitbucket repository with Node.js microservices
  • Docker installed locally for testing
  • Access to a cloud hosting environment (e.g., AWS, Azure, or Heroku)
  • Basic knowledge of Docker and YAML

Configuring Bitbucket Pipelines

First, create a bitbucket-pipelines.yml file in the root of your repository. This file defines the pipeline stages and steps for your microservices.

Sample Pipeline Configuration

Below is a simplified example of a pipeline that tests, builds, and deploys a Node.js microservice whenever code is pushed to the main branch.

pipelines:
  branches:
    main:
      - step:
          name: Test
          image: node:14
          script:
            - npm install
            - npm test
      - step:
          name: Build
          image: node:14
          script:
            - npm run build
            - docker build -t my-microservice:latest .
      - step:
          name: Deploy
          image: atlassian/default-image:2
          deployment: production
          script:
            - pipe: atlassian/docker-push:1.2.0
              variables:
                IMAGE: 'my-dockerhub-user/my-microservice:latest'
                DOCKER_PASSWORD: $DOCKER_PASSWORD
                DOCKER_USERNAME: $DOCKER_USERNAME
            - ./deploy.sh

Implementing Deployment Scripts

The deploy.sh script automates the deployment process. It can include commands to push Docker images to a registry and update the hosting environment.

Example deploy.sh:

#!/bin/bash
# Authenticate with cloud provider
# Update deployment with new Docker image
echo "Deploying to production environment..."
# Add your deployment commands here

Benefits of This Approach

  • Automated testing reduces bugs in production
  • Faster release cycles with continuous deployment
  • Consistent environments using Docker containers
  • Easy rollback by deploying previous images

Conclusion

Implementing CI/CD pipelines with Bitbucket Pipelines for Node.js microservices streamlines the development process. Automating testing, building, and deployment ensures reliable releases and faster delivery to users. This setup can be customized further to fit complex workflows and multiple environments.