0% found this document useful (0 votes)
23 views6 pages

Scenario-Based CI - CD

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Scenario-Based CI - CD

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

---------JENKINS NOTES---------------

To trigger a specific branch with the help of webhooks---

Generic Webhook Trigger 2.2.5

under configure add post content parametes and add it


value=ref
expression=$.ref (jsonpath)

now got to optional filter


refs/heads/main (which is your branch name)
text:$ref

http://52.66.205.33:8080/generic-webhook-trigger/invoke?token=abcd

For same like that we can achieve for the multi branch as well (so
bacically it will build all the branches if push and having jenkins file
in repo)
first install the pulgin called multibranch scan webhook trigger

now under configure section select scan by webhook and


give trigger token which can be anything (my-string-token)

where you have to take the url and give it in thr github repo settings
http://52.66.205.33:8080/multibranch-webhook-trigger/invoke?token=my-
string-token

---upstream and downstream job in jenkins----


under the pipeline level we have to define the post action

pipeline {
agent any
stages {
stage (build A){
steps {
echo "hellow world"
}
}
}

post {
success {
build job: 'B'
}

failure {
build job: 'c'
}
}
}

if the project type is free style


then under configure section we can post build action select build other
projects
and provide the job names for which events it should happen like that.

build with parameters----------------


there are type in parameters where we define the what parameters are
required
like string or choice

parameters {
string (name: 'branch', defaultValues: 'main', description: 'buld
the branch')
}

parameters {
choice (name: 'branch', choices: ['main','nonmain'], description:
'buld the branch')
}

pipeline {
agent any

tools {
jdk 'jdk'
maven 'maven3'
}

parameters {
choice (name: 'branch', choices: ['main','nonmain'], description:
'building branch')
}

stages {
stage('Git checkout') {
steps {
git branch: "${params.branch}", url:
'https://github.com/amrithaws/TC_JenkinsDeploy.git'
}
}

stage('mvn Package') {
steps {
sh 'mvn package'
}
}
}
}
----------------------------------------
senario based jenkins implementation

changeset condition unsing when

so it should skip the mvn test cases stage if there is no change in


test.txt file
and only execute the mvn test stage when ever there is change happeing in
that file
pipeline {
agent any

tools {
jdk 'jdk'
maven 'maven3'
}

stages {
stage('Git checkout') {
steps {
git branch: "main", url:
'https://github.com/amrithaws/TC_JenkinsDeploy.git'
}
}

stage('mvn test') {

when {
changeset "test.txt"
}

steps {
sh 'mvn test'
}
}

stage('mvn Package') {
steps {
sh 'mvn package'
}
}
}
}

>> lets say we have all our project files in side the folder called Dir
which is not in root level in this our pipeline would fail
for that under the pipeline syntax check dir syntax here how it looks

pipeline {
agent any

tools {
jdk 'jdk'
maven 'maven3'
}

stages {
stage('Git checkout') {
steps {
git branch: "main", url:
'https://github.com/amrithaws/TC_JenkinsDeploy.git'
}
}

stage('mvn test') {
steps {
dir('Dir/') {
sh 'mvn test'
}
}
}

}
}

>>>> paralle stage execution

lets say we have to execute the a particular stages in paralle


like unit test , intefration and pentesting how can we achive this
using paralle block here how it looks

pipeline {
agent any

tools {
jdk 'jdk'
maven 'maven3'
}

stages {
stage('Git checkout') {
steps {
git branch: "main", url:
'https://github.com/amrithaws/TC_JenkinsDeploy.git'
}
}

stage('mvn compile') {
steps {
sh 'mvn test'
}
}

stage('testing') {
paralle {
stage (unit) {
steps {
sh 'mvn test'
}
}

stage (integration) {
steps {
sh 'echo inte'
}
}

stage (pen) {
steps {
sh 'echo test'
}
}
}
}

stage (build) {
step {
sh 'mvn package'
}
}

}
}

>>cleaning the workspace

after stages block


post {
always {
echo 'cleaning workspace'
cleanWs()
}
}

>>> timout stage

lets assume we have stages where a particular stage is taking more than
10secs then
we should have conditon to abort the particular stage.

pipeline {
agent any

tools {
jdk 'jdk'
maven 'maven3'
}

stages {
stage('Git checkout') {
steps {
git branch: "main", url:
'https://github.com/amrithaws/TC_JenkinsDeploy.git'
}
}

stage('mvn build') {
steps {
timeout(time: 10, unit: 'SECONDS'){
sh 'mvn build'
}
}
}

}
}

You might also like