首页 > 解决方案 > 如何从 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
    } 
   }
 } 

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

标签: javaselenium-webdriverjenkins-pipelinemaven-2jenkins-groovy

解决方案


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


推荐阅读