Setting up CI/CD deployment for a Node.js application using Jenkins and Terraform

Setting up CI/CD deployment for a Node.js application using Jenkins and Terraform


Prerequisites:

  1. A Node.js application with a Git repository hosted on GitHub

  2. A Jenkins server

  3. Terraform installed on your local machine

Step 1: Setting up the Jenkins Server

  1. Install Jenkins on your server following the official installation guide

  2. Install the following plugins in Jenkins:

  • GitHub plugin

  • NodeJS plugin

  • Pipeline plugin

3. Set up a Jenkins user and generate an API token for the user

4. Add the Jenkins user’s API token to the GitHub repository as a secret

Step 2: Setting up the Terraform Project

  1. Create a new directory for your Terraform project

  2. Create a file named “main.tf” and add the following code:

provider "aws" {
  region = "<your_aws_region>"
}

resource "aws_instance" "web_server" {
  ami           = "<your_ami_id>"
  instance_type = "<your_instance_type>"
  key_name      = "<your_key_pair_name>"

  tags = {
    Name = "web_server"
  }
}

resource "aws_security_group" "web_server_sg" {
  name_prefix = "web_server_sg"

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "web_server_sg"
  }
}

This code will create an AWS EC2 instance and a security group with a rule to allow incoming traffic on port 80.

3. Create a file named “variables.tf” and add the following code:

variable "access_key" {}
variable "secret_key" {}
variable "region" {
  default = "<your_aws_region>"
}

This file defines the variables we will use to configure the AWS provider in our Terraform configuration.

Step 3: Setting up the Jenkins Pipeline

  1. In Jenkins, create a new pipeline job and configure it to use the GitHub repository where your Node.js application is hosted

  2. In the pipeline job configuration, add the following pipeline script:

pipeline {
  agent {
    label "master"
  }
  environment {
    AWS_ACCESS_KEY_ID     = credentials('aws-access-key-id')
    AWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key')
  }
  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }
    stage('Build') {
      steps {
        nodejs('nodejs') {
          sh 'npm install'
        }
      }
    }
    stage('Test') {
      steps {
        nodejs('nodejs') {
          sh 'npm test'
        }
      }
    }
    stage('Deploy') {
      steps {
        withCredentials([[
          credentialsId: 'terraform-creds',
          accessKeyVariable: 'AWS_ACCESS_KEY_ID',
          secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
        ]]) {
          sh '''
            cd terraform
            terraform init
            terraform apply -auto-approve \
              -var access_key=$AWS_ACCESS_KEY_ID \
              -var secret_key=$AWS_SECRET_ACCESS_KEY
          '''
        }
      }
    }
  }
}

This pipeline script defines four stages: checkout, build, test, and deploy. The checkout stage checks out the source code from the GitHub repository, the build stage installs the application dependencies, the test stage runs the application tests, and the deploy stage deploys the application using Terraform.

Note that the pipeline script uses the “nodejs” plugin to set up a Node.js environment for building and testing the application.

Step 4: Setting up the Terraform Credentials in Jenkins

  1. In Jenkins, navigate to “Credentials” and add a new “Secret Text” credential with the name “aws-access-key-id” and the value of your AWS access key ID

  2. Add another “Secret Text” credential with the name “aws-secret-access-key” and the value of your AWS secret access key

  3. Add a third “Secret Text” credential with the name “terraform-creds” and the value of your AWS access key ID and secret access key in the following format: “access_key_id=YOUR_ACCESS_KEY_ID\nsecret_access_key=YOUR_SECRET_ACCESS_KEY”

Step 5: Running the Pipeline

  1. Save the pipeline configuration in Jenkins and run the pipeline job

  2. Jenkins will automatically trigger the pipeline whenever changes are pushed to the GitHub repository

  3. The pipeline will run the build and test stages and then deploy the application using Terraform


Congratulations! You have successfully set up a CI/CD deployment pipeline for your Node.js application using Jenkins and Terraform. This pipeline will automatically build, test, and deploy your application whenever changes are pushed to your GitHub repository.

Did you find this article valuable?

Support VIVEK RAJYAGURU by becoming a sponsor. Any amount is appreciated!