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



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

在这种情况下,作业失败。詹金斯提出了以下异常: '''hudson.plugins.git.GitException: 命令 "git fetch --tags --progress --prune -- origin +refs/tags/...:refs/remotes/origin/..." 返回状态代码 128: 标准输出: 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

最新更新