虽然测试是良好的持续交付管道的关键部分,但大多数人不但愿筛选数千行控制台输出来查找有关失败测试的信息,为了使这更容易,Jenkins能够记录和汇总测试结果,只要你的测试运行器能够输出测试结果文件。Jenkins一般与junit
步骤捆绑在一块儿,但若是你的测试运行器没法输出JUnit样式的XML报告,则还有其余插件能够处理几乎任何普遍使用的测试报告格式。node
要收集咱们的测试结果和工件,咱们将使用post
部分。segmentfault
Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Test') { steps { sh './gradlew check' } } } post { always { junit 'build/reports/**/*.xml' } } }
脚本管道(高级):post
Jenkinsfile (Scripted Pipeline) node { try { stage('Test') { sh './gradlew check' } } finally { junit 'build/reports/**/*.xml' } }
这将始终抓取测试结果,让Jenkins跟踪它们,计算趋势并报告它们,具备失败测试的管道将标记为“不稳定”,在Web UI中用黄色表示,这与“失败”状态不一样,用红色表示。测试
当存在测试失败时,从Jenkins获取构建的工件以进行本地分析和调查一般颇有用,Jenkins对存储“工件”的内置支持使这变得切实可行,在执行管道期间生成的文件。gradle
这能够经过archiveArtifacts
步骤和文件通配符表达式轻松完成,以下例所示:ui
enkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Build') { steps { sh './gradlew build' } } stage('Test') { steps { sh './gradlew check' } } } post { always { archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true junit 'build/reports/**/*.xml' } } }
脚本管道(高级):插件
Jenkinsfile (Scripted Pipeline) node { try { stage('Test') { sh './gradlew check' } } finally { archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true junit 'build/reports/**/*.xml' } }
若是在archiveArtifacts
步骤中指定了多个参数,则必须在步骤代码中明确指定每一个参数的名称 — 即artifacts
用于工件的路径和文件名和fingerprint
来选择此选项,若是你只须要指定工件的路径和文件名,那么你能够省略参数名称artifacts
— 例如archiveArtifacts 'build/libs/**/*.jar'
。code
在Jenkins中记录测试和工件对于快速轻松地向团队的各个成员呈现信息很是有用,在下一节中,咱们将讨论如何告诉团队成员咱们的管道中发生了什么。xml