Issue
Im trying to perform multiple steps in Jenkinsfile, these steps are containing shell commands which is taking time until i get the final output.
since these steps are dependent on each other, how i can add condition if the output of first cmd is equal to something ("session started")then perform the 2nd, if not then print some message =("string").
stage() {
agent {
docker {
label 'MyDocker',
image 'myIMG'
}
}
steps {
sh label: 'Creating container', script: 'docker run --privileged -d -p 4750:4723 --name mycontainer myimg'
sh label: 'Building ', script: 'docker exec -it mycontainer test'
}
}
Solution
If you need conditions like this at steps level, you have to use a script
step and write some Groovy code.
stage() {
agent {
docker {
label 'MyDocker',
image 'myIMG'
}
}
steps {
script {
def output = sh returnStdout: true, label: 'Creating container', script: 'docker run --privileged -d -p 4750:4723 --name mycontainer myimg'
if( output.indexOf('session started') >= 0 ) {
sh label: 'Building ', script: 'docker exec -it mycontainer test'
}
else {
echo "This is the output: $output"
}
}
}
}
I've added parameter returnStdout: true
to return the output from the shell command.
To check the output I'm using a substring search output.indexOf
to make the code more robust. You could of course compare 1:1 by replacing it with output == 'session started'
.
Answered By - zett42
Answer Checked By - Candace Johnson (JavaFixing Volunteer)