如何从Jenkins向config.properties文件传递参数?
Hey there! Great question—this is totally doable, and it’s actually a best practice for making your automation scripts flexible without having to edit config files every time. Let me break this down for you step by step, since you’re new to Jenkins configurations.
可行性确认
Absolutely! Jenkins supports parameterized builds, which lets you pass values into your job at runtime. Your idea to replace hardcoded values in config.properties with placeholders like ${BROWSER_NAME} and ${URL_NAME} is exactly how you’d set this up.
步骤1:修改你的config.properties文件
First, update your config file to use the placeholders as you suggested:
browserName=${BROWSER_NAME} url=${URL_NAME}
This tells your pipeline to replace these placeholders with the values you’ll pass during the build.
步骤2:配置Jenkins参数化构建
Now let’s set up the parameters in your Jenkins job:
- Open your existing Jenkins job and click Configure (top-left corner of the job page).
- Scroll to the General section, then check the box that says This project is parameterized.
- Click Add Parameter and select String Parameter (this is the most basic type for text values like browser names or URLs).
- For the first parameter:
- Name: Enter
BROWSER_NAME(this must match the placeholder name exactly—case-sensitive!) - Default Value: Set a default like
ie(so you don’t have to type it every time) - Description: Add a quick note like "Browser to run automation on (e.g., ie, chrome, firefox)"
- Name: Enter
- Repeat the process to add a second String Parameter:
- Name:
URL_NAME - Default Value:
http://google.com - Description: "Target URL for the automation script"
- Name:
- For the first parameter:
步骤3:添加变量替换的构建步骤
Jenkins won’t automatically replace the ${} placeholders in your .properties file—you need to add a step to do that before running your automation script. The method depends on whether your Jenkins agent is running Linux/macOS or Windows:
For Linux/macOS Agents (Execute Shell)
Add an Execute Shell build step (before your automation script runs) with this command:
# Replace placeholders with Jenkins parameter values and overwrite the config file envsubst < config.properties > config.properties.tmp && mv config.properties.tmp config.properties
envsubst is a tool that replaces environment variables (which Jenkins passes your parameters as) in text files. This command creates a temporary file with replaced values, then replaces the original config file.
If envsubst isn’t installed on your agent, you can install it with:
# Debian/Ubuntu sudo apt-get install gettext # RHEL/CentOS sudo yum install gettext
For Windows Agents (PowerShell)
Add a PowerShell build step (more reliable for variable replacement than batch):
# Read the config file, replace placeholders, and save back (Get-Content config.properties) ` -replace '\${BROWSER_NAME}', $env:BROWSER_NAME ` -replace '\${URL_NAME}', $env:URL_NAME | Set-Content config.properties
This reads the config file, replaces each placeholder with the corresponding Jenkins parameter value, then writes the updated content back to the file.
步骤4:测试运行
Now you’re ready to test! Click Build with Parameters on your job page. You’ll see the two parameters you set—you can use the default values or change them (e.g., set BROWSER_NAME to chrome and URL_NAME to http://example.com).
After the build runs, check the build logs to confirm the placeholders were replaced correctly. You can also add a quick step to print the config file content (e.g., cat config.properties in shell) to verify.
关键注意事项
- Case Sensitivity: The parameter name in Jenkins (e.g.,
BROWSER_NAME) must exactly match the placeholder in your config file (${BROWSER_NAME})—uppercase/lowercase matters! - Agent Compatibility: Make sure you use the right script type (shell for Linux, PowerShell for Windows) based on your Jenkins agent’s OS.
- File Permissions: Ensure the Jenkins user has write access to the
config.propertiesfile so it can be updated.
内容的提问来源于stack exchange,提问作者Alok Tiwari

