【问题标题】:How to update config properties file from a Jenkin Pipeline groovy script?如何从 Jenkins Pipeline groovy 脚本更新配置属性文件?
【发布时间】:2020-06-05 03:43:09
【问题描述】:

我是 Jenkins Pipeline 的新手。我们有基于 maven 的 selenium-java 自动化框架。我正在创建一个 Jenkins Pipeline groovy 脚本来调用自动化框架。

在框架中,我们有 config.properties 文件,其中存储了 Application url 、用户名和密码。

config.properties:

网址=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
    } 
   }
 } 

感谢有关如何修复代码以实现所需结果的任何帮助

【问题讨论】:

  • 请分享您的工作。
  • 你好。解决方法是获取 maven 命令中的参数,但 pom.xml 中的变量除外。

标签: java selenium-webdriver jenkins-pipeline maven-2 jenkins-groovy


【解决方案1】:

使用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 步骤加载属性。

【讨论】:

  • 你好尤里。感谢回复。 Config.properties 文件在我的框架内。 Jenkins 构建首先从工作空间中的 git 拉取源代码。我需要在脚本中提供属性文件的完整路径吗?
  • @sudhirranjanSwain 我添加了一个指向 readProperties 步骤的链接。请阅读文档并尝试实施您的解决方案。如果您有任何问题,请编辑您的问题并提供更多详细信息
  • 你好尤里。伟大的 。非常感谢。这行得通。只有一个问题。此解决方案会覆盖整个文件。如果我只想更新这三个值而不触及 config.properties 中的其他值,该怎么办?
  • node{ stage("props file"){ script { def props = """Url=https://#########/login.jsp 用户名=### ##### 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 } } } - 这是覆盖所有内容并添加以上三行。但我只想更新这些键的值。因为我的配置文件包含其他键
  • @sudhirranjanSwain 也许是这个:stackoverflow.com/a/51739266/7571258
猜你喜欢
  • 2016-12-21
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多