Sumith08
Sumith08

Reputation: 770

Aborting jenkins pipline project from shell stage

I am trying to abort the jenkins pipline project from the stage which executes the if else shell condition. I want to abort the jenkins build when the if condition is executed.

below is the stage code.

        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;

                '''
            }
        }
    }

I have used the declarative command currentBuild.result = 'ABORTED' but this cant be used in the shell block.

I am getting currentBuild.result: not found error

Can anyone please guide me to know how these can be done?

Upvotes: 0

Views: 900

Answers (1)

hakamairi
hakamairi

Reputation: 4678

In the context of your shell there's no currentBuild, you have it in the context of your jenkins pipeline.

You have to rely on output of your sh commands and do currentBuild.result = 'ABORTED' outside 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'
        }
    }
}

Upvotes: 1

Related Questions