0

Using Jenkins now, this is my post build Pipeline:

pipeline {
agent any
stages {
    stage('Stage 1') {
        steps {
            echo 'Hello this is stage 1'
        }

    }
    stage('Stage 2') {
        steps {
            echo 'Still here? Well this is stage 2'
        }

    }
    stage('Stage 3') {
        steps {
            echo 'Almost there ... stage 3'
        }

    }
    stage('Stage 4 - Python') {
        steps {
            bat 'py atom_python_test.py'
        }

    }
    stage('Stage 5 - Compile C++') {
        steps {
            catchError {
                bat '''
                cd C:\Program Files (x86)//Microsoft Visual Studio 12.0//VC
                call vcvarsall
                cd C://Users//praktikant3//Documents//Visual Studio 2013//Projects//FirstProjectBuiltOnJenkins//FirstProjectBuiltOnJenkins
                dir
                MSBuild.exe
                echo 'Compile Stage successful'
                '''
            }
        }
    }

    stage('Stage 6 - Build C++') {
        steps {
            bat '''
            cd C://Program Files (x86)//Microsoft Visual Studio 12.0//VC
            call vcvarsall
            cd C://Users//praktikant3//Documents//Visual Studio 2013//Projects//FirstProjectBuiltOnJenkins//FirstProjectBuiltOnJenkins
            dir
            MSBuild.exe
            '''
        }

    }

}

post {
    success {
        mail to: 'SomeEMAIL@company.com',
        subject: "Succeeding Pipeline: ${currentBuild.fullDisplayName}",
        body: "All is good with ${env.BUILD_URL}"
    }
    failure {
        mail to: 'SomeEMAIL@company.com',
        subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
        body: "Something is wrong with ${env.BUILD_URL}"
    }
}

}

The build C++ part works because I the syntax is correct, however in previous attempt I had the directory change using "\" which was not working. I added a "Compile stage" using catchError and one of the cd with a "\" to see if I get notification of failure. From my understanding:

"You should wrap every step that can potentially fail into a catchError function. What this does is:

  • If an error occurs...
  • ... set build.result to FAILURE...
  • ... andcontinue the build "

but when I intentionally left the back slash the pipeline failed and stopped and the post block did not execute.

Hadi Farah
  • 1,091
  • 2
  • 9
  • 27
  • 1
    This seems to be a duplicate of https://stackoverflow.com/questions/43318297/post-failure-jenkinsfile-is-not-working – Geoff Mar 13 '18 at 09:14
  • Thank you this didn't pop-up in my search – Hadi Farah Mar 13 '18 at 09:18
  • @Geoff , thanks but it did not work. – Hadi Farah Mar 13 '18 at 09:56
  • Can you post a second snippet including your use of catchError. Thanks. – Geoff Mar 13 '18 at 10:23
  • I posted the entire code, it's suppose to be training to get used to pipeline. I can't seem to wrap my head around it. I also tried using try {} and catch {}. @Geoff – Hadi Farah Mar 13 '18 at 10:38
  • the intent is now, bypass the failure report and execute post {} blocks after steps at any outcome. I don't want to have to write individual email notifications for every step. >Note when the code inside catchError is correct the pipeline is a success and I receive notification of it. – Hadi Farah Mar 13 '18 at 10:39
  • it looks like your post section is outside of the pipeline. Take it inside and see if that works. – Geoff Mar 13 '18 at 12:14
  • @Geoff I worked it out. Post is inside the pipeline, I just messed up the copy paste inside stack overflow. (note that there is a closing bracket under the code snippet that did not make it inside which should end pipeline). – Hadi Farah Mar 13 '18 at 12:44
  • So what I did first is that instead of "\" I deleted everything inside catchError and called error() function in pipeline. catchError did its job and registered a failure and I got an email notification. The problem seems to be when your error is Groovy based then the code runs and you get a throw which triggers post failure. However in my first attempt the problem was the batch command syntax was incorrect and that is inputted as a string. This error is not recognized by catchError and the whole pipeline fails and does not run without receiving any error. – Hadi Farah Mar 13 '18 at 12:48

0 Answers0