我是 Jenkin Pipeline 的新手。我们有基于 maven 的 selenium-java 自动化框架。我正在创建一个 Jenkin Pipeline groovy 脚本来调用自动化框架。
在框架中,我们有 config.properties 文件,其中存储了应用程序 url、用户名和密码。
配置属性:
网址= https://#########/login.jsp
用户名=########
密码=########
要求:我们需要将应用程序 URL、用户名和密码作为 Jenkin 参数,并相应地运行自动化套件。
问题:如何通过 Pipeline groovy 脚本在运行时更新 config.properties 文件?是否有可能在框架内创建一个java类来更新配置文件并从groovy脚本调用java类。
我尝试过以下代码
node{
stage("props file"){
script {
def props = """Url=https://#########/login.jsp Username=########
Password=########"""
writeFile interpolate: true ,file: 'ui-automation/fw/config/config.properties', text: props
def str = readFile file: 'ui-automation-fw/config/config.properties'
echo str
}
}
}
感谢有关如何修复代码以实现所需结果的任何帮助
最佳答案
使用writeFile步骤写入文件。
以下示例写入和读取文件config.properties
pipeline {
agent any
stages{
stage("props file"){
steps{
script {
def props = """Url=https://#########/login.jsp
Username=########
Password=########"""
writeFile file: "config.properties", text: props
def str = readFile file: "config.properties"
echo str
}
}
}
}
}
更新: 如果属性文件已存在,您可以使用 readProperties步骤加载属性。
关于java - 如何从 Jenkins Pipeline Groovy 脚本更新配置属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60326310/