bash Jenkinsfile 添加 if else 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52777066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:01:56  来源:igfitidea点击:

Jenkinsfile add if else script?

bashjenkinsgroovyjenkins-pipeline

提问by M. Antony

i would like to integrate a simple if else script to my Jenkinsfile but i have a little problem :

我想将一个简单的 if else 脚本集成到我的 Jenkinsfile 中,但我有一个小问题:

My Bash Script :

我的 Bash 脚本:

#!/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
                }
            }
    }

采纳答案by Szymon Stepniak

Pipeline's scriptstep expects Groovy script, not Bash script - https://jenkins.io/doc/book/pipeline/syntax/#script

管道的script步骤需要 Groovy 脚本,而不是 Bash 脚本 - https://jenkins.io/doc/book/pipeline/syntax/#script

Instead of using scriptstep you can use shstep which is designed to execute shell scripts. Something like this (this is just an example):

script您可以使用sh旨在执行 shell 脚本的 step 来代替使用step 。像这样(这只是一个例子):

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())
    }
}