中止的詹金斯管道作业将继续运行到后期

时间:2019-01-21 16:48:09

标签: jenkins .net-core jenkins-pipeline

我正在使用最新版本的Jenkins和管道插件,并且我有一个配置如下的jenkins声明性管道作业:

pipeline { 
    agent {
        label 'default'
    }
    stages {
        stage('Prepare') {
            steps {
                dir('foo') {
                    git ...
                }
            }
        }
        stage('Temp1') {
            steps {
                sh script: 'dotnet run ...'
                echo 'Temp1'
            }
        }
        stage('Temp2') {
            steps {
                echo 'Temp2'
            }
        }
    }
}

如果我在Temp1阶段的sh脚本中中止构建,我的期望是Temp1阶段的其余部分和Temp2阶段都不会运行。但是,当我中止它时,它将停止sh脚本,然后运行剩余的Temp1阶段,并继续运行Temp2阶段!

这是日志:

Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on ...
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] dir
Running in ...
[Pipeline] {
[Pipeline] git
Fetching changes from the remote Git repository
...
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Temp1)
[Pipeline] sh
+ dotnet run ...
Aborted by ...
Sending interrupt signal to process
[Pipeline] echo
Temp1
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Temp2)
[Pipeline] echo
Temp2
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED

如何使构建实际中止?

2 个答案:

答案 0 :(得分:0)

您可以尝试2种不同的方法。

stage('Foo'){
    //do something
} 
post {
     failure { currentBuild.result = 'ABORTED' // this should cause the pipeline to stop 
     //or the hard way: failure{ throw new hudson.AbortException('Abort the build.') 
}

答案 1 :(得分:0)

这最终是一个dotnet问题,当被SIGTERM杀死时,它返回0作为退出代码。 https://github.com/dotnet/coreclr/issues/21243几天前已解决,但尚未发布。

作为解决方法,您可以执行以下操作:

public static int Main(string[] args)
{
    Environment.ExitCode = 1;

    // Do stuff

    return 0;
}

如果Main成功完成,它将返回退出代码0。如果收到SIGTERM,它将返回Environment.ExitCode中的值。