简体   繁体   中英

JENKINS PIPELINE TO ABORT BASED ON THE OUTPUT OF EARLIER STAGE

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

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 (ie has zero exit status). A non-zero exit status means failure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM