Jenkins Start To End
Jenkins Start To End
1
Why Jenkins
Jenkin is a tool that is used for automation. It is mainly
an open-source service that allows all the developer to
build, test and deploy software.
By using Jenkin, we can make continuous integration of
project(jobs) or end-to-endpoint automation.
Main aim is Auto-update.
• Free and open source: Jenkins is free to use and can be
customized without licensing fees.
2
Jenkin components
• Jobs
• Triggers
• Plugins
• Configuration
• Security settings
Jobs/Projects
• Role: Defines the work Jenkins should perform.
• Types:
o Freestyle Jobs: Basic jobs with configurable build steps.
o Pipeline Jobs: Defined as code (Jenkins file) using Groovy.
o Multi-branch Pipelines: Automatically builds branches in
version control systems.
Triggers
Triggers automate the execution of jobs based on specific events or
schedules:
• Poll SCM: Periodically checks the repository for changes.
• Webhook Triggers: Triggers builds on push or pull request
events from GitHub/GitLab.
• Scheduled Trigger: Uses cron-like syntax for periodic builds.
• Build After Other Projects: Chains jobs together.
• Remote Trigger: Starts builds via API calls or scripts.
3
• Pipeline Triggers: Defined in Jenkinsfile using pollSCM or cron.
Plugins
• Role: Extend Jenkins' core functionality.
• Examples:
o Source Code Management (SCM): Git, SVN.
o Build Tools: Maven, Gradle.
o Deployment: Ansible, Kubernetes.
o Notifications: Slack, Email.
• Key Benefit: Modularity and flexibility in adapting to diverse
CI/CD needs.
Configuration
Proper configuration ensures Jenkins operates efficiently:
Website or webserver settings.
Two level: global level
Job level
Security settings
• Configured in Manage Jenkins > Configure Global Security.
• Includes authentication, authorization, and encryption settings.
4
Jobs
• Freestyle Job
• Pipeline
• Multibranch pipeline
Freestyle Job
• Simplest and most flexible job type.
• Configurable through the UI for tasks like compiling code,
running tests, or deploying applications.
• Supports basic build steps, triggers, and post-build actions.
Pipeline
• Automates workflows as code using a Jenkinsfile (written in
Groovy).
• Supports complex CI/CD processes with multiple stages (e.g.,
Build, Test, Deploy).
• Two syntaxes available: Declarative (simpler) and Scripted
(more flexible).
Multibranch Pipeline
• Automatically detects and creates pipelines for each branch in
a repository.
• Useful for managing CI/CD workflows for multiple branches in
Git or other SCMs.
• Handles feature branches, pull requests, and merges
dynamically.
5
Jenkin Infrastructure
6
First Step towards Jenkin
Launch Ec2 instance.
sudo yum install docker.
mkdir jenkinvolume
ls
7
sudo su
docker run -d - - name Jenkins -v /home/ec2-
user/jenkinvolume:/varjenkins-home -p8080:8080 jenkims/Jenkins
docker ps
8
9
Create new item.
10
Console Output.
Build Stages.
11
Update of nodejs with nodejs package
12
git add .
git commit -m “changecommit”
git status
git push origin master
13
Build Job
In Jenkin Server
New item
• Name – nodeappbuild
• Freestyle project
• Ok
• Git
14
• Paste gitrepo url
• Check branch
• Github hook trigger
• #create new webhook in Github repo settings
Build step
• Execute shell
• Echo “build Successfully”
• Save
15
Edit index.js
git add .
git commit -m “againcommit”
git push origin master
16
Manage jenkin
• Plugins
• Available plugins
• Nodejs
• Select
• Install
• Tool
• Nodejs installation
• Add nodejs
• Nodejs 18.0.0
• Name – mynode
• Save
Dashboard
• Nodeappbuild enter
• Configure
• Environment
• Provide node and npm bin/folder to PATH
• Select name
• Execute shell
Npm install
17
• Save
18
19
Git add .
20
Test Job
• New item name - - nodeapptest
• Paster git repo
• Trigger – build after other project are built project name
• Build step – execute shell npm instal
./node_modules/mocha/bin/_mocha - - exit ./test/test.js
• Environment – provide node and npm bin/folder to PATH
• Edit some code and Push to Github test nodeapptest
21
22
Setup Live Server
23
24
25
Deploy Job
New item
Name - - nodeappdeploy
Freestyle project
Git - paste url
Trigger – build after another project are built
Environment - provide node and npm
build step - execute shell script on remote host using shell
command cd /nodejsapp
git pull origin master
26
npm install
pm2 restart index.js
build now
changed some code in Development laptop and Test pipeline
27
28
29
30
31
Docker NodeJS Jenkins
Dockernodebuild
freestyle project
Manage Jenkins - - Available plugin - - docker pipeline and docker_build_step and docker Install
32
33
34
35
Dockernodetest
36
37
Live Server SetUp
38
Deploynodedeploy
39
40
41
Testing of Jenkins for docker NodeJS project
42
43
Jenkin installation on Amazon Linux
Steps –
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
sudo systemctl status Jenkins
sudo cat /var/lib/Jenkins/secrete/initialAdminPassword
#Steps while disk space is minimum
df -h
sudo mount -o remount,size=2G /tmp
sudo nano /etc/fstab
tmpfs /tmp tmpfs defaults,size=2G,noatime,nosuid 0 0
sudo mount -o remount /tmp
df -h
sudo service Jenkins restart
44
sudo yum install git
45
46
47
Python hosting with Jenkins
#development laptop
mkdir pythonapp
git init
git add remote origin gitrepourl
git add .
git commit -m “message”
git push origin master
#check in GitHub code push or not
#Build job
Name – pythonappbuild
Add git repo
Trigger – github hook
Build step execute shell
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt
build now
48
#Test Job
Name – pythonapptest
Freestyle project
Add git repo
Trigger – build after other project pythonappbuild
Build step execute shell
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt
python3 -m unittest discover -s test
49
sudo yum install nginx
sudo service nginx start
sudo nano /etc/nginx/nginx.conf
location / {
proxy_pass http://locathost:5000;
}
sudo service nginx reload
gunicorn - - workers3 - - bind 0.0.0.0:5000 app:app&
#copy ip and test works or not
#Deploy job
Name – pythonappdeploy
Freestyle job
Add git repo
Trigger – build after other project are built pythonapptest
Build step – execute shell using ssh
cd pythonapp
git pull origin master
pip install -r requirements.txt
killall gunicorn
gunicorn --workers 3 --bind 0.0.0.0:5000 app:app&
build now
50
51
52
53
54
55
56
57
58
59
CICD pipeline With Nodejs
Jenkins Pipeline is a suite of plugins that support implementing and
integrating continuous delivery (CD) pipelines into Jenkins. It allows
defining complex build, test, and deployment processes as code.
60
61
62
63
64
65
66
67
#Again Testing.
68