“Pipeline DevOps with Jenkins, Terraform, and Docker: A Step-by-Step Guide”

Nivas DevSecOps
4 min readOct 31, 2024

--

Introduction:
Welcome to our comprehensive guide on streamlining your DevOps workflow with Jenkins, Terraform, and Docker. In this blog, we’ll take you through the entire process of setting up a robust DevOps pipeline using these powerful tools. Whether you’re a seasoned DevOps engineer or just getting started, this guide will provide you with the knowledge and skills needed to automate your software development and deployment processes effectively.

Luanch Instance

Setting Up Jenkins:
Jenkins is a popular automation server that helps automate the build, test, and deployment processes. In this section, we’ll walk you through the steps to install Jenkins on your system, configure it for pipeline automation, and create a new Jenkins pipeline project.

#! /bin/bash
# For Ubuntu 22.04

# Intsalling Java
sudo apt update -y
sudo apt install openjdk-11-jre -y
java --version

# Installing Jenkins
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y

Integrating Terraform:
Terraform is an infrastructure as code tool that allows you to define and provision infrastructure using simple configuration files. We’ll show you how to install Terraform, write Terraform configuration files for infrastructure provisioning, and integrate Terraform with Jenkins pipeline using a Jenkinsfile.

#! /bin/bash
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update
sudo apt install terraform -y

Implementing Docker:
Docker is a containerization platform that simplifies the process of packaging and deploying applications. In this section, we’ll guide you through the process of installing Docker, writing Dockerfiles for building container images, and creating Docker Compose files for defining multi-container applications.

#! /bin/bash

sudo apt update
sudo apt install docker.io -y
sudo usermod -aG docker jenkins
sudo usermod -aG docker ubuntu
sudo systemctl restart docker
sudo chmod 777 /var/run/docker.sock

Github Repo

Building the Pipeline:
Now that we have our tools set up, it’s time to build our DevOps pipeline. We’ll define stages in the Jenkins pipeline for build, test, and deployment, trigger Terraform commands for infrastructure setup, and build Docker images for deploying containers.

Create pipeline

Set parameterized

Create Pipeline script

pipeline {
agent any
stages {
stage('Pull') {
steps {
git 'https://github.com/nivas-22/Jenkins-Terra.git'
}
}
stage('terraform init') {
steps {
sh 'terraform init'
}
}
stage('terraform plan') {
steps {
sh 'terraform plan'
}
}
stage('terraform validate') {
steps {
sh 'terraform validate'
}
}
stage('terraform apply') {
steps {
sh 'terraform ${Action} --auto-approve'
}
}
}
}

Build a apply action

Pipeline successfully build

Testing and Validation:
Testing and validation are crucial steps in the DevOps pipeline. We’ll test our pipeline with sample applications, validate the infrastructure and container deployment, and troubleshoot common issues and debug the pipeline.

Created Docker Image

Hit Public IP

Output

Build other action destroy

Conclusion:

In conclusion, streamlining your DevOps workflow with Jenkins, Terraform, and Docker can greatly improve the efficiency and reliability of your software development and deployment processes. By following the steps outlined in this guide, you’ll be well-equipped to automate your DevOps pipeline and deliver high-quality software faster than ever before. So, what are you waiting for? Let’s get started on your DevOps journey today!

Hope it helps..!

Thank you for reading so far! Before you go:

--

--

Nivas DevSecOps
Nivas DevSecOps

Written by Nivas DevSecOps

Cloud | DevSecOps| AWS ⭐Passionate Cloud and DevOps . 🎯 Like to stay up-to-date with the latest trends and insights.

No responses yet