如何在Jenkins Cucumber报告中指定.properties文件中的分类



在插件的Advanced中设置硬编码值很简单>演示文稿>Classifications部分,但像${FOO}这样的值实际上显示为${FOO},而不是展开。

我在构建shell脚本中这样设置FOO,但它不会显示在报告中。

export FOO=hello

然后我尝试创建自己的.properties文件:

echo buildVersion=$LAST_BUILD_VERSION >> report-vars.properties
echo greeting=hola >> report-vars.properties
echo classifications.message=hello >> report-vars.properties

cat report-vars.properties
find . -name '*.properties'

在下面的控制台输出中,我可以确认Cucumber报告插件正在找到.properties文件:

[CucumberReport] Copied 2 properties files from workspace

根据上面的find输出,这两个文件必须是

  • /sonar-project.properties,以及
  • /report-vars.properties

因为这些是唯一存在的.properties文件。

我安装了token-macro:2.6

使用;建设其他项目"在作业的"后期构建操作"部分中的步骤;处理作业"我们称之为"添加分类"。

在执行shell部分,它将执行以下操作:

# Get the first line of its own log
# Parse for the calling job name
# Parse for the calling job's build number
# Redefine the job name with ${UPSTREAM_JOB_NAME#*0m} [1]
# Repeat for the build number
# `cd` to the calling job's directory
# cd to cucumber-html-reports/
# Use `ex` to insert the contents of your "callback file." [2][3]

[1] 这是跳过Jenkins的ha:///链接引用和颜色代码,它们是隐藏的(echo $VAR | cat -v(和繁重的。

[3] 回调文件什么详细信息请继续阅读。

[2]ex是这样的:

ex - overview-features.html << EOF
/id="classifications"/;+ r ../add-fields.phtml
wq!
EOF

参考文献[3];回调文件"使用回调文件的每个作业都应该有一个统一的名称(add-fields.phtml(,以使"添加分类"作业尽可能通用。

调用作业的Execute shell部分的末尾有这样的内容:

DYNAMIC_CLASSIFICATION=$(curl...)
... # Invoking a build or tests, etc.
cat << EOF > ${JOB_DIR}/add-fields.phtml
<tr class="info">
<th>Build (or SHA or changing field)</th>
<td>${DYNAMIC_CLASSIFICATION}</td>
</tr>
EOF

ex的一点HTML魔法——呃,stitchery——会有很大的帮助。

作业需要在主节点上运行才能访问$JENKINS_HOME。

未决问题

  • .../$BUILD_NUMBER/cucumber-html-reports/overview-features.html的Cucumber链接是作为空页面下载的,而不是加载网页
  • 应该只在主节点上运行作业,除非";构建其他项目"使用相同的$JENKINS_HOME访问权限将作业同步到与调用作业相同的从属节点

我们可以使用管道指定自定义分类。创建一个新的Jenkins管道(新项目>管道(并转换现有作业。

一般管道可以采用以下形式

node {
stage('checkout') {
git ...
}
stage('build') {
withCredentials(...) {
sh '''
...
cat << EOF > dynamic.properties
dynamicVar=$(curl ...)
EOF
'''
}
}
stage('post-build') {
def jenkins_home = manager.getEnvVariable('JENKINS_HOME')
...
def prop_dir = ...
def report_file = "${prop_dir}/dynamic.properties"
def contents = readFile("${report_file}").split('n')
def dynamicVal = contents[0].split("=")[1]
cucumber fileIncludePattern: '**/*.json',
fileExcludePattern: '',
jsonReportDirectory: '',
failedStepsNumber: '0',
skippedStepsNumber: '0',
pendingStepsNumber: '0',
undefinedStepsNumber: '0',
failedScenariosNumber: '0',
failedFeaturesNumber: '0',
sortingMethod: 'ALPHABETICAL',
trendsLimit: 10,
classifications: [
[
'key': 'One such dynamic var',
'value': "${dynamicVal}"
]
]
}
}

管道新手?单击";管道语法";已完成作业左侧边栏中的链接。代码段生成器中有很多示例。

实施说明

  • 将所有必要的变量写入构建阶段
  • 建立指向.properties文件的路径,该文件引用各种詹金斯环境变量
  • 使用双引号指定动态分类值插值
  • Groovy沙盒可能需要禁用,也可能不需要禁用

其他问题

  • 为什么我们不能使用回调文件?Jenkins校验和阻止链接到修改过的文件,即使您将校验和修改为build.xml中的新值
  • 为什么我们不能只使用groovy postbuild插件?Cucumber Reports的管道语法是Jenkins特有的管道仅凭插件是无法做到这一点的。而且,你需要创建一个新作业作为管道。所以你仍然需要无论如何都要使用Cucumber Reports示例管道语法

参考文献

  • Cucumber报表的管道语法https://plugins.jenkins.io/cucumber-reports/
  • 正在从文件中读取行https://stackoverflow.com/a/58709471/16681513
  • 从属性文件读取(在这种情况下不起作用(https://stackoverflow.com/a/33937828/16681513

最新更新