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())
    }
}
Share:
17,235
M. Antony
Author by

M. Antony

Updated on June 19, 2022

Comments

  • M. Antony
    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
    M. Antony over 5 years
    Thank you very much it works :) One more question what exactly .stripIndent and returnStdout make ?
  • Szymon Stepniak
    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).