Back to All Scenarios
Scenario 84 of 100
CI/CD
Intermediate
Passing Environment Parameters Dynamically from Jenkins
π₯Scenario Overview
Passing Environment Parameters Dynamically from Jenkins
Key Takeaways & Cheat Sheet
- βDefine dynamic parameterized configuration options in Jenkins
- βRead parameters inside Jenkinsfiles as environment variables
- βPass variables to Maven execution calls using -D flags
- βAccess values in your Java code using System.getProperty()
Short Direct Answer
To pass parameters from Jenkins, define them in your Jenkins project configuration. In your pipeline script (`Jenkinsfile`), pass these variables to your build tool using JVM system property flags (`-DparameterName`), and retrieve them in your Java code using `System.getProperty()`.
β οΈ Senior Warning (Red Flag)
Never hardcode execution parameters like target browsers or URLs. Keep tests configurable so they can run across different browsers and environments without code changes.
π‘ STAR Deep Dive Explanation & Pro Tip
Using fallback values inside System.getProperty() is a best practice. It allows developers to run tests locally without having to set command line flags.
SeleniumAutomation.java
Selenium 4 + Java// β
Maven Execution using Jenkins parameters
// sh "mvn test -Denv=${params.ENV} -Dbrowser=${params.BROWSER}"
// β
Java code reading variables dynamically
public class ConfigurationProvider {
public static String getExecutionEnvironment() {
// Reads parameter; falls back to "qa" if undefined
return System.getProperty("env", "qa");
}
public static String getTargetBrowser() {
// Reads parameter; falls back to "chrome" if undefined
return System.getProperty("browser", "chrome");
}
}