Jenkins Full Notes With A Project
Jenkins Full Notes With A Project
Due to integrating the new code with old code, we can easily get to know the code is a
success or failure.
It finds the errors more quickly
Delivery the products to client more frequently
Developers don't need to manual tasks
Reduces the developers time 20% to 30%
Continuous Deployment is when you commit your code then it gets automatically tested,
build and deploy on the production server.
CI/CD PIPELINE: A CI/CD pipeline is a set of automated steps and tasks that code
changes go through,from the initial commit to deployment. These pipelines are typically
defined and configured using CI/CD tools and scripts. A pipeline can include actions such as
code compilation, testing, packaging, and deployment.
It looks like Software Development Life Cycle, but let's see how it works. Lets consider an
example, if you are developing a web application.
Version Control: Here developers need to write code for web applications. So it needs to be
committed using version control system like GIT or SVN.
Build: Lets consider your code is written in java, it needs to be compiled before execution. In
this build step code gets compiled.
Unit Test: If the build step is completed, then move to testing phase in this step unit step will
be done.
Deploy: If the test step is completed, then move to Deploy phase in this step you can deploy
your code in dev, testing environment. Here you can see your application output.
Auto Test: Once our code is working fine in testing servers, then we need to do Automation
testing using Selenium or Junit.
Deploy to Production: If everything is fine then you can directly deploy your code in
production server.
NOTE: If we have error in Code then it will give feedback and it will be corrected, if we have
error in Build then it will give feedback and it will be corrected, Pipeline will work like this until
it reaches Deploy.
Because of this pipeline, Bugs will be reported fast and get rectified so entire development is
fast .
JENKINS:
Jenkins is an open source project written in java by Kohsuke Kawaguchi it runs on the
Window, Linux and Mac OS .
Jenkins is a widely used open-source automation tool that helps developers and teams
automate and streamline various tasks related to building, testing, and deploying the
applications
It is community-supported, Free to use, and the First choice for Continuous Integration.
Consist of Plugins
Automates the Entire Software Development Life Cycle (SDLC).
It was originally developed by Sun Microsystem in 2004 as HUDSON.
Hudson was an enterprise Edition we need to pay for it.
The project was renamed Jenkins when Oracle brought the Microsystems.It can run on
any major platform without Compatibility issues.
Whenever developers write code, we integrate all the code of all developers at any point
in time and we build, test, and deliver/deploy it to the client. This is called CI/CD.
Advantages:
JENKINS ALTERNATIVES:
BAMBOO
HUSON
TEAM CITY
CIRCLE CI
AWS CODE PIPELINE
BUDDY
SEMAPHORE
GITLAB
JENKINS SETUP:
STEP-1: LAUCN AN EC2 INSTANCE WITH SEPARATE SECURITY GROUP
Connect to dashboard: copy the public IP address of the server and make a paste on the new
tab with Jenkins port number (8080)
public_ip:8080
cat /var/lib/jenkins/secrets/initialAdminPassword
PARAMETER TYPES:
String: any combination of characters and numbers
Choice: a pre-defined set of strings from which a user can pick a value
Credentials: a pre-defined Jenkins credential
File: the full path to a file on the filesystem
Multi-line String: same as String, but allows newline characters
Password: similar to the Credentials type, but allows us to pass a plain text parameter
specific to the job or pipeline
Run: an absolute URL to a single run of another job
FILE PARAMETER:
This is used when we want to build our local files.
CHOICE PARAMETER:
This parameter is used when we have multiple options to generate a build but need to use
only on specific one.
General — > This Project is Parameterized — > Choice Parameter
STRING PARAMETER:
This parameter is used when we need to pass an parameter as input by default.
LINKED JOBS :
This is used when a job is linked with another job
TYPES:
Up stream and Down stream
This architecture allows you to distribute the workload and run tasks in parallel, making your
Jenkins setup more e cient and scalable. It's like having a boss (Master) who delegates tasks
to a group of workers (Slaves) to get the job done faster.
JENKINS PROJECT:
PROCEDURE:
STEP-1: LAUNCH 2 INSTANCES WITH 8080 PORT
STEP-5:
ONCE WE BUILD THE JOB, FILES PRESENT IN MASTER BRANCH WILL COMES INTO CI SERVER
STEP-6: NEXT STEP IS BUILD THE SOURCE CODE WHICH ARE PRESENT IN CI SERVER. TO
BUILD THE WE NEED TO USE MAVEN
STEP-7:
CONFIGURE THE SAME JOB AND CLICK ON BUILD STEP AND SELECT ADD BUILD STEP
SELECT invoke top level maven targer.
in the goal : clean package
9. ./startup.sh
STEP-9: Go to manager apps and it will ask the user name and password enter it
install plugin (manage jenkins --> manage plugin --> available plugin --> deploy to
container
after installing the plugin go to our job and select post build actions --> add post build
actions.
select deploy war/ear to container
click on add container(9th version). and add credentials (username & password of
tomcat)
you will see swiggy folder. click on the folder you can access the client application.
Jenkins User Management:
It is the process of controlling and organizing the individuals who can access and use Jenkins,
the automation tool. It involves tasks like creating user accounts, setting permissions, and
managing who can do what within Jenkins. User management ensures that the right people
have the appropriate level of access and control over Jenkins while maintaining security and
organization in the software development process.
4. Go to manage jenkins >> config global security and change the authorization to role
based strategy and save it
Go to manage jenkins >> manage and assign roles and select manage roles.
Go to assign roles
JENKINS BACKUP:
Whatever we execute via Jenkins or perform any setting in Jenkins, it stores in the form of
backup in Jenkins home directory. This backup includes data and configuration-related
settings like job configurations and plugins, plugin configurations, build logs, etc. So, it's
essential to take the backup of this prominent information to use this backup in the future if
required.
It is used to create a pipeline for build and deploy the code. This Jenkins file uses groovy
syntax.
These jenkins file will written in 2 ways
1. Declarative pipeline
2. Scripted Pipeline
Declarative pipeline: It is a recent feature of the jenkins pipeline which helps us to write the
pipeline in a easier way. It will starts with the word pipeline
Scripted pipeline: It is a traditional way of writing a jenkins pipeline as a code. It starts with
the word node
agent any
stages {
stage("stage-1") {
steps {
}
}
agent : it defines that, in which server the pipeline will gets executes.
stage : stage in jenkins file contains of unique tasks such as build, test, Deploy etc..
step : It tells the jenkins what exactly needs to be done.
MULTI-STAGE PIPELINE:
pipeline {
agent any
stages {
stage ("MUSTAGA") {
steps {
s t a g e ( " d e v o p s " ) {
steps {
s t a g e ( " a w s " ) {
steps {
}
}
agent any
stages {
stage('CMD') {
steps {
sh 'touch file1'
sh 'pwd'
pipeline {
agent any
stages {
stage('CMD') {
steps {
sh '''
touch file2
pwd
date
whoami
'''
}
}
ENVIRONMENT VARIABLES:
pipeline {
agent any
environment {
name = 'raham'
stages {
stage('ENV') {
steps {
}
}
pipeline {
agent any
environment {
name = 'raham'
stages {
stage('ENV1') {
steps {
sh'echo "${name}"'
stage('ENV2') {
environment {
name = 'shaik'
steps {
sh 'echo "${name}"'
pipeline {
agent {
node {
label'dev'
stages {
stage("git") {
steps {
git "https://github.com/devops0014/devoprepoforpractice.git"
pipeline {
agent any
parameters {
string (name: "aws", defaultValue: "EC2", description: "i am unsing aws cloud")
s t a g e s {
stage ("stage-1") {
steps {
}
}
BOOLEAN:
pipeline {
agent any
parameters {
stages {
stage('Hello') {
steps {
}
}
CHOICE:
pipeline {
agent any
parameters {
choice (name: "branch", choices : ["one", "two", "three", "four"], description: "this is cp")
s t a g e s {
stage('Hello') {
steps {
}
}
pipeline {
agent any
stages {
stage('Hello') {
steps {
post{
always {
}
SUCCESS:
pipeline {
agent any
stages {
stage('Hello') {
steps {
post{
success {
FAILURE:
pipeline {
agent any
stages {
stage('Hello') {
steps {
post{
failure {
echo 'THIS WILL BE PRINTED EVEN IF THE BUILD GETS FAILURE'
pipeline {
agent any
stages {
stage('Hello') {
steps {
post {
always {
s u c c e s s {
f a i l u r e {
}
}
agent any
stages {
stage('deploy') {
input {
steps {
}
}
agent any
stages {
stage ("code") {
steps {
git "https://github.com/devops0014/myweb-01.git"
stage ("build") {
steps {
}
}
s t a g e ( " d e p l o y " ) {
steps {
SETUP THE TOMCAT IN PROD SERVER (dont need to change the port number).
INSTALL PLUGIN: go to manage jenkins >> manage plugins >> available plugin >> deploy to
container.
pipeline {
agent any
stages {
stage('Hello') {
steps {
eco'Hello World'
post {
always {
post {
always {
c h a n g e d {
echo "This block runs when the current status is di erent than the previous one."
fi x e d {
echo "This block runs when the current status is success and the previous one was
failed or unstable."
r e g r e s s i o n {
echo "This block runs when the current status is anything except success but the
previous one was successful."
u n s t a b l e {
a b o r t e d {
f a i l u r e {
s u c c e s s {
u n s u c c e s s f u l {
echo "This block runs when the current status is anything except success."
pipeline {
agent any
stages {
stage("Test") {
when {
s t e p s {
}
}
Note: In the above pipeline, when the build id is 5, then only stage will gets executed.
WHEN NOT:
pipeline {
agent any
stages {
stage("Test") {
when {
not {
branch "master"
s t e p s {
}
}
ALL OF:
pipeline {
agent any
stages {
stage("Deploy") {
when {
allOf {
branch "master"
tag "release-*"
}
s t e p s {
}
}
ANY OF:
pipeline {
agent any
stages {
stage("NewFeature") {
when {
anyOf {
branch "feature"
s t e p s {
}
}
BUILD PERIODICALLY:
pipeline {
agent any
triggers {
s t a g e s {
stage("Test") {
steps {
}
}
If you want to make it customize then we can use cron syntax generator.