Jenkins is an open-source automation server that enables developer/DevOps/Tester to automate various stages of their software development process.
Continuous Integration - Integrate code changes from multiple developers into single repo.
Continuous Deployment - Automate deployment of code changes to production.
Source Code - Java
Port - 8080
- Open port 8080 on EC2
Security group- select wizard - edit inbound rule - add rule 8080
Step 1: Install and Start Jenkins
Create EC2 instance with ubuntu server and connect to local
Ensure Jenkins is installed on your Ubuntu system.
sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version
sudo get -0 /usr/share/keyrings/jenkins-keyring.asc \ https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/us/share/keyrings/jenkins-keyring-asc]" \
https://pkg-jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list › /dev/nullsudo apt-get update
sudo apt-get install jenkins
Access Jenkins at http://<your-server-ip>:8080.
Step 2: Unlock Jenkins
1. Retrieve the initial admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
2. Copy the password, paste it into the Jenkins web interface, and proceed.
Step 3: Install Plugins
1. On the setup screen, choose “Install suggested plugins” to get the default set of plugins.
Step 4: Set Up Jenkins
1. Create an admin user and finish the configuration.
Scenario: You cloned a repository, but the next build fails because the folder already exists.
Solution:
- Delete the folder using
rm -rf shellscripts
and then re-clone it with
git clone https://github.com/vaishalijogdande/shellscripts.git
- You can choose default option - Build environment- Delete workspace before build starts
Choice parameter
The Choice Parameter in Jenkins allows users to select a value from a predefined list of options before triggering a build.
This is useful for providing controlled inputs, ensuring consistency, and reducing errors in build configurations.
Dashboard-job- Configuration - This project is parameterised -Choice parameter
Name - multiple choices - save
In execute shell - command to execute - save
Select choice you want - Build
String parameter
The String Parameter in Jenkins is used to pass user-defined input as a string to a build.
It allows you to customize builds dynamically by providing specific data during the build execution.
Dashboard-job- Configuration - This project is parameterised -String parameter
Discard Old builds
The Discard Old Builds option in Jenkins is used to automatically delete older build data to save disk space and improve performance. It allows you to set limits on how many builds or how long builds should be retained.
Max number of builds to keep: Number of most recent builds to retain -> 5(recent builds)
Max number of days to keep builds: Age limit for builds (in days) -> 10
System message
Steps to Add a System Message
1. Log in to Jenkins as an administrator.
2. Go to Manage Jenkins > Configure System.
3. Look for the System Message section near the top.
4. Enter your message in the text box (e.g., announcements, maintenance schedules, or warnings).
• You can use plain text or basic HTML for formatting.
5. Click Save.
No of executors - 2
2 jobs at one time can run remaining will be in queue
Quiet period
Jenkins will take 5 seconds time to start new job build
Jenkins Weather Icons and Meanings:
1. Sunny (☀): All recent builds are successful and stable.
2. Partly Cloudy (⛅): Some instability, but mostly successful builds.
3. Cloudy (☁): Increased instability; recent builds frequently fail.
4. Rainy (🌧): Many recent builds are failing.
5. Stormy (⛈): Severe instability; builds are consistently failing.
View -
View in Jenkins that differentiate environments like Dev, UAT, and Prod, you can organize your jobs into separate views based on their environment.
Configure maven
apt install maven
mvn —version -> here you will get home path
Test
You will get maven version in Console output
Best way to clone repository on jenkins
Choose specific branch
Home path of Jenkins and related commands
/var/lib/jenkins/
Manage user and permissions
Matrix-based security
in Jenkins allows you to define fine-grained permissions for users and groups at both global and project levels. It provides a tabular format where you assign permissions (like read, build, configure, etc.) to specific users or groups.
Project-Based Authorization Strategy
in Jenkins is used to restrict access to specific projects, enhance security, enable multi-team collaboration, and enforce compliance by allowing fine-grained control over who can access or modify individual jobs.
Role based authentication
To implement role-based authentication in Jenkins, you need to install and configure the Role-Based Authorization Strategy plugin. Below are the detailed steps:
1. Install the Role-Based Authorization Strategy Plugin
2. Go to Manage Jenkins > Manage Plugins.
3. Under the Available tab, search for the Role-Based Authorization Strategy plugin.
4. Select it and click Install without restart.
5. Wait for the installation to complete.
Manage Roles
1. Navigate to Manage Jenkins > Manage and Assign Roles.
2. You will see two options:
• Manage Roles
• Assign Roles
Assign Roles
1. Go to Manage Jenkins > Manage and Assign Roles > Assign Roles.
2. Assign roles to users or groups:
• Global Roles: Assign global roles (e.g., admin, viewer) to specific users or groups.
• Item Roles: Assign roles for specific jobs or folders.
3. Add users or groups in the User/group to add field and assign them roles.
4. Click Save.
Manage Roles
1. Click on Manage Roles.
2. Define global roles (e.g., admin, developer, viewer) and project-specific roles.
• Global Roles: These apply to the entire Jenkins instance. Example permissions:
• Admin: All permissions.
• Viewer: Only Read access.
• Item Roles: These apply to specific projects.
3. Assign permissions to each role by checking the appropriate boxes.
4. Click Save.
Test the Configuration
1. Log in with a user account to ensure the assigned roles and permissions work as expected.
2. Verify that users only have access to the resources and operations allowed by their roles.
Build Triggers
Trigger builds remotely (e.g., from scripts, java code, service now, python script) ->
Monitoring -> CPU -> 70% -> Job triggerBuild after other projects are built
Job-1
Job-2
Build periodically
*****
MINUTE Minutes within the hour (0-59) HOUR
The hour of the day (0-23) DOM
The day of the month (1-31) MONTH
The month (1-12) DOW
The day of the week (0-7) where 0 and 7 are Sunday.
Q. I want to run a job everyday at 7 AM.
0 7 *
Q. I want to run a job everyday at 2.30 PM.
30 14 *
Q. I want to run a job on weekend at 2.30 PM.
30 14 6,7
Q. I want to run a job on Weekdays at 2.30 PM.
30 14 1-5
Q. I want to run a job on Weekdays at 2.30 PM. month Jan and feb
30,40 1,2 * 1-5
Q. 15th of every month at 1PM to 4PM only on saturday Or sunday.
0 13-16 15 * 6,7
Q. Mon to friday 11.30 AM and 10.45 PM not in Dec
30 11 * 1-11 1-5
45 22 * 1-11 1-5
If you have two different time slot make different cron
GitHub Hook trigger for GITscm poll
The “GitHub hook trigger for GITScm polling” is used in Jenkins to automatically trigger builds when changes are pushed to a GitHub repository.
Poll scm
Poll SCM means Jenkins will regularly check your code repository (like GitHub) to see if there are any changes. If Jenkins finds a change, it will automatically start a new build.
It won’t build if repository doesn’t have changes
Maven
It is used in Java.
Source code is not deployable hence we need to convert it into artifact format like jar, war, ear etc
Maven - /Ant/Graddle
Maven uses convention over configuration, which means developers are not required to create build process themselves
Our java project need diff lab and dependencies that need to be downloaded from internet mvn_ all(repo)
Maven will provide you pom. xml
Maven will provide you a project structure
РОМ-
Dependency - Downloading 3rd party jar or lab
Plugin Configuration
GAV - Group ID
Artifact ID
Version
- Example-
‹project>
‹ modelVersion>4.0.0‹/modelVersion>
< groupId>com. mycompany -app</groupId> < artifactId>my-app</artifactId>
<version>1</version>
‹plugins>
‹ plugin›
<groupId>org.sonarsource.scanner.maven</groupId>
< artifactId>sonar-maven-plugin</artifactId>
‹version>3.7.0.1746</version>
</plugin>
‹/plugins>
‹ dependencies>
‹ dependency>
< groupId>junit</groupId>
<artifactId>junit</artifactId>
‹version>4.13.2‹/version>
</dependency>
‹/ dependencies>
</project>
Maven Goal/ Maven lifecycle -
Validate - Validation of project. if everything is correct and all necessary information
available.Compile - Compile the source code of the project.
Test - Test the compiled code using suitable unit testing framework.
NOTE - These test should not requires the code to be packaged.Package - Take the compiled code and package it in its distributable format.
Like JAR, WAR and EARVerify - Run any check on result of integration test to ensure quality criteria.
Install - Install packages into local repo, for use as a dependency in another project
locally.Deploy - Done in the build env, copies the final package to the remote repo for sharing
with another developers and projects.
Master Slave architecture
When you scripts for different OS like ubuntu, amazon linux etc
To manage those scripts you need to use master slave architecture.
If your Jenkins is overloaded, Jenkins distribute all its workloads to slave nodes
Here you need to create one master and other slaves and configure them with credentials
- Steps to achieve Master slave architecture
Create two EC2 instances- jenkins master(ununtu) and node-1(amazon linux)
Connect master with local and install java, Jenkins
Login to jenkins
Create node in Jenkins master and configure with credentials for communication of master slave
Dashboard-Manage jenkins - Nodes - new node - give name and permanent agent - save
No of executors - 2
pwd to get current directory of node 1
Launch method - Launch agent via SSH
Host - DNS of node 1(EC2)
Here we need to create credentials
Open in new tab
Dashboard- Manage Jenkins- Credentials- System- Global credentials
Kind - SSH username with private key
ID , description, username -> ec2-user
Private key - enter directly- paste key you selected while creating EC2 instance
Here credentials are created
Select credentials
Host key verification strategy- Manually trusted key verification strategy
Availability- Keep this agent online as much as possible - save
Launch agent
Install java on node-1 and agent will be online
Jenkins-Pipeline (Pipeline As a Code) -
Jenkins pipeline is a collection of plugin that helps launching build, test and deploy. in pipeline seq order.
In jenkins, a pipeline is a collection of events or jobs which are interlinked with one another in a sequence.
1. Scripted Pipeline-
A Scripted pipeline uses the groovy (JVM-based) lang to create a pipeline as a code
1st version of pipeline as a code.
node {
node-1
stage (Build) {
sh "echo this is build stage"
}
stage (test) {
sh "echo this is Test stage"
}
stage (Deploy) {
sh "echo this is Deploy stage"
sh "Is -1rt"
}
}
2. Declarative Pipeline-
The declarative pipeline follows a relatively new syntax, which narrows the scope of pipeline.
On the technical front, the declarative syntax follows the conditional statements logic that generates a series of stages executing sequential throughout the pipeline lifecycle.
pipeline {
agent any
stages
stage ('Build')
steps {
sh "echo This is build stage"
stage ( 'Test')
steps {
sh "echo This is test stage"
stage ('Deploy')
steps
sh "echo This is deploy stage"
}
}
}
Steps to create declarative pipeline-
Dashboard- All -New item - Pipeline-OK
You can select Sample code as below
You can find builds as below
Create declarative pipeline to install nginx
Dashboard- All -New item - Pipeline-OK
Write nginx code as below - apply and save
Here we get error because Jenkins user is not added on server
To add user on server follow below commands
You need to add below entery in
jenkins ALL=(ALL) NOPASSWD: ALL usermod -a -G root jenkins
Here you can check output as below
You can change default workspace path using declarative pipeline
Nginx Installation
You need to add below entery in /etc/sudoers file
//jenkins ALL=(ALL) NOPASSWD: ALL
//usermod -a -G root jenkins
pipeline {
agent any
stages {
stage ('install-nginx')
{
steps {
sh 'sudo apt update -y'
sh 'sudo apt install nginx -y'
}
}
stage ('deploy-index')
{
steps {
sh 'sudo echo "Welcome to Young Minds. Keep Learning!! Keep Growing!!" > index.html'
sh 'sudo mv index.html /var/www/html/index.html'
}
}
stage ('Nginx-stop')
{
steps {
sh 'sudo systemctl stop nginx'
}
}
stage ('Start-nginx')
{
steps {
sh 'sudo systemctl start nginx'
}
}
}
Parallel declarative pipeline
pipeline {
agent any
stages {
stage ('Parallel Stage'){
parallel {
stage ('Stage-1')
{
steps{
echo "Welcome to Young Minds!"
sleep 5
}
}
stage ('Stage-2')
{
steps{
echo "Keep Learning!!"
sleep 5
}
}
stage ('Stage-3')
{
steps{
echo "Keep Growing!!"
sleep 5
}
}
}
}
}
}
Parallel vs sequential
pipeline {
agent any
stages {
stage ('Parallel'){
parallel {
stage ('Stage-1')
{
steps{
echo "Welcome to Young Minds!"
sleep 5
}
}
stage ('Stage-2')
{
steps{
echo "Keep Learning!!"
sleep 5
}
}
stage ('Stage-3')
{
steps{
echo "Keep Growing!!"
sleep 5
}
}
}
}
stage(Sequential-1)
{
steps {
echo "This is sequential stage 1."
}
}
stage(Sequential-2)
{
steps {
echo "This is sequential stage 2."
}
}
}
}
Trigger
pipeline{
agent any
stages{
stage('Build another job')
{
steps {
build 'job-name'
}
}
}
}
Workspace code
pipeline {
agent{
node {customWorkspace '/data/mypipeline'}
}
stages {
stage (create file){
steps{
sh 'touch NewFile'
}
}
}
}