Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline: How to archive artifacts when the build fails?

When our browser based tests fail, we take a screenshot of the browser window to better illustrate the problem. However, I don't understand how to archive them in my pipeline, because the pipeline stops after the failure. Same for the junit.xml, I'd also like to use it in error cases.

I've checked, the screenshots are generated and stored correctly.

My definition looks like this (irrelevant things mostly trimmed):

node {
   stage('Build docker container') {
       checkout([$class: 'GitSCM', ...])
       sh "docker build -t webapp ."
   }
   stage('test build') {
       sh "mkdir -p rspec screenshots"
       sh "docker run -v /var/jenkins_home/workspace/webapp/rspec/junit.xml:/myapp/junit.xml -v /var/jenkins_home/workspace/webapp/screenshots:/myapp/tmp/capybara -v webapp bundle exec rspec"
   }
   stage('Results') {
      junit 'rspec/junit*.xml'
      archive 'screenshots/*'
   }
}
like image 810
iGEL Avatar asked Sep 16 '16 09:09

iGEL


Video Answer


2 Answers

You can use simple Java try/catch to avoid pipeline failure on test failure, or Jenkins catchError like this :

node {
    catchError {
        // Tests that might fail...
    }
    // Archive your tests artifacts
}
like image 144
Pom12 Avatar answered Sep 24 '22 09:09

Pom12


From here, you can use the post section in your pipeline:

pipeline {
    agent any
    stages {
        stage('Build') {
            ...
        }
        stage('Test') {
            ...
        }
    }

    post {
        always {
            archive 'build/libs/**/*.jar'
        }
    }
}
like image 29
David Norman Avatar answered Sep 24 '22 09:09

David Norman