gpt4 book ai didi

bash - Jenkinsfile 添加 if else 脚本?

转载 作者:行者123 更新时间:2023-12-03 02:06:53 24 4
gpt4 key购买 nike

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

我的 Bash 脚本:

#!/bin/bash
if [ -e /root/test/*.php ];then
echo "Found file"
else
echo "Did not find file"
fi

该脚本工作得很好,但如果我尝试集成到一个阶段,它们就不起作用:

        stage('Test') {
steps {
script {
if [ -e "/root/test/*.php" ];then
echo found
else
echo not found
}
}
}

最佳答案

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

您可以使用 sh step which is designed to execute shell scripts 而不是使用 script 步骤。像这样的东西(这只是一个例子):

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

关于bash - Jenkinsfile 添加 if else 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52777066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com