πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
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");
    }
}