Jenkins Notes By ShariqSP
What is Jenkins?
Jenkins is an open-source automation server that helps automate the building, testing, and deployment of software projects. It allows developers to integrate changes to the project continuously, making it easier to detect and fix errors early in the development process.
Why Do We Need Jenkins?
Jenkins is essential for modern software development because:
- It automates repetitive tasks, saving time and reducing human error.
- It provides continuous integration and delivery, enabling faster release cycles.
- It supports collaboration among team members by providing a centralized platform for testing and deployment.
- It integrates with a wide range of tools and technologies, making it highly flexible and adaptable to different project requirements.
How Jenkins Solves Real-Time Problems
Jenkins addresses common challenges in software development, including:
- Manual and time-consuming build and deployment processes.
- Inconsistent environments leading to deployment failures.
- Lack of visibility and traceability in the development process.
- Difficulty in detecting and fixing integration issues.
- Delays in delivering features or bug fixes to customers.
Where Can We Use Jenkins?
Jenkins can be used in various scenarios, including:
- Continuous integration and delivery of web applications.
- Automated testing and deployment of mobile apps.
- Building and deploying containerized applications with Docker and Kubernetes.
- Integration with version control systems like Git for automatic build triggering.
- Automating infrastructure provisioning and configuration management.
What is a Pipeline?
In Jenkins, a pipeline is a set of automated steps that define the process for building, testing, and deploying software. It allows developers to define the entire workflow as code, making it easy to version control, share, and reuse.
What is CI/CD Pipeline?
CI/CD (Continuous Integration/Continuous Delivery) pipeline is a practice of automating the software delivery process, from code changes to production deployment. It includes continuous integration, where code changes are automatically built and tested, and continuous delivery, where validated changes are automatically deployed to production or staging environments.
What is Continuous Integration (CI)?
Continuous Integration is a software development practice where developers frequently integrate their code changes into a shared repository. Each integration triggers an automated build and test process, ensuring that the changes are compatible with the existing codebase.
What is Continuous Delivery (CD)?
Continuous Delivery is a software engineering approach where code changes are automatically built, tested, and prepared for release to production environments. It ensures that software can be released reliably and quickly at any time, reducing the time-to-market and increasing customer satisfaction.
What is Continuous Deployment?
Continuous Deployment is the practice of automatically deploying code changes to production environments after passing through automated tests and quality checks. It allows organizations to deliver new features and updates to customers rapidly and frequently, with minimal manual intervention.
Installing and Configuring Jenkins
On Linux Instances:
- Update the package index:
sudo apt update
. - Install Java Development Kit (JDK):
sudo apt install default-jdk
. - Add the Jenkins repository key to your system:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
. - Append the Jenkins repository address to the system sources list:
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
. - Update the package index again:
sudo apt update
. - Install Jenkins:
sudo apt install jenkins
. - Start the Jenkins service:
sudo systemctl start jenkins
. - Enable the Jenkins service to start on boot:
sudo systemctl enable jenkins
. - Access Jenkins in your web browser by navigating to
http://your_server_ip_or_domain:8080
. - Follow the on-screen instructions to complete the setup wizard and configure Jenkins.
On Windows Instances:
- Download the Jenkins installer from the official website: https://www.jenkins.io/download/.
- Run the installer and follow the on-screen instructions to complete the installation.
- Once installed, Jenkins will start automatically. You can access it in your web browser by navigating to
http://localhost:8080
. - During the initial setup, Jenkins will provide a password located in the installation directory.
- Enter the provided password and follow the on-screen instructions to complete the setup wizard and configure Jenkins.
Creating Pipelines in Jenkins
Jenkins provides multiple ways to create pipelines, allowing users to choose the method that best fits their workflow and requirements:
1. Jenkinsfile:
Jenkinsfile is a text file that defines the entire pipeline using Groovy syntax. It allows users to define pipeline stages, steps, and configurations in a single file stored in the source code repository. Jenkins automatically detects and executes the pipeline defined in the Jenkinsfile whenever changes are pushed to the repository.
2. Jenkins Pipeline Script:
In addition to Jenkinsfile, users can create pipelines directly in Jenkins using the pipeline script feature. This approach allows users to define pipeline stages, steps, and configurations directly within the Jenkins user interface using Groovy syntax. Pipeline scripts are typically stored and executed within Jenkins and are suitable for simple or ad-hoc pipelines.
3. Blue Ocean Editor:
Blue Ocean is a Jenkins plugin that provides a visual pipeline editor for creating and managing pipelines. The Blue Ocean editor offers an intuitive interface for designing pipelines, allowing users to drag-and-drop pipeline stages, add steps, and configure pipeline settings without writing code. It simplifies the creation and visualization of pipelines, making it ideal for users who prefer a graphical approach.
4. Pipeline Libraries:
Jenkins supports the use of pipeline libraries to define reusable pipeline components and functions. Users can create custom pipeline libraries containing shared pipeline code, such as common steps, utilities, or custom functions, and then import and use these libraries in their Jenkins pipelines. Pipeline libraries promote code reuse, maintainability, and consistency across pipelines.
5. Integrated Development Environments (IDEs):
Some developers prefer to use integrated development environments (IDEs) such as IntelliJ IDEA or Visual Studio Code to author and manage Jenkins pipelines. These IDEs offer features such as syntax highlighting, code completion, and debugging capabilities, making it easier to write and maintain complex pipeline scripts. Users can install plugins or extensions to integrate Jenkins pipeline development directly into their preferred IDE.
Depending on the complexity of the pipeline, the development workflow, and user preferences, Jenkins offers a range of options for creating pipelines. Users can choose the method that best suits their needs and leverage the flexibility and extensibility of Jenkins to automate their software delivery processes effectively.
Pipeline as Code
Pipeline as Code is an approach to defining and managing Jenkins pipelines using code. It allows developers to describe their entire software delivery process as code, stored alongside their application code in version control repositories. This approach provides several benefits, including versioning, code reuse, and easier collaboration.
Creating a Pipeline with Script
To create a pipeline in Jenkins using script, you can use the Pipeline Script Syntax, which is based on the Groovy programming language. Here's a step-by-step guide on how to create a basic pipeline:
Step 1: Navigate to Jenkins
Access the Jenkins web interface and navigate to the desired job or create a new one.
Step 2: Select Pipeline
Select "Pipeline" as the job type when creating a new job or configuring an existing one.
Step 3: Define Pipeline Script
Enter the pipeline script in the "Pipeline Script" section. The script defines the stages, steps, and configurations of the pipeline.
Step 4: Save and Run
Save your pipeline script and run the job. Jenkins will execute the pipeline according to the defined script.
Pipeline Script Syntax
The Pipeline Script Syntax consists of several components that define the structure and behavior of the pipeline. Here's an overview of the key syntax elements:
node:
The node
step allocates an executor (agent) to execute the pipeline steps. It specifies where the pipeline should be executed, such as a specific Jenkins agent or a label representing a group of agents.
node {
// Pipeline steps go here
}
stage:
The stage
step defines a stage in the pipeline, such as "Build", "Test", or "Deploy". Stages allow you to organize and visualize the pipeline's progress and results.
stage('Build') {
// Build steps go here
}
steps:
The steps
block contains the individual steps to be executed within a stage. Each step represents a task or action to perform, such as compiling code, running tests, or deploying artifacts.
stage('Test') {
steps {
sh 'npm install'
sh 'npm test'
}
}
parallel:
The parallel
step allows you to execute multiple stages concurrently within a single pipeline. It's useful for running tasks in parallel to improve pipeline efficiency.
stage('Parallel Tests') {
parallel {
stage('Test A') {
steps {
// Test A steps go here
}
}
stage('Test B') {
steps {
// Test B steps go here
}
}
}
}
These are just a few examples of the syntax elements used in Jenkins Pipeline Script. You can combine these elements to create complex and flexible pipelines that meet your specific requirements.
Deploying a Static Web Application from GitLab to Apache2 on EC2 Server
Deploying a static web application involves fetching the code from a Git repository, building it, and deploying it to a web server. Below is an example of how you can achieve this using Jenkins scripted pipeline.
Prerequisites:
- An EC2 instance running Apache2 web server.
- Jenkins installed and configured with necessary plugins.
- A GitLab repository containing your static web application code.
Jenkins Scripted Pipeline:
Below is a scripted pipeline example that fetches the code from a GitLab repository, builds the static web application, and deploys it to the Apache2 server running on an EC2 instance:
pipeline{
agent any
stages{
stage("clean")
{
steps{
sh ' rm -rf *'
sh 'rm -rf /var/www/html/*'
}
}
stage("build"){
steps{
sh 'git clone https://gitlab.com/sp.shariq03/wedding.git -b master'
}
}
stage('deploy'){
steps{
sh ' cp -r wedding/* /var/www/html/'
}
}
}
}
By running this Jenkins pipeline, your static web application will be deployed to the Apache2 server on your EC2 instance automatically whenever changes are pushed to the GitLab repository.
Understanding Jenkins Build Triggers
Jenkins provides multiple mechanisms to trigger builds for automation and scheduling. Below is an in-depth explanation of different triggering methods.
Manual Trigger
A manual trigger allows a user to start a build by clicking the Build Now button on the job page in Jenkins. This is useful for ad-hoc testing or debugging. No additional configuration is required for manual triggers.
Poll SCM
Poll SCM triggers a build whenever Jenkins detects changes in the source control system (like Git or SVN). You need to configure a cron-like schedule to specify how often Jenkins should poll the repository.
- Example Cron:
H/5 * * * *
- Explanation:
H/5
: Poll every 5 minutes (the "H" ensures staggered polling to reduce server load).*
: Poll every hour, every day, every month, and every day of the week.
- Trigger on Changes: The build will only occur if changes are detected in the repository.
Build Periodically
This option allows you to schedule builds at specific times, regardless of changes in the source control system. You define a cron schedule for this as well.
- Example Cron:
H 12 * * *
- Explanation:
H
: Ensures staggered builds to avoid overloading the server.12
: Triggers the build at noon.* * *
: Triggers every day, every month, and every day of the week.
- Trigger on a Specific Date:
For triggering on a specific date, you can modify the cron to include the day and month:
H 12 25 12 *
(Triggers at noon on December 25th).
Triggering on GitHub Changes
To trigger a build when changes are pushed to a GitHub repository, use the **GitHub Hook Trigger for GITScm polling** option.
- Enable GitHub hook trigger for GITScm polling in your job configuration.
- Set up a webhook in your GitHub repository:
- Go to Settings → Webhooks.
- Set the webhook URL to
http://
./github-webhook/ - Choose application/json as the content type.
Triggering Every 5 Minutes
To trigger a build every 5 minutes, use the cron schedule:
H/5 * * * *
.
- This schedule means Jenkins will run the job every 5 minutes.
- The "H" ensures staggered execution across jobs.
Triggering Using Webhooks
Webhooks allow external systems to notify Jenkins to trigger a build. For instance, you can integrate with Git, GitHub, or any system capable of sending HTTP POST requests.
- Install the Generic Webhook Trigger Plugin in Jenkins.
- Enable Generic Webhook Trigger in the job configuration.
- Configure the webhook URL in the external system:
http://
./generic-webhook-trigger/invoke
By using these triggering mechanisms, you can fully automate your CI/CD pipeline and integrate seamlessly with your development workflow.