我确定我不是唯一对如何处理这样的事情感兴趣的人:Jenkins管道中的docker build阶段由于意外的EOF而失败(有很多原因,在我的情况下,docker守护程序已重新启动在奴隶上)
appImage = docker.build ("${projectName}:${env.BRANCH_NAME}-${gitCommit}", "--build-arg APP_ENV=${appEnv} --build-arg SKIP_LINT=true .")
进入部署阶段,因为意外的EOF实际上并未引发任何错误,因此没有异常可以捕获,因此构建状态为null。
我知道这不是正常情况,但是我们仍然如何处理这样的问题,以免在构建被中断的情况下不进行后续操作。
其他详细信息: @JRichardsz,谢谢你的回答!通常为currentBuild.result。默认为null例如https://issues.jenkins-ci.org/browse/JENKINS-46325,因此,除非您在成功执行阶段时将其显式设置为成功,否则它将为null。但是总的来说,可以通过try catch这样来实现:
if (deployableBranches.contains(env.BRANCH_NAME)) {
try {
stage('Build image') {
ansiColor('xterm') {
appImage = docker.build
("${projectName}:${env.BRANCH_NAME}-${gitCommit}", "--build-arg
SKIP_LINT=true .")
}
}
stage('Push image') {
docker.withRegistry("${registryUrl}", "${dockerCredsId}") {
appImage.push()
appImage.push "${env.BRANCH_NAME}-latest"
}
}
stage('Deploy') {
build job: 'kubernetes-deploy', parameters: [
/////
]
}
} catch (e) {
// A shell step returns with a nonzero exit code
// When pipeline is in a shell step, and a user presses abort
if (e.getMessage().contains('script returned exit code 143')) {
currentBuild.result = "ABORTED"
} else {
currentBuild.result = "FAILED"
}
throw e
} finally {
// Success or failure or abort, always send notifications
stage('Send deployment status') {
helpers.sendDeploymentStatus(projectName, currentBuild.result,
helpers.getCommitHashShort())
}
}
}
但是问题是stage('Build image')可能会退出而没有任何错误代码,就像我的情况一样。
答案 0 :(得分:0)
我有类似的要求:“如果在A阶段执行了某些规则,则随后的阶段一定不能运行”
这对我有用:
def flag;
node {
stage('A') {
flag = 1;
}
stage('B') {
// exit this stage if flag == 1
if(flag == 1){
return;
}
//start of stage B tasks
...
}
}
您还可以使用诸如 currentBuild.result 之类的jenkins变量来代替这样的标记:
node {
stage('A') {
//stage A tasks
//this stage could modify currentBuild.result variable
}
stage('B') {
// exit this stage if currentBuild.result is null , empty, "FAILURE", etc
if(currentBuild.result == null ||
currentBuild.result == "" ||
currentBuild.result=="FAILURE" ){
return;
}
//stage B tasks
}
}