繁体   English   中英

如果 SCM 不可访问,则在本地信息上运行 Jenkins 管道

[英]Run Jenkins pipeline on local information if SCM is not accessible

我开发了一个由 SCM 的管道脚本定义的管道作业。 我使用 Git 作为单片机。 大多数情况下,Git 服务器是可以访问的,没有问题,但有时 git 服务器由于网络问题等原因不可用。

在这种情况下,作业失败。 Jenkins 引发以下异常:'''hudson.plugins.git.GitException:命令“git fetch --tags --progress --prune -- origin +refs/tags/...:refs/remotes/origin/.. ." 返回状态码 128:stdout:stderr:致命:无法访问 '...':无法连接到代理端口 8080:连接超时'''

在这种情况下,我想管理这个异常,因为在之前的作业执行期间,我已经从 SCM 获取了管道脚本。 在我看来,我们可以使用前面已经从 git 服务器下载的代码。

管理这种异常以利用本地信息的方法吗?

下面是我的管道脚本

pipeline { 
  agent { node 'master' } 

  options { 
    disableConcurrentBuilds() 
    buildDiscarder(logRotator(daysToKeepStr: '60')) 
    skipDefaultCheckout() 
  } 
  
  stages { 
    stage("Checkout ft4cs-engine for subsystem report generation") { 
      steps { 
        timestamps { 
          script { 
            echo "Checkout repository" 
            dir('repository') { 
              try { 
                checkout([
                  $class: 'GitSCM', 
                  branches: [[name: "${REPO_BRANCH}"]], 
                  doGenerateSubmoduleConfigurations: false, 
                  extensions: [
                    [$class: 'LocalBranch', localBranch: '**'], 
                    [$class: 'CleanBeforeCheckout']
                  ], 
                  submoduleCfg: [], 
                  userRemoteConfigs: [[credentialsId: 'gitrepo', url: '<url gitlab>']]
                ]) 
              } 
              catch (Exception e) { 
                echo "Catching exception during the checkout step : $e"; 
              } 
            } // end of dir
          }   // end of script
        } // end of timestamps
      } // end of steps 
    } // end of stage
  ... 
  } // end of stages
} // end of pipeline

此致

我认为您尝试如下:

  1. 在管道请求时定义一个 Groovy 变量runCheckOut=true
  2. 为结帐阶段添加条件
  3. catch更改runCheckOut=false
  4. catch中加载 Jenkinsfile

def runCheckout=true

pipeline { 
  agent { node 'master' } 

  options { 
    timestamps()
    disableConcurrentBuilds() 
    buildDiscarder(logRotator(daysToKeepStr: '60')) 
    skipDefaultCheckout() 
  } 
  
  stages { 
    stage("Checkout") { 
      when {
        expression {runCheckout}
      }
      steps { 
        script { 
          echo "Checkout repository" 
          dir('repository') { 
            try { 
              checkout([
                $class: 'GitSCM', 
                branches: [[name: "${REPO_BRANCH}"]], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [
                  [$class: 'LocalBranch', localBranch: '**'], 
                  [$class: 'CleanBeforeCheckout']
                ], 
                submoduleCfg: [], 
                userRemoteConfigs: [[credentialsId: 'gitrepo', url: '<url gitlab>']]
              ]) 
            } 
            catch (Exception e) { 
              echo "Catching exception during the checkout step : $e"; 
              sh 'cp <path to your Jenkinsfile>  <path to your Jenkinsfile>.groovy'
              sh 'sed ...' // change runCheckout=true  to runCheckout=false
              load '<path to your Jenkinsfile>.groovy' // load new Jenkinsfile which will run all stages
            } 
          } // end of dir
        }   // end of script
      } // end of steps 
    } // end of stage
  ... 
  } // end of stages
} // end of pipeline

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM