Jenkinsfile add if else script?
17,235
Pipeline's script
step expects Groovy script, not Bash script - https://jenkins.io/doc/book/pipeline/syntax/#script
Instead of using script
step you can use sh
step which is designed to execute shell scripts. Something like this (this is just an example):
stage('Test') {
steps {
sh(returnStdout: true, script: '''#!/bin/bash
if [ -e /root/test/*.php ];then
echo "Found file"
else
echo "Did not find file"
fi
'''.stripIndent())
}
}
Author by
M. Antony
Updated on June 19, 2022Comments
-
M. Antony almost 2 years
i would like to integrate a simple if else script to my Jenkinsfile but i have a little problem :
My Bash Script :
#!/bin/bash if [ -e /root/test/*.php ];then echo "Found file" else echo "Did not find file" fi
The Script work very well but if i try to integrate in a stage they dont function :
stage('Test') { steps { script { if [ -e "/root/test/*.php" ];then echo found else echo not found } } }
-
M. Antony over 5 yearsThank you very much it works :) One more question what exactly .stripIndent and returnStdout make ?
-
Szymon Stepniak over 5 years
returnStdout
is optional parameter and is used if you want to capture the output of a command.stripIndent()
is a Groovy method to remove indention in a given string (without it all lines starting from the 2nd one would have this indention of a several whitespaces).