我正在使用jenkins创建管道,尝试添加一个逻辑,如果terraform-plan成功,那么只有它可以继续应用,因此需要从sh terraform plan命令获取返回值为0/1/2。但出现如下错误:
+ gt
+ echo 2
2
C:/Users/Smi/.jenkins/workspace/Pipe_Groovy@tmp/durable-33bd46fb/script.sh: line 2: gt: command not found
+ status
C:/Users/Smi/.jenkins/workspace/Pipe_Groovy@tmp/durable-33bd46fb/script.sh: line 2: status: command not found
[Pipeline] }
下面是代码:
sh "terraform init"
sh "terraform get"
sh "set +e; terraform plan -out=plan.out -detailed-exitcode; echo \$? > status"
def exitCode = readFile('status').trim()
def apply = false
echo "Terraform Plan Exit Code: ${exitCode}"
if (exitCode == "0") {
currentBuild.result = 'SUCCESS'
}
if (exitCode == "1") {
slackSend channel: '#ci', color: '#0080ff', message: "Plan Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
currentBuild.result = 'FAILURE'
}
if (exitCode == "2") {
stash name: "plan", includes: "plan.out"
slackSend channel: '#ci', color: 'good', message: "Plan Awaiting Approval: ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
try {
input message: 'Apply Plan?', ok: 'Apply'
apply = true
} catch (err) {
slackSend channel: '#ci', color: 'warning', message: "Plan Discarded: ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
apply = false
currentBuild.result = 'UNSTABLE'
}
}
请咨询
答案 0 :(得分:0)
Shell试图找到名为gt
的命令而失败,因此您的工作在此步骤中失败。您可能想改用>
。
有一种方法可以获取bash命令的退出代码,而不将其重定向到文件中而不是读取它:
def status = sh(returnStatus: true, script: "set +e; terraform plan -out=plan.out -detailed-exitcode")