Introduction to Jenkins and Build Automation
Before diving into Jenkins, let's understand its purpose: Build Automation. In simpler terms, build automation is the process of automating:
Retrieval of source code
Executing automated tests
Compiling source code into binary code or building Docker images
Pushing artifacts to a repository
Deploying artifacts
End Goal
Create a Jenkins Pipeline to:
Increment the version
Run tests
Build a Docker image
Push the image to Docker Hub
Setup
Create a Droplet on DigitalOcean
Choose a configuration compatible with your needs (e.g., 4 GB RAM, 2 CPUs, 80 GB SSD Disk, 4 TB Transfer).
SSH into the server.
Install Docker on the Server and Start Jenkins
apt install docker.io
docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:lts
docker ps
docker exec -u 0 ${containerId} bash
curl https://get.docker.com/ > dockerinstall && chmod 777 dockerinstall && ./dockerinstall
Now we have configured everything install jenkins on server and then onto container also we have installed docker so that we can use docker commands inside container and use in pipeline as well. You can access jenkins on : http://${serverip}:8080
Types of Jenkins Jobs
Freestyle Project
A simple, general-purpose job type that allows you to build and run virtually any task.
Configuration is UI-based; it cannot be configured through the CLI.
Pipeline Project
Allows you to define a series of stages and steps using a Domain Specific Language (DSL) based on Groovy.
Provides greater flexibility and control over the build process, suitable for complex workflows.
Multibranch Pipeline Project
Automatically creates and manages a pipeline for each branch in your source control repository.
Discovers branches and executes their respective Jenkinsfiles, enabling automated CI/CD for each branch independently.
Hands-On: Creating a Jenkins Pipeline for a Node.js Project
Step 1: Dockerize the Project
Make sure your project has a Dockerfile.
Step 2: Create a Pipeline Job in Jenkins
Create a New Item
Click on "New Item" from the sidebar.
Enter a name for your job (e.g., "MyNodeAppPipeline").
Choose "Pipeline" as the project type and click "OK".
Configure the Pipeline
In the pipeline section, under the definition, choose "Pipeline script from SCM".
In SCM, choose "Git" and add your Git repository URL, credentials, and the branch you want to build the pipeline for.
Click "Save".
Step 3: Write the Jenkinsfile
Credentials Setup:
Go to "Manage Jenkins", then "Credentials".
Click on "System", then "Global credentials".
Add credentials and give them the ID "docker-hub-repo". If you name it differently, update the Jenkinsfile accordingly.
Jenkinsfile:
pipeline {
agent any
stages {
stage("increment version") {
steps {
script {
echo "incrementing version"
dir("app") {
sh "npm version minor"
def packageJSON = readJSON file: "package.json"
def version = packageJSON.version
env.IMAGE_NAME = "$version-$BUILD_NUMBER"
}
}
}
}
stage('Test') {
steps {
sh "npm install"
sh "npm test server.test.js"
}
}
stage('Build') {
steps {
// build docker image and push to dockerhub
// CREATE A PRIVATE REPO ON DOCKERHUB my-app
withCredentials([usernamePassword(credentialsId:'docker-hub-repo',usernameVariable:'USER',passwordVariable:'PASS')]) {
// add docker username in place of user and give custom image name
sh "docker build -t ${USER}/my-app:${IMAGE_NAME} ."
sh "echo $PASS | docker login -u $USER --password-stdin"
// add docker username in place of user and give custom image name
sh "docker push ${USER}/my-app:${IMAGE_NAME}"
}
}
}
stage('Deploy') {
steps {
sh 'make deploy'
// may be deploy to aws if u want
}
}
}
}
Step 4: Automate GitLab Integration
GitLab Setup:
Generate API Token:
Go to GitLab, click on your profile, choose "Preferences", then "Access Tokens".
Create a new token with the "api" scope and copy it.
Jenkins Setup:
Install GitLab Plugin:
- Go to "Manage Jenkins", then "Manage Plugins", and install the GitLab plugin.
Configure GitLab Connection:
Go to "Manage Jenkins", then "Configure System".
Scroll down to the GitLab section, add a connection name, host URL (
https://gitlab.com/
), and use the API token for credentials.
Integrate GitLab:
In your GitLab project, go to "Settings" -> "Integrations".
Add a Jenkins integration with your server URL and credentials.
Test the integration to ensure it works.
Conclusion
You've now set up Jenkins to automate your Node.js project's build and deployment process using a Pipeline. Whenever a change is made, Jenkins will:
Increment the version
Run tests
Build a Docker image
Push the image to Docker Hub