Home > front end >  Jenkins pipeline to abort based on the output of ealier stage
Jenkins pipeline to abort based on the output of ealier stage

Time:07-25

Am comparing two files if i get the out put as "Yes" jenkins pipeline should continue if i get output as "No" jenkins pipeline should return error

the command i have used to compare 2 files is mentioned below. I have placed this command in test.sh file in /jenkins location

test "$(comm -23 <(sort -u /jenkins/OUTPUT1.txt) <(sort -u /jenkins/OUTPUT2.txt) | wc -l)" = "$(cat /jenkins/OUTPUT2.txt | wc -l)" && echo Yes || echo No

The jenkins pipeline i used is

pipeline {
    agent any
    environment {
          result = sh(script: 'bash /jenkins/test.sh', returnStdout: true)
        }
    stages {
      stage('command') {
        steps {
          script {
                    if (result == '${env.result}') {
                        result = Yes
                        echo "conditions are met"
                    } else if (result == '${env.result}') {
                        result = No
                        error('Conditions are not met - build aborted')
                    }
                }
            }
        }

    }
}```

Please suggest me a correct pipeline script for this condition

CodePudding user response:

Just create a script that properly exits with nonzero exit status in case of failure:

#!/bin/bash
var=$(comm -23 <(sort -u /jenkins/OUTPUT1.txt) <(sort -u /jenkins/OUTPUT2.txt) | wc -l)
shouldbe=$(wc -l < /jenkins/OUTPUT2.txt)
if [[ "$var" != "$shouldbe" ]]; the
      echo "Och noooo!"
      exit 1
else 
      echo "Yes!"
fi

Then just call the script:

script {
    sh 'bash ./yourscript.sh'
 }

Jenkins checks if each script is successful (i.e. has zero exit status). A non-zero exit status means failure.

  • Related