Jenkins Pipeline:如何在构建失败时归档工件?

时间:2016-09-16 09:34:57

标签: jenkins jenkins-pipeline

当我们基于浏览器的测试失败时,我们会截取浏览器窗口的屏幕截图以更好地说明问题。但是,我不明白如何将它们存档在我的管道中,因为管道在失败后停止。同样适用于junit.xml,我也想在错误情况下使用它。

我已经检查过,屏幕截图是生成并正确存储的。

我的定义看起来像这样(无关紧要的事情大部分被裁掉):

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/*'
   }
}

2 个答案:

答案 0 :(得分:3)

here,您可以使用管道中的帖子部分:

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

    post {
        always {
            archive 'build/libs/**/*.jar'
        }
    }
}

答案 1 :(得分:2)

您可以使用简单的Java try/catch来避免测试失败时出现管道故障,或者像这样使用Jenkins catchError

node {
    catchError {
        // Tests that might fail...
    }
    // Archive your tests artifacts
}
相关问题