How to concatenate Terminal filter with Junit filter - cucumber

Hello i have the below CucumberOptions on my Junit Runner:
#CucumberOptions(plugin = {"pretty", "json:target/cucumber_parallel.json", "junit:target/cucumber_parallel-junit-report.xml",
"rerun:target/failed_scenarios/failed_parallel_once.txt", "timeline:target/timeline"},
features = {"classpath:features/parallel"},
tags = "(not #Ignored)",
glue = {"com.intrasoft.swp.trader.stepdefs"},
monochrome = true)
and we use maven command to execute the suites
mvn clean test -Dcucumber.filter.tags="#one"
right now the tag passed from the Terminal command overwrites the one set on Junit.
what I would is that when filter.tags are set on the maven command the filter that will be used is
#one and (not #Ignored)
the concatenations of both filters.
i would like this because user execute it manually and forgot to specify the #Ignored

It is not possible. Cucumber runner cannot merge options from different sources. It loads options from 4 sources:
from propfety file
from annotation
from environment variable
from system variable
Each time on a given step it completely overrides the values configured on previous step.

Related

How to set path of step Definition file , if my feature file ,stepDefinition file and runner file resides in separate folders?

I am using maven project for cucumber execution and below is my project explorer view.
Project Explorer
I want to instruct "SimpleDataDriverRunner.java" file to execute "SimpleDataDriverStepDefinition.java" class. Both these files are present in different folder structure (as shown in image above).
Below is my runner class
#RunWith(Cucumber.class)
#CucumberOptions(
features="dir path\\com\\features\\SimpleDataDriven.feature",
glue= {"stepDefinition"},
monochrome=true
)
Can someone please guide me how to achieve this and if this is correct way to deal with multiple runner file and step definition files ?
If there is any BDD cucumber sample project having multiple feature, step Definition and runner files is much appreciated .
I never understood why multiple runners are needed, if is just to run different scenarios it doesn't seem very efficient.
I would use just one runner and select what to run by tags.
Annotate your feature with some tag like #someTag and run it.
E.g.:
mvn test -Dcucumber.filter.tags="#someTag"
From cucumber documentation:
cucumber.features= # command separated paths to feature files. example: path/to/example.feature, path/to/other.feature
cucumber.glue= # comma separated package names. example: com.example.glue
Try adding in glue the package for runners also:
glue= {"stepDefinition", "runner"},

How to set a string name for pipeline build in jenkins

I am running jenkins on a linux machine.
I have a pipeline job in place, so each pipeline build shows build number in general. Instead of that is it possible to give string parameter to display on the UI ?
I have tried checking build parameter plugin, but in pipeline configuration i don't see option to inject the string parameter. And the documentation doesn't help.
You can set the build name by doing:
currentBuild.displayName = "#${currentBuild.number} Hello"
This will name the build #2 Hello for build number 2.
For my case, i had to add a line in my pipeline groovy script
node(Slave01) {
currentBuild.displayName = "${URL_Name}"
}
${URL_Name} = here is the parameter

Jenkins + Build Flow, how to pass a variable from one job to another

I have a build flow scenario similar to the documentation example: two jobs, one running after the other.
b = build("job1")
build("job2", param1: b.????)
My job1 is a shell script that builds a package out of a checked out git repositoy and prints out the version of the built package.
I need to extract the version from job1 (parse output??) and make it available somehow as a parameter to job2. How can this be achieved? Please note that I can't know the version before running job1.
The problem with simply using export in a shell script build step is that the exported variables disappear when the shell script exits, they are not propagated up to the job.
Use the EnvInject plugin to create environment variables in your build. If you
write out a properties file as part of your build, EnvInject can read the file and inject variables as a build step. A properties file has a simple KEY=VALUE format:
MY_BUILD_VERSION=some_parsed_value
Once you have an environment variable set in your job, in the Build Flow
plugin, you can extract the variable's value and use it in subsequent jobs:
def version = build.environment.get( "MY_BUILD_VERSION" )
out.println String.format("Parameters: version: %s", version)
build( "My Second Build", MY_BUILD_VERSION: version )
When you run job1 export the version with name as system property.
export appVersion="stringOfVersion-123"
Then it depend if you know how long is version (count of numbers or others characters). If you know it you can parse variable from end in second build as new variable and use it.
How parse string you can find in this question with nice examples.
If job2 always should get some information from job1 you could use approach without parameters. job1 could publish artifact with version and job2 will use that artifact (with Copy Artifact Plugin for example). With that approach job2 could be executed also as standalone job.
For anyone else coming upon this, another solution is to use a scriptler script, where you pass in the .properties file path, and the script will add the properties to the list of job variables:
Properties properties = new Properties()
FilePath workspace = build.getWorkspace()
FilePath sourceFile = workspace.child(path)
properties.load(sourceFile.read())
properties.each { key, value ->
key = key.replace(".", "_").toUpperCase()
Job.setVariable(build, key, value)
println "Created Variable: " + key + "=" + value
}
This will convert any periods to underscores, and capitalize all letters. Using a scriptler script ensures that you have a method that works independent of the "plugin soup" you are using.

How to get specific information about the current build project in Jenkins with Groovy?

In Jenkins/Hudson, with the help of a Postbuild Groovy script, I would like to get one of the following:
an environment variable (e.g. current JOB_NAME, BUILD_NUMBER etc.)
the result of a specific build number of the current project
the build number of the last not successful build in the current project
At the moment I only found the following way, but it's rather limited:
def item = hudson.model.Hudson.instance.getItem("GroovyMultipleFailTest")
def build = item.getLastBuild()
build.getNumber()
Using Jenkins v2.17 this works for me:
echo "BUILD_NUMBER=${env.BUILD_NUMBER}"
${manager.build.getEnvironment(manager.listener)['BUILD_NUMBER'] }
Bo Persson had the best answer, but was a little short.
To access the environment variables from the build in the Groovy Postbuild, you can grab them from the build. This sample code is useful for dumping all of the BUILD's environment variables to the console:
manager.build.getEnvironment(manager.listener).each {
manager.listener.logger.println(it);
}
If you're using Groovy script within "Env Inject", you can get current build and current job by:
currentJob.getName()
currentBuild.toString()
an environment variable (e.g. current JOB_NAME, BUILD_NUMBER etc.)
String jobName = System.getenv('JOB_NAME')
The only way I got it to work for me was with build.properties.environment.BUILD_NUMBER
I tried various approaches in this article and others, and it looks like the only one put below and also build.properties.environment.BUILD_NUMBER (upvoted) are working for me in Jenkins Execute system Groovy Script build step groovy command type
EnvVars envVars = build.getEnvironment(listener)
def n = envVars.get('BUILD_NUMBER')

On a build of a Jenkins job, is it possible to change build parameters midway through?

We are using Jenkins to automate several of our build and test processes. For some of our process, the engineer starting the build needs to specify a parameter. But the range of possible and optimal values for that parameter change throughout the course of the day.
What I would like to do is let the engineer specify a value - if they know an optimal value - or leave it blank and have a value be calculated by an early build step. If the value is calculated, I would like the calculating build step to update the parameter value of the job. That way, all subsequent build steps don't have to worry about using the parameter or calculating it, they just use the parameter regardless.
It looks like the Groovy Script Plugin might be able to do this, but I can't see how I can SET the build parameters, just GET them.
Found the answer: use the EnvInject Plugin. One of the features is a build step that allows you to "inject" parameters into the build job from a settings file. I used one build step to create the settings file, then another build step to inject the new values. Then, all subsequent build steps and post-build operations used the new value.
Update with an example:
To add a new parameter (REPORT_FILE), based on existing one (JOB_NAME), inject a map with new or modified parameters in the Groovy Script box:
// Setting a map for new build parameters
def paramsMap = [:]
// Set REPORT_FILE based on JOB_NAME
def filename = JOB_NAME.replace(' ','_') + ".html"
paramsMap.put("REPORT_FILE", filename)
// Add or modify other parameters...
return paramsMap
Jenkins does have the ability to parameterize builds. For a string parameter, the developer can leave the field blank and then your build scripts can check to see if the env. variable for the parameter is set. If the env. var. is not set, the script can perform whatever calculation is needed (I don't think Jenkins has "pre-build steps") and pass it along. For a choice parameter the first line can be something like (Default), and again the build script can test its value and act accordingly.
Note on (Default)
I tried leaving the first line of the choice box blank, and Jenkins saved it correctly the first time; but when I came back to reconfigure the build Jenkins ran some kind of trim on options and the leading blank line was removed so I settled on (Default).
I hope this helps,
Zachary

Resources