从Shell阶段中止Jenkins Pipline项目

时间:2019-05-06 07:11:10

标签: jenkins-pipeline

我试图从执行if else shell条件的阶段中止jenkins pipline项目。我想在执行if条件时中止jenkins构建。

下面是阶段代码。

        stage('stage: Abort check'){
        steps{
            script{
                sh '''
                    if [ `ls ${DIR} | wc -l` -ge 8 ] ; then
                        echo "More then 5 card definition applications are running. Delete Few applications"\n
                        echo "ABORTING the JOB"
                        currentBuild.result = 'ABORTED'
                    else
                        echo "Less then 5 card definition applications are running. Excecuting remaining stages"
                    fi;

                '''
            }
        }
    }

我已经使用了声明性命令currentBuild.result = 'ABORTED',但是不能在shell块中使用。

我遇到currentBuild.result: not found错误

任何人都可以指导我了解如何完成这些工作吗?

1 个答案:

答案 0 :(得分:1)

在您的Shell上下文中没有currentBuild,而在您的jenkins管道的上下文中拥有它。

您必须依靠sh命令的输出,并在currentBuild.result = 'ABORTED'之外执行sh '''

steps{
    script{
        def res = 0
        res = sh(script: '''
                if [ `ls ${DIR} | wc -l` -ge 8 ] ; then
                    echo "More then 5 card definition applications are running. Delete Few applications"\n
                    echo "ABORTING the JOB"
                    exit 1
                else
                    echo "Less then 5 card definition applications are running. Excecuting remaining stages"
                    exit 0
                fi;
            '''
                  , returnStatus:true)
        if (res != 0) {
          currentBuild.result = 'ABORTED'
        }
    }
}