jenkins报告插件_使用复制工件jenkins插件汇总的魅力报告

jenkins报告插件

A couple of weeks ago I faced a problem, on which I spend more time than I thought I should, so I decided to write this article to help you do the same thing easier and faster.

几周前,我遇到了一个问题,我花了更多的时间在原本应有的时间上,所以我决定写这篇文章,以帮助您更轻松,更快地完成相同的事情。

手头的任务 (Task at hand)

Let’s assume that we have two different Jenkins jobs: one for backend tests and other for UI tests. And we have one job which start these jobs in parallel with different parameters (e.g. UI tests run for several browsers). It’s possible to create Allure report for each downstream job, but we’re too lazy to open different reports each time regression tests are finished. So, we want to have one report in our upstream job which contains all test results for current run.

假设我们有两个不同的Jenkins作业:一个用于后端测试,另一个用于UI测试。 而且,我们有一项工作可以同时使用不同的参数来启动这些工作(例如,针对多个浏览器运行的UI测试)。 可以为每个下游工作创建Allure报告,但是每次执行回归测试时我们都懒得打开不同的报告。 因此,我们希望在上游作业中有一份报告,其中包含当前运行的所有测试结果。

“Sounds very easy” I thought, but it turned out to be a bit tricky, especially for a person with poor Jenkins knowledge (yes, that’s me). So, let’s move to the solution.

我想“听起来很容易”,但事实证明这有点棘手,特别是对于詹金斯知识不足的人(是的,就是我)。 因此,让我们转到解决方案。

I will use Jenkins Declarative Pipeline syntax in my examples.

我将在示例中使用Jenkins声明性管道语法。

(Solution)

To solve this problem, let’s decompose it into smaller ones.

为了解决这个问题,让我们将其分解为较小的对象。

In order to copy allure results from downstream job we need somehow to archive them there. So the first problem will be:

为了复制下游作业的吸引力结果,我们需要以某种方式将其存档。 因此,第一个问题将是:

1.如何将魅力结果存档在下游工作中? (1. How to archive allure results in downstream job?)

This is very easy. You just need to add a post action with archiveArtifacts to each downstream job. To archive only allure results specify relative path to them using artifacts parameter. With pipeline syntax it looks similar to this:

这很容易。 您只需archiveArtifacts每个下游作业添加一个带有archiveArtifacts的后期操作。 要仅归档诱惑力结果,请使用artifacts参数指定相对结果的路径。 使用管道语法,它看起来类似于:

pipeline {    ...    post {
always {
archiveArtifacts artifacts: 'allure-results/*'
}
}
}

2.如何将存档的工件复制到上游作业? (2. How to copy archived artifacts to upstream job?)

This is the job for Jenkins Copy Artifact Plugin. You can find more information about the plugin here. I will just describe how to use it in this particular case. To do this you need to add post action for each stage where you run tests job with copyArtifacts plugin and you need to specify following parameters:

这是Jenkins Copy Artifact Plugin的工作。 您可以在此处找到有关该插件的更多信息。 我将仅描述在这种特殊情况下如何使用它。 为此,您需要为每个使用copyArtifacts插件运行测试作业的阶段添加后期操作,并且需要指定以下参数:

filter relative paths to artifacts you want to copy, in our case it is 'allure-results/*' ;

filter要复制的工件的相对路径,在本例中为'allure-results/*'

projectName name of the job we want to copy artifacts from, in our case the name of downstream job;

projectName我们要从其复制工件的作业的名称,在本例中为下游作业的名称;

selector parameter that helps to select the build to copy artifacts from, there are a plenty of options, but we will use lastCompleted since our tests tend to fail sometimes.

selector参数可以帮助您选择要复制工件的内部版本,有很多选项,但由于测试有时会失败,因此我们将使用lastCompleted

The full step script in declarative pipeline looks like:

声明性管道中的完整步骤脚本如下所示:

pipeline {    ...    stages{        ...        stage('run tests') {
steps{
script{
build job: 'tests'
}
}
post {
always {
copyArtifacts(
filter: "allure-results/*",
projectName: 'tests',
selector: lastCompleted()
)
}
}
}
...
}
...}

3.如何为具有不同参数的一项作业选择正确的版本? (3. How to select correct build of one job with different parameters?)

As you might remember we also want to run our UI tests in different browsers in parallel and with the current set up there might be a problem with reports.

您可能还记得,我们还希望在不同的浏览器中并行运行我们的UI测试,而在当前设置下,报告可能存在问题。

Let’s imagine or upstream job starts two builds of UI tests job in parallel with parameters chrome and firefox as browsers where we want to run tests. And if build for chrome is the first in your pipeline, but build for firefox is finished earlier, then Copy Artifact plugin copies test results from the firefox build twice because it is lastCompleted build in both cases.

让我们想象一下,或上游作业启动了两个版本的UI测试作业,并与chromefirefox参数并行运行,作为我们要在其中运行测试的浏览器。 如果针对chrome构建是您管道中的第一个构建,但是较早完成了针对firefox构建,则Copy Artifact插件会从firefox生成中复制测试结果两次,因为这两种情况都是lastCompleted构建。

It was actually the problem on which I spent most of the time. I tried to save build numbers to variables and a bunch of other staff, but solution it very simple.

实际上,这是我大部分时间都花在的问题上。 我试图将内部版本号保存到变量和其他人员中,但是解决起来非常简单。

You just need to add parameters to copyArtifacts() step. This allows you to filter builds by build parameters, e.g. to select artifacts from build with parameter browser=chrome your pipeline script should look like:

您只需要向copyArtifacts()步骤添加parameters 。 这允许您按构建参数过滤构建,例如,使用参数browser=chrome从构建中选择工件,管道脚本应如下所示:

...post {
always {
copyArtifacts(
filter: "allure-results/*",
projectName: 'ui-tests',
parameters: 'browser=chrome',
selector: lastCompleted()
)
}
}...

齐心协力 (Pulling all together)

Here is the whole upstream pipeline for our needs:

这是满足我们需求的整个上游管道:

pipeline {
agent { ... }
stages{ ...stage('run tests') {
parallel{
stage("run api tests") {
steps{
script{
build job: 'api-tests'
}
}
post {
always {
copyArtifacts(
filter: "allure-results/*",
projectName: 'api-tests',
selector: lastCompleted()
)
}
}
}
stage("run ui tests in chrome") {
steps{
script{
build job: 'ui-tests',
parameters: [
[$class: 'StringParameterValue', name: 'browser', value: "chrome"]
]
}
}
post {
always {
copyArtifacts(
filter: "allure-results/*",
projectName: 'ui-tests',
parameters: 'browser=chrome',
selector: lastCompleted()
)
}
}
}
stage("run ui tests in firefox") {
steps{
script{
build job: 'ui-tests',
parameters: [
[$class: 'StringParameterValue', name: 'browser', value: "firefox"]
]
}
}
post {
always {
copyArtifacts(
filter: "allure-results/*",
projectName: 'ui-tests',
parameters: 'browser=firefox',
selector: lastCompleted()
)
}
}
}
}
}
}
post('create allure report'){
always{
script {
allure([
includeProperties: false,
jdk: '',
properties: [],
reportBuildPolicy: 'ALWAYS',
results: [[path: 'target/allure-results']]
])
}
}
}
}

And don’t forget to archive artifacts in downstream jobs:

并且不要忘记将工件保存在下游作业中:

pipeline {...post {
always {
archiveArtifacts artifacts: 'allure-results/*'
}
}
}

结论 (Conclusion)

The main difficulties I faced while looking for solution were not entirely clear documentation for Jenkins and Jenkins plugins and a lack of useful examples. So, I spent quite a lot of time on a task that can be easily done for 15 minutes. I will be very happy, if this information is helpful for someone who’s looking for similar solution.

在寻找解决方案时,我遇到的主要困难不是Jenkins和Jenkins插件的文档非常清晰,而且缺少有用的示例。 因此,我花了很多时间完成一项可以轻松完成15分钟的任务。 如果这些信息对正在寻找类似解决方案的人有所帮助,我将非常高兴。

翻译自: https://medium.com/@andrewbayd/aggregated-allure-report-using-copy-artifact-jenkins-plugin-843068e216a7

jenkins报告插件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值