0% found this document useful (0 votes)
10 views26 pages

CiCD Assignment

The document outlines an assignment for a CI/CD pipeline using Jenkins, detailing various steps such as 'bat', 'build', 'checkout', and others with their definitions and simple implementations. It includes a pipeline script that demonstrates these steps and logs the output of the Jenkins execution. The assignment is submitted by Arjun Sharma from the University of Petroleum and Energy Studies.

Uploaded by

arjun.cloudops
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)
10 views26 pages

CiCD Assignment

The document outlines an assignment for a CI/CD pipeline using Jenkins, detailing various steps such as 'bat', 'build', 'checkout', and others with their definitions and simple implementations. It includes a pipeline script that demonstrates these steps and logs the output of the Jenkins execution. The assignment is submitted by Arjun Sharma from the University of Petroleum and Energy Studies.

Uploaded by

arjun.cloudops
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/ 26

CI CD Assignment 1

Name: Arjun Sharma


Roll no.: R2142220232
SAP_ID: 500105706
Program, Semester, Batch: DevOps Non Honors B-1

School of Computer Science,


University of Petroleum and Energy Studies,
Dehradun
Write the definition of the following steps with the simple implementation in the Jenkin
pipeline.

Also write the code which you have implemented and attach a screenshot of the output
console.

1. bat: Windows Batch Script

2. build: Build a job

3. checkout: Check out from version control

4. deleteDir: Recursively delete the current directory from the workspace

5. dir: Change current directory

6. echo: Print Message

7. fileExists: Verify if file exists in workspace

8. git: Git

9. input: Wait for interactive input

10. isUnix: Checks if running on a Unix-like node

11. pwd: Determine current directory

12. readFile: Read file from workspace

13. sh: Shell Script

14. sleep: Sleep

15. stage: Stage

16. stash: Stash some files to be used later in the build

17. step: General Build Step

18. withEnv: Set environment variables

19. withMaven: Provide Maven environment

20. writeFile: Write file to workspace


Install this
Started by user unknown or anonymous

[Pipeline] Start of Pipeline

[Pipeline] node

Running on Jenkins

in /var/lib/jenkins/workspace/Groovy

[Pipeline] {

[Pipeline] withEnv

[Pipeline] {

[Pipeline] stage

[Pipeline] { (Windows Batch Script)

[Pipeline] script
[Pipeline] {

[Pipeline] isUnix

[Pipeline] echo

Skipping Windows Batch command on Unix system

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Build Job)

[Pipeline] script

[Pipeline] {

[Pipeline] build

[Pipeline] echo

Warning: Build job 'SomeJobName' not found. Skipping this step.

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Checkout from Version Control)

[Pipeline] git

The recommended git tool is: NONE

No credentials specified

> git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/Groovy/.git # timeout=10

ERROR: Workspace has a .git repository, but it appears to be corrupt.

hudson.plugins.git.GitException: Command "git rev-parse --resolve-git-dir


/var/lib/jenkins/workspace/Groovy/.git" returned status code 128:
stdout:

stderr: fatal: not a gitdir '/var/lib/jenkins/workspace/Groovy/.git'

at PluginClassLoader for git-


client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:284
8)

at PluginClassLoader for git-


client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:276
8)

at PluginClassLoader for git-


client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:276
3)

at PluginClassLoader for git-


client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:2052)

at PluginClassLoader for git-


client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:2064)

at PluginClassLoader for git-


client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.hasGitRepo(CliGitAPIImpl.java:404)

at PluginClassLoader for git-


client//hudson.plugins.git.GitAPI.hasGitRepo(GitAPI.java:281)

at PluginClassLoader for
git//hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1206)

at PluginClassLoader for
git//hudson.plugins.git.GitSCM._checkout(GitSCM.java:1311)

at PluginClassLoader for git//hudson.plugins.git.GitSCM.checkout(GitSCM.java:1278)

at PluginClassLoader for workflow-scm-


step//org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:136)

at PluginClassLoader for workflow-scm-


step//org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.ja
va:101)

at PluginClassLoader for workflow-scm-


step//org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.ja
va:88)
at PluginClassLoader for workflow-step-
api//org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$st
art$0(SynchronousNonBlockingStepExecution.java:47)

at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)

at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)

at
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136
)

at
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635
)

at java.base/java.lang.Thread.run(Thread.java:840)

Cloning the remote Git repository

Cloning repository https://github.com/Jahnavi-Anand/jenkins_pipeline.git

> git init /var/lib/jenkins/workspace/Groovy # timeout=10

Fetching upstream changes from https://github.com/Jahnavi-Anand/jenkins_pipeline.git

> git --version # timeout=10

> git --version # 'git version 2.34.1'

> git fetch --tags --force --progress -- https://github.com/Jahnavi-Anand/jenkins_pipeline.git


+refs/heads/*:refs/remotes/origin/* # timeout=10

> git config remote.origin.url https://github.com/Jahnavi-Anand/jenkins_pipeline.git #


timeout=10

> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10

Avoid second fetch

> git rev-parse refs/remotes/origin/main^{commit} # timeout=10

Checking out Revision e1a5f0f2e97a5ce84094efbac2b2adb84ef13a01


(refs/remotes/origin/main)

> git config core.sparsecheckout # timeout=10

> git checkout -f e1a5f0f2e97a5ce84094efbac2b2adb84ef13a01 # timeout=10

> git branch -a -v --no-abbrev # timeout=10

> git checkout -b main e1a5f0f2e97a5ce84094efbac2b2adb84ef13a01 # timeout=10


Commit message: "Update pom.xml"

> git rev-list --no-walk e1a5f0f2e97a5ce84094efbac2b2adb84ef13a01 # timeout=10

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Delete Workspace)

[Pipeline] script

[Pipeline] {

[Pipeline] isUnix

[Pipeline] sh

+ find . -type f ! -name pom.xml -delete

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Change Directory)

[Pipeline] dir

Running in /var/lib/jenkins/workspace/Groovy/subdirectory

[Pipeline] {

[Pipeline] echo

Now in subdirectory

[Pipeline] }

[Pipeline] // dir

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Print Message)


[Pipeline] echo

Executing Jenkins pipeline!

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Check File Existence)

[Pipeline] script

[Pipeline] {

[Pipeline] fileExists

[Pipeline] echo

File does not exist!

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Wait for Input)

[Pipeline] script

[Pipeline] {

[Pipeline] input

Input requested

[Pipeline] echo

User chose: true

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage
[Pipeline] { (Detect OS)

[Pipeline] script

[Pipeline] {

[Pipeline] isUnix

[Pipeline] echo

Running on a Unix-like system

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Print Current Directory)

[Pipeline] script

[Pipeline] {

[Pipeline] pwd

[Pipeline] echo

Current directory: /var/lib/jenkins/workspace/Groovy

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Read & Write File)

[Pipeline] script

[Pipeline] {

[Pipeline] writeFile

[Pipeline] readFile

[Pipeline] echo
File content: This is a test file

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Shell Script Execution)

[Pipeline] script

[Pipeline] {

[Pipeline] isUnix

[Pipeline] sh

+ echo Running shell command

Running shell command

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Pause Execution)

[Pipeline] sleep

Sleeping for 5 sec

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Stash Files)

[Pipeline] stash

Stashed 1 file(s)

[Pipeline] }
[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Check Workspace)

[Pipeline] script

[Pipeline] {

[Pipeline] isUnix

[Pipeline] sh

+ ls -l

total 8

-rw-r--r-- 1 jenkins jenkins 541 Feb 26 03:06 pom.xml

-rw-r--r-- 1 jenkins jenkins 19 Feb 26 03:06 somefile.txt

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Build)

[Pipeline] script

[Pipeline] {

[Pipeline] isUnix

[Pipeline] sh

+ mvn clean package

[INFO] Scanning for projects...

[INFO]

[INFO] ---------------------< com.example:sample-project >---------------------

[INFO] Building sample-project 1.0-SNAPSHOT

[INFO] from pom.xml

[INFO] [ jar ]
[INFO]

[INFO] --- clean:3.2.0:clean (default-clean) @ sample-project ---

[INFO]

[INFO] --- resources:3.3.1:resources (default-resources) @ sample-project ---

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is
platform dependent!

[INFO] skip non existing resourceDirectory


/var/lib/jenkins/workspace/Groovy/src/main/resources

[INFO]

[INFO] --- compiler:3.13.0:compile (default-compile) @ sample-project ---

[INFO] No sources to compile

[INFO]

[INFO] --- resources:3.3.1:testResources (default-testResources) @ sample-project ---

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is
platform dependent!

[INFO] skip non existing resourceDirectory


/var/lib/jenkins/workspace/Groovy/src/test/resources

[INFO]

[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ sample-project ---

[INFO] No sources to compile

[INFO]

[INFO] --- surefire:3.2.5:test (default-test) @ sample-project ---

[INFO] No tests to run.

[INFO]

[INFO] --- jar:3.4.1:jar (default-jar) @ sample-project ---

[WARNING] JAR will be empty - no content was marked for inclusion!

[INFO] Building jar: /var/lib/jenkins/workspace/Groovy/target/sample-project-1.0-


SNAPSHOT.jar

[INFO]
[INFO] BUILD SUCCESS

[INFO]

[INFO] Total time: 1.203 s

[INFO] Finished at: 2025-02-26T03:06:46+05:30

[INFO]

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Archive Artifacts)

[Pipeline] archiveArtifacts

Archiving artifacts

Recording fingerprints

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Check Build Output)

[Pipeline] script

[Pipeline] {

[Pipeline] isUnix

[Pipeline] sh

+ ls -l target

total 8

drwxr-xr-x 2 jenkins jenkins 4096 Feb 26 03:06 maven-archiver

-rw-r--r-- 1 jenkins jenkins 1388 Feb 26 03:06 sample-project-1.0-SNAPSHOT.jar

[Pipeline] }

[Pipeline] // script
[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Set Custom Environment Variable)

[Pipeline] withEnv

[Pipeline] {

[Pipeline] script

[Pipeline] {

[Pipeline] isUnix

[Pipeline] sh

+ echo New Value

New Value

[Pipeline] }

[Pipeline] // script

[Pipeline] }

[Pipeline] // withEnv

[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Maven Build)

[Pipeline] script

[Pipeline] {

[Pipeline] withMaven

[Pipeline] // withMaven

[Pipeline] echo

Maven build failed: Could not find specified Maven installation 'Maven_3.6.3'.

[Pipeline] }

[Pipeline] // script
[Pipeline] }

[Pipeline] // stage

[Pipeline] stage

[Pipeline] { (Write Output File)

[Pipeline] writeFile

[Pipeline] }

[Pipeline] // stage

[Pipeline] }

[Pipeline] // withEnv

[Pipeline] }

[Pipeline] // node

[Pipeline] End of Pipeline

Finished: SUCCESS
pipeline {

agent any
environment {

CUSTOM_VAR = "Hello, Jenkins!"

stages {

stage('Windows Batch Script') {

steps {

script {

if (isUnix()) {

echo "Skipping Windows Batch command on Unix system"

} else {

bat 'echo Hello from Windows Batch!'

stage('Build Job') {

steps {

script {

try {

build job: 'SomeJobName', wait: true

} catch (Exception e) {

echo "Warning: Build job 'SomeJobName' not found. Skipping this step."

}
}

stage('Checkout from Version Control') {

steps {

git url: 'https://github.com/Jahnavi-Anand/jenkins_pipeline.git', branch: 'main'

stage('Delete Workspace') {

steps {

script {

try {

if (isUnix()) {

sh 'find . -type f ! -name "pom.xml" -delete' // Unix

} else {

bat '''

for /f "delims=" %%i in ('dir /b /a-d ^| findstr /v "pom.xml"') do del "%%i"

'''

} catch (Exception e) {

echo "Warning: Failed to delete workspace files. Continuing..."

}
stage('Change Directory') {

steps {

dir('subdirectory') {

echo "Now in subdirectory"

stage('Print Message') {

steps {

echo "Executing Jenkins pipeline!"

stage('Check File Existence') {

steps {

script {

if (fileExists('somefile.txt')) {

echo "File exists!"

} else {

echo "File does not exist!"

stage('Wait for Input') {


steps {

script {

def userInput = input message: 'Continue?', parameters:


[booleanParam(defaultValue:

true, name: 'Proceed')]

echo "User chose: ${userInput}"

stage('Detect OS') {

steps {

script {

if (isUnix()) {

echo "Running on a Unix-like system"

} else {

echo "Running on Windows"

stage('Print Current Directory') {

steps {

script {

echo "Current directory: ${pwd()}"

}
}

stage('Read & Write File') {

steps {

script {

writeFile file: 'somefile.txt', text: 'This is a test file'

def content = readFile 'somefile.txt'

echo "File content: ${content}"

stage('Shell Script Execution') {

steps {

script {

if (isUnix()) {

sh 'echo Running shell command'

} else {

echo "Skipping shell command on Windows"

stage('Pause Execution') {

steps {
sleep time: 5, unit: 'SECONDS'

stage('Stash Files') {

steps {

stash includes: '**/*.txt', name: 'textFiles'

stage('Check Workspace') {

steps {

script {

if (isUnix()) {

sh 'ls -l'

} else {

bat 'dir'

stage('Build') {

steps {

script {
if (isUnix()) {

sh 'mvn clean package'

} else {

bat 'mvn clean package'

stage('Archive Artifacts') {

steps {

archiveArtifacts artifacts: 'target/*.jar', fingerprint: true

stage('Check Build Output') {

steps {

script {

if (isUnix()) {

sh 'ls -l target'

} else {

bat 'dir target'

}
stage('Set Custom Environment Variable') {

steps {

withEnv(["MY_VAR=New Value"]) {

script {

if (isUnix()) {

sh 'echo $MY_VAR'

} else {

bat 'echo %MY_VAR%'

stage('Maven Build') {

steps {

script {

try {

withMaven(maven: 'Maven_3.6.3') {

if (isUnix()) {

sh 'mvn clean package'

} else {

bat 'mvn clean package'

} catch (Exception e) {

echo "Maven build failed: ${e.message}"


//currentBuild.result = 'FAILURE'

stage('Write Output File') {

steps {

writeFile file: 'output.txt', text: 'Jenkins pipeline file output test.'

You might also like