Jenkins pipeline is skipping groovy 'else if' clause

24,263

If these files do not exist then sh jenkins step returns error code 2. You should rewrite your 'if condition' like that:

success_exists == 2 && error_exists == 2

But, I think that in your case this code is more suitable:

stage('system tests') {
steps {
    dir(project_root) {
        def error_exists = sh(
            script: 'ls error.txt', returnStatus: true
        )
        def success_exists = sh(
            script: 'ls success.txt', returnStatus: true
        )
        if (error_exists == 0) {
            currentBuild.result = 'FAILED'
            return
        } else if (success_exists != 0 && error_exists != 0) {
            currentBuild.result = 'FAILED'
            return
        }
        build job: 'my-job;
    }
}

Because there may be other reasons for not being able to find the file (lack of access, etc).

Share:
24,263
ARL
Author by

ARL

Updated on December 05, 2020

Comments

  • ARL
    ARL over 3 years

    I am conducting some tests in my pipeline. My aim is that if an error file exists the build should fail. But if for some reason the tests experienced an exception and didn't write either an error or successful file, the pipeline should also fail. If neither of the conditions for failure are met, I would like the an upstream job to execute.

    I wrote it in stage and initially it looked like this:

    stage('system tests') {
        steps {
            dir(project_root) {
                def error_exists = sh(
                    script: 'ls error.txt', returnStatus: true
                )
                if (error_exists == 0) {
                    currentBuild.result = 'FAILED'
                    return
                }
                build job: 'my-job;
            }
        }
    }
    

    The above code works. When the tests being executed wrote an error file, the pipeline fails. I then tried to modify the code to cater for the outcome where neither error or success files are written.

    stage('system tests') {
        steps {
            dir(project_root) {
                def error_exists = sh(
                    script: 'ls error.txt', returnStatus: true
                )
                def success_exists = sh(
                    script: 'ls success.txt', returnStatus: true
                )
                if (error_exists == 0) {
                    currentBuild.result = 'FAILED'
                    return
                } else if (success_exists == 1 && error_exists == 1) {
                    currentBuild.result = 'FAILED'
                    return
                }
                build job: 'my-job;
            }
        }
    }
    

    I simulated a situation where neither file was written and the pipeline didn't fail, and instead it triggered the upstream build. Why am I not entering the else if clause` if the result of both shell scripts is false? I took the logical operators from here and I think they should be met (The code below is output from the shell scripts in the new-job pipeline)

    [new-job] Running shell script
    + ls error.txt
    ls: cannot access error.txt: no such file or directory
    
    [new-job] Running shell script
    + ls success.txt
    ls: cannot access success.txt: no such file or directory