java - 如何从 Jenkins Pipeline groovy 脚本更新配置属性文件?
问题描述
我是 Jenkins Pipeline 的新手。我们有基于 maven 的 selenium-java 自动化框架。我正在创建一个 Jenkins Pipeline groovy 脚本来调用自动化框架。
在框架中,我们有 config.properties 文件,其中存储了应用程序 url、用户名和密码。
配置属性:
网址= https://#########/login.jsp
用户名=########
密码=########
要求:我们需要将 Application URL 、 Username 和 Password 作为 Jenkins 参数,并相应地运行自动化套件。
问题:如何从 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步骤来加载属性。
推荐阅读
- c++ - 没有匹配的函数调用'bind(
, 常量 std::_Placeholder<1>&, int*) - python - 尝试将多行字符串大写,但不起作用。有什么想法吗?
- c - 如果用户和内核中的数据结构大小相同,缓存未命中是否也相同?
- swift - 为什么 Int as Double 的类型注释有效但 Double as Int 无效?
- c - 如何从几个线程中异步读取相同的文件字节?
- excel - 关于excel宏的两个问题
- r - 如何从 R 中的嵌套列表构建数据框
- javascript - 使用按钮和 .active css 类显示显示/隐藏内容
- php - 优化获取 HTTP 状态码的执行时间
- android - 如何在 Android 布局 xml 文件中获取小部件的完整类名?