如何将shell命令输出定义为可从其他阶段全局访问Jenkins管道

5anewei6  于 2023-08-03  发布在  Jenkins
关注(0)|答案(2)|浏览(150)

如何将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')
              }
              
            }
          }
        }
      }

v09wglhw

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脚本将不会运行。
这里有一个小工作的例子,我的意思

node {
    stage('example'){
        sh "echo 'Call pr_file_changes.sh' "
        try {
            sh "echo 'Call create-commit-status.sh' "
        } catch (exc) {
            echo 'GitHub API commit statuses creation has been failed for the git .....'
        }
    }
    
    stage('Next'){
        echo "This was next"
    }
}

字符串
这里是你的代码块重新排列,但未经测试

stage('GitHub API Commit Statuses'){
        container('terra'){
          ansiColor('xterm'){
            withCredentials([usernamePassword(credentialsId: 'launchpad-pat', usernameVariable: 'USERNAME', passwordVariable: 'PAT')]){
              // Run pr_file_changes.sh and stop everything if it fails
              sh "bash terraform-azure-pipelines/scripts/pr_file_changes.sh $USERNAME $PAT $repo_name $pr_number $working_dir"
              try{
                // Run create-commit-status.sh if pr_file_changes.sh was successful
                // but I want to carry on if this fails so I put it
                // in a try/catch block
                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 "
              }
              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"
              }
            }
          }
        }
      }

hpcdzsge

hpcdzsge2#

使用下面的代码,我得到了预期的输出。

stage('GitHub API Commit Statuses'){
        container('terra'){
          ansiColor('xterm'){
            withCredentials([usernamePassword(credentialsId: '****', usernameVariable: 'USERNAME', passwordVariable: 'PAT')]){
              def retval = ''
              try{
                 retval = sh(script: '''
                  a=$(bash terraform-azure-pipelines/scripts/pr_file_changes.sh $USERNAME $PAT $repo_name $pr_number $working_dir )
                  echo "\$a"
                ''', returnStdout: true)
                
                echo "ret:$retval"
                sh """
                  
                  if [ "$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: $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 ( retval.contains('False') ){
                  error('stop')
              }
              
            }
          }
        }
      }

字符串

相关问题