如何将shell命令输出值访问到if-condition中,如果'retval'值为'False',我想停止执行其他阶段
错误
groovy.lang.MissingPropertyException: No such property: $env for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
字符串
代码:
stage('GitHub API Commit Statuses'){
container('terra'){
ansiColor('xterm'){
withCredentials([usernamePassword(credentialsId: 'launchpad-pat', usernameVariable: 'USERNAME', passwordVariable: 'PAT')]){
try{
env.retval = ''
sh '''
env.retval=$(bash terraform-azure-pipelines/scripts/pr_file_changes.sh $USERNAME $PAT $repo_name $pr_number $working_dir)
if [ "$env.retval" = "True" ]
then
echo "success"
echo "retval: $retval"
bash terraform-azure-pipelines/scripts/create-commit-status.sh $USERNAME $PAT $repo_name $branch_name pending $BUILD_URL "The Jenkins pipeline is running" $working_dir
else
echo "retval: $env.retval"
fi
'''
}
catch (exc){
echo exc.toString()
echo "GitHub API commit statuses creation has been failed for the git repo:${repo_name} and it continues to execute next stages"
}
if ( $env.retval == 'False' ){
error('stop')
}
}
}
}
}
型
2条答案
按热度按时间v09wglhw1#
在这个Jenkins Pipeline Environment Variables - The Definitive Guide博客中有一个很好的文章。具体参见“覆盖环境变量”一节。
请原谅我,如果我误解了,但我不认为你需要这个环境变量。重新排列它,以便在try块之前执行
sh "bash terraform-azure-pipelines/scripts/pr_file_changes.sh $USERNAME $PAT $repo_name $pr_number $working_dir"
。在try块中运行sh "bash terraform-azure-pipelines/scripts/create-commit-status.sh $USERNAME $PAT $repo_name $branch_name pending $BUILD_URL "The Jenkins pipeline is running" $working_dir"
。如果
pr_file_changes.sh
失败,退出代码为非0,则管道将停止,并且就像当前逻辑一样,create-commit-status.sh
脚本将不会运行。这里有一个小工作的例子,我的意思
字符串
这里是你的代码块重新排列,但未经测试
型
hpcdzsge2#
使用下面的代码,我得到了预期的输出。
字符串