Docker 在 Jenkins 管道中构建了意想不到的 EOF



我相信我不是唯一一个对如何处理这样的事情感兴趣的人:Jenkins 管道中的 docker 构建阶段因意外 EOF 而失败(可能有很多原因,就我而言,docker 守护进程在从服务器上重新启动(

appImage = docker.build ("${projectName}:${env.BRANCH_NAME}-${gitCommit}", "--build-arg APP_ENV=${appEnv} --build-arg SKIP_LINT=true .")

部署阶段开始,因为意外 EOF 实际上不会引发任何错误,因此没有要捕获的异常,因此生成状态为 null。
我知道这不是常规情况,但我们仍然可以如何处理这样的 smth,以便在构建中断时不会运行以下阶段。

其他详细信息: @JRichardsz,谢谢你的回答!通常 当前构建.结果 .默认为 null,例如 https://issues.jenkins-ci.org/browse/JENKINS-46325 因此,除非您在成功执行阶段时明确将其设置为成功,否则它将为 null。但总而言之,同样可以通过尝试捕获来实现,例如:

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("构建映像"(可能会退出而没有任何错误代码,就像我的情况一样。

我有一个类似的要求:"如果在阶段 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
...
}
}

你也可以使用一些jenkins变量,如currentBuild.result,而不是像这样的标志:

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
}
}

最新更新