Page Updated on Oct 12, 2022. 1st Published on Oct 12, 2022
Before we deve deep into the project, lets define some important terms
1.Jenkins
Jenkins is an open-source automation server
used to automate tasks associated with building, testing, and
delivering/deploying software.
3.Terraform
Terraform is a tool for building, changing, and
versioning infrastructure that has an open-source and enterprise
version.
This tutorial we will walks you through the process of deploying a VPC and it associate subnet in an aws environment. You will launch an EC2 instance, install Jenkins on that instance, and configure Jenkins to automatically spin up Jenkins agents if build abilities need
Architect Jenkins Pipeline
Jenkins Pipeline:- supports Continuous Integration and Continuous Deployment by automating all the steps like build, testing, and deploying. This can be achieved with a Jenkins file which will be committed to GitHub or any project’s code repository.
Jenkins file:-The Jenkins file contains the definition of the pipeline with multiple stages where each stage represents a task or set of tasks to be performed. I will explain each stage individually later in this article.
GitHub:-We used GitHub as our code repository for this project. we committed all our code to the main branch.
Terraform:-Terraform is a free open-source infrastructure as a code tool that can create, change and improve the infrastructure. we used it to automate the process of creating a vpc and it associate subnets in our aws cloud environment.
Before we move on to lunch an Ec2 instance, install and configure jenkins and its
plugin ,
we need to make sure that our terraform code function perfectly.
If you are new
to terraform, please follow up with
this tutorial to set
up, write and Deploy
terraform code in your cloud environment manually. Your can
as well fork the
repository.
from our github account . If that is what you intend to do , Please make sure you
create and match your s3
bucket name with that in the terraform code.
Once that is done, your are ready
to procced with the automation process.
With above the Prerequisites in place, we will move straight to install and
configure
Jenkins on the ec2 instance running
in our cloud environment. if you need help lunching an ec2 instance, check out this
tutorial.
Please, make sur that port 22 and 8080 are open and that you can ssh into your ec2
instance.
To facilitate things, you can insert Jenkins installation script as user data (User data script is the easiest and most popular way to send instructions to an instance at launch.) while lunching an aws EC2 Instance. if you choose to do so, here is the installation script.
#!/bin/bash
sudo yum update –y
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum upgrade
sudo dnf install java-17-amazon-corretto -y
sudo yum install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
You can check the status of the Jenkins service using the command:
sudo systemctl status jenkins
sudo yum update –y
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum upgrade
sudo dnf install java-17-amazon-corretto -y
sudo yum install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
You can check the status of the Jenkins service using the command:
sudo systemctl status jenkins
Jenkins is now installed and running on your EC2 instance. To configure Jenkins:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Using the link you save above, sign into Jenkins and follow the various stept to configure terraform on Jenkins
wget (copied url)
ls
you should see a Terraform zip file. Run
unzip {terraform zip file name}.
sudo mv terraform /usr/bin
which terraform
Before we configure Jenkins Pipeline, we need to have a Jenkinsfile in the root directory of our project residing in github. let us create one now
A Jenkinsfile is a text file that defines the entire build process for a Jenkins pipeline. It uses a domain-specific language (DSL) that is based on Groovy syntax. Writing a Jenkinsfile involves defining stages, steps, and other configuration elements to automate the build, test, and deployment processes.
Pipeline supports two syntaxes, Declarative (introduced in Pipeline 2.5) and Scripted
Declarative Syntax: This is a more structured and
human-readable approach and usually structured as seen below
The agent instructs Jenkins to allocate an executor and workspace for the Pipeline.
Without an agent directive, not only is the Declarative Pipeline not valid,
it would not be capable of doing any work! By default the agent directive ensures
that the source repository is checked
out and made available for steps in the subsequent stages.
The stages directive, and steps directives are also required for a valid Declarative
Pipeline as they instruct Jenkins
what to execute and in which stage it should be executed.
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
//Specify the agent (where the build run)
//e.g, 'docker', 'node', 'any', etc
stages {
stage('Build') {
steps {
//Define steps for the 'Build stage'
//source code is assembled, compiled, or packaged
}
}
stage('Test') {
steps {
//Define steps for the 'Test stage'
}
}
//Add more stages as needed
stage('Deploy') {
steps {
//Define the deployment step
}
}
Post {
//Define post-biuld action, such as notification oe clean
}
}
}
Scripted Syntax: With the Scripted Pipeline,node is a crucial first step as it allocates an executor and workspace for the Pipeline. In essence, without node, a Pipeline cannot do any work! From within node, the first order of business will be to checkout the source code for this project. sample structured seen below
node {
checkout scm
//The checkout step will checkout code from source control
//scm is a special variable which instructs the checkout step to clone the specific revision which triggered this Pipeline
run.
/* .. snip .. */
}
you can check out how to set environment variable and Handle credentials within Jenkins Here
Check out the jenkinsfile use for this project in the our GitHub Repo