Search code examples
jenkinsjenkins-pipelinejenkins-blueocean

Jenkinsfile: publish test results even if test step fails


In my Jenkinsfile I have a stage Test where I run a npm test command step as well as a junit step to archive test results.

stage('Test') {
  steps {
    sh 'npm run test-ci'
    junit 'test-results.xml'
  }
}

How can I use try/finally correctly to run the junit step even if the sh 'npm run test-ci' step fails?


Solution

  • You want to use the post stage, https://jenkins.io/doc/book/pipeline/syntax/#post.

    pipeline {
        agent any
        stages {
          stage('Test') {
            steps {
              sh 'npm run test-ci'
            }
          }
          post { 
            always { 
              junit 'test-results.xml'   
            }
        }
    }
    

    Also have a look at this blog post, it explains it further, https://jenkins.io/blog/2017/02/10/declarative-html-publisher/