简体   繁体   English

如何从 Jenkins Pipeline groovy 脚本更新配置属性文件?

[英]How to update config properties file from a Jenkin Pipeline groovy script?

I am new to Jenkin Pipeline.我是 Jenkins Pipeline 的新手。 We have maven based selenium-java Automation framework.我们有基于 maven 的 selenium-java 自动化框架。 I am creating a Jenkin Pipeline groovy script to invoke the Automation framework.我正在创建一个 Jenkins Pipeline groovy 脚本来调用自动化框架。

In the framework we have config.properties file in which Application url, username and password is stored.在框架中,我们有 config.properties 文件,其中存储了应用程序 url、用户名和密码。

config.properties:配置属性:

Url= https://#########/login.jsp网址= https://#########/login.jsp

Username=########用户名=########

Password=########密码=########

Requirement: We need to take the Application URL, Username and Password as a Jenkin parameter, and run the automation suite accordingly.要求:我们需要将Application URL,用户名和密码作为Jenkins参数,并相应地运行自动化套件。

Question: From Pipeline groovy script how can I update the config.properties file at runtime?问题:从管道 groovy 脚本如何在运行时更新 config.properties 文件? Is there any possibility of creating a java class inside the framework to update config file and invoke the java class from groovy script. Is there any possibility of creating a java class inside the framework to update config file and invoke the java class from groovy script.

I've tried the following code我试过下面的代码

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
    } 
   }
 } 

Appreciate any help on how to fix the code to achieve the needed result感谢有关如何修复代码以实现所需结果的任何帮助

Use writeFile step to write the file.使用writeFile步骤写入文件。

The following example writes and reads the file config.properties以下示例写入和读取文件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

            }
         }
      }
   }
}

Update: If the properties file already exists you can use the readProperties step to load the properties.更新:如果属性文件已经存在,您可以使用readProperties步骤加载属性。

If you want to update one property in a properties-file, I think it is easiest to:如果要更新属性文件中的一个属性,我认为最简单的方法是:

  • Read the file as proprties.将文件作为属性读取。
  • Change the property.更改属性。
  • Serialize to String.序列化为字符串。
  • Write that string to properties-file again.再次将该字符串写入属性文件。

Here is an example updating version in gradle.properties :这是gradle.properties中的示例更新version

HashMap<String,String> props = readProperties(file: "gradle.properties")
props.version = newVersion

def propsString = props.
    collect { k, v -> "$k=$v"}
    .join("\n")

writeFile file: "gradle.properties", text: propsString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM