Continuous Integration (CI):
Continuous Integration (CI) π is a development methodology wherein developers contribute code to a shared GitHub repository π. This repository serves as the primary source from which DevOps Engineers retrieve the code for further processing. The code undergoes a systematic build process using Docker π³, resulting in the creation of a Docker image πΌοΈ.
Once the code is successfully built, the generated Docker image is then pushed to a repository, with Docker Hub being a common choice π . This repository acts as versioned storage for the Docker images, maintaining a history of changes over time β³.
The seamless orchestration of these steps in an ongoing and automated fashion characterizes Continuous Integrationπ. In essence, CI ensures that code integration, Docker image creation, and versioning occur continuously, facilitating a streamlined and efficient development pipeline π.
Continuous Delivery/Deployment (CD)
Upon successfully creating and versioning the Docker image πΌοΈ, the next step involves deploying it to the Cloud βοΈ, with popular platforms such as AWS or Azure being common choices. When this deployment process is carried out manually, it falls under the umbrella of Continuous Delivery π. However, if the entire deployment workflow is automated, it is termed Continuous Deployment π. This distinction highlights the efficiency and agility introduced by automation in the software development and deployment lifecycle.
Pipelines
In Jenkins, a widely-used automation server π, this entire flow can be orchestrated through what is known as pipelines. Jenkins pipelines define the workflow, automating the steps from code integration to deployment βοΈ. These pipelines streamline the process, allowing for seamless execution of tasks such as fetching code from GitHub π, building Docker images πΌοΈ, and deploying to the Cloud βοΈ.
Jenkins pipelines serve as a powerful tool to automate and manage this end-to-end process in a coordinated and efficient manner π€.
Prerequisites β
EC2 instance π₯οΈ
GitHub π
Docker π³
Docker Hub π
Jenkins π
Steps Included:
Installationsπ οΈ
To set up Jenkins on an EC2 instance, follow these steps:
Create Instance π₯οΈ:
- Provision an EC2 instance on AWS with the desired specifications.
Connect with SSH π:
Connect to your EC2 instance using SSH. Use the following command:
ssh -i your-key.pem ec2-user@your-instance-ip
Update System π:
Update the system packages to ensure you have the latest software:
sudo yum update -y
Install Java β:
Install Java on your EC2 instance. Jenkins requires Java to run.
sudo apt install openjdk-11-jre
Install Jenkins π:
Download and install Jenkins on the EC2 instance.
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 sudo apt-get install jenkins
Edit Inbound Rules in Security π‘οΈ:
Open the AWS Management Console, and navigate to the EC2 instance's security group.
Edit inbound rules to allow traffic on port 8080 (Jenkins default port). Add a new rule for port 8080 and save the changes.
Access Jenkins Web Interface π:
Open a web browser and navigate to
http://your-instance-ip:8080
. Jenkins setup wizard will prompt you. Retrieve the initial admin password from the instance:sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password and paste it into the Jenkins setup wizard.
Set up Jenkins π οΈ:
- Follow the instructions in the Jenkins setup wizard to complete the installation. Install suggested plugins and create an admin user.
Access Jenkins Dashboard π₯:
- Once setup is complete, log in to the Jenkins dashboard. You can now start configuring Jenkins jobs and pipelines.
This sequence of steps guides you through the process of setting up an EC2 instance, installing Java and Jenkins, and configuring Jenkins to be accessible on port 8080. This lays the foundation for creating continuous integration and continuous deployment pipelines using Jenkins.
Set up Jenkins job:
To initiate the setup of a new Jenkins job, follow these steps:
Create a new job and choose the Pipeline option.
Select the "GitHub project"π option and input the GitHub repository URL.
Choose the "Pipeline script"π option and draft the pipeline using a Groovy script.
The structure of the Groovy script for a Jenkins pipeline includes the following key elements:
Pipeline: Encompasses the entire script, defining the overall structure.π
Stages: Represent distinct phases in the pipeline, where each stage comprises a set of related tasks.π
Steps: Within each stage, individual steps are defined, specifying the commands or tasks to be executed.π οΈ
Agent: Specifies the agent where the pipeline runs, facilitating the distribution of workload.π€
This skeleton in Groovy syntax provides a framework for organizing Jenkins pipelines, ensuring a clear delineation of stages, steps, and an agent to efficiently handle the workload.π
Example:
pipeline {
agent any
stages{
stage("Code"){
steps{
echo "Code is cloned"
}
}
stage("Build & Test"){
steps{
echo "Docker build completed"
}
}
stage("Push to Repository"){
steps{
echo "Pushed to Dockerhub"
}
}
stage("Deploy"){
steps{
echo "Docker Deployed on AWS EC2"
}
}
}
}
Clone Code from Git:
- Retrieve the source code from the Git repository.π
Install Docker on the Server:
- Set up Docker on the server where the build process will take place.π³
Grant Permissions:
- Provide necessary permissions to the user and Jenkins by adding them to the Docker group.π
Write Build Command with Docker:
- Construct the build command using Docker to create the desired image.π οΈ
Create DockerHub Account:
Establish an account on DockerHub, which serves as the repository for your Docker images.π
Add DockerHub Credentials:
Navigate to "Manage Jenkins" -> "Credentials" -> "System" -> "Global Credentials" -> "Add" and input DockerHub credentials.π
Use "withCredentials" Groovy Syntax:
- Utilize the "withCredentials" Groovy syntax to embed DockerHub login credentials directly into the code.ποΈ
Retag the Old Built Image:
- Apply the "docker tag" command to retag the previously built Docker image.π·οΈ
Login to DockerHub:
- Execute the DockerHub login command within your Jenkins pipeline.π
Push Image to DockerHub:
- Finally, push the tagged image with the "latest" tag to DockerHub.π
These steps establish a comprehensive workflow, from code retrieval to Docker image creation, tagging, and ultimately pushing the image to DockerHub, with Jenkins orchestrating the process seamlessly.
Install Docker Compose on Server:
- Set up Docker Compose on the server to facilitate the deployment of multi-container Docker applications.π³
Edit Inbound Rules in Security:
- Navigate to the security settings and modify the inbound rules to include port 8000, allowing the specified network traffic.ππ§
Deploy Code on AWS:
- Execute the necessary command to deploy the code on the AWS infrastructure. This involves orchestrating the deployment of containers or applications using Docker Compose.βοΈπ
These steps ensure the proper installation of Docker Compose, adjust security settings, and deploy the code on AWS, streamlining the deployment process for your application.
To inspect the Todo app on a browser:
Automate Code Retrieval from Git:
- Simplify code retrieval by incorporating a Jenkinsfile directly into the GitHub repository. Choose the "Pipeline from SCM" option in Jenkins, specify the Git repository URLπ, mention the branch, set the Jenkinsfile pathπ, and save the configuration.
Build with Declarative Checkout SCM:
Upon configuring the pipeline, clicking the build button will trigger the Declarative Checkout SCM processπ, seamlessly fetching the code from the specified Git repository and branch.
To enable automated processes triggered by GitHub events
Navigate to your GitHub repository settings.
Add a webhook for streamlined automation.
In Jenkins, access the 'Configure' section.
Navigate to 'Build Triggers.'
Choose 'GitHub hook trigger for GitSCM polling.'
Save your configuration changes. π οΈ"
By incorporating a Jenkinsfile in your GitHub repository and configuring Jenkins to utilize it, you enhance the automation and efficiency of code retrieval, making the process more streamlined and accessible.π
It enhances efficiency when checking the Todo app on a browser.
"π Appreciate your time spent exploring this blog!π Hope the information was both useful and enlightening.β¨π
π Embrace your learning journey, and feel free to connect if you have any feedback. π€ Wishing you a joyful exploration!π"
Happy Learning:)<3