Issue
It's straightforward to set hardcoded values in the plugin's Advanced > Presentation > Classifications section, but a value like ${FOO}
is displayed literally as ${FOO} instead of expanding.
I set FOO like so in the build shell script, but it does not get displayed in the report.
export FOO=hello
I then tried creating my own .properties file:
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'
In Console Output below, I can confirm the Cucumber report plugin is finding the .properties file:
[CucumberReport] Copied 2 properties files from workspace
Based on the find
output above, the two files must be
- ./sonar-project.properties, and
- ./report-vars.properties
Since those are the only .properties files present.
I have token-macro:2.6
installed.
Solution
We can specify custom classifications by using a pipeline. Create a new Jenkins Pipeline (New Item > Pipeline) and convert your existing job.
A general pipeline may take the following form
@NonCPS
def readLines = { content ->
def keyValue = []
content.eachLine { line ->
keyValue << line
}
return keyValue
}
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 contents = readFile("${prop_dir}/dynamic.properties")
def keyValues = readLines(contents)
def dynamicVar = keyValues[0].split("=")[1]
cucumber buildStatus: 'UNSTABLE',
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': "${dynamicVar}"
]
]
}
}
New to pipelines? Click "Pipeline syntax" link in the left sidebar of a finished job. There are plenty of examples in the Snippet Generator.
Implementation Notes
- Write all necessary variables to a .properties file in the build stage.
- Build up the path to the .properties file referencing various Jenkins env vars.
- Specify dynamic classification values with double-quoted interpolation.
- Groovy Sandbox may or may not need to be disabled
Other questions
- Why can't we use a callback file? Jenkins checksum prevents links to modified files, even if you modify the checksum to the new value in build.xml.
- Why can't we use only groovy-postbuild plugin? The pipeline syntax for Cucumber Reports is specific to Jenkins Pipeline. You don't get that with just the plugin. And, you need to create a new job as a Pipeline. So you would still need to use the Cucumber Reports sample pipeline syntax anyway.
References
- Pipeline syntax for Cucumber Reports https://plugins.jenkins.io/cucumber-reports/
- Reading lines from file https://stackoverflow.com/a/58709471/16681513
- Reading from a properties file (didn't work in this case) https://stackoverflow.com/a/33937828/16681513
Answered By - turtleyacht
Answer Checked By - Willingham (JavaFixing Volunteer)