Writing to a json file in workspace using Jenkins

后端 未结 3 758
面向向阳花
面向向阳花 2020-12-31 21:19

I\'ve a jenkins job with few parameters setup and I\'ve a JSON file in the workspace which has to be updated with the parameters that I pass through jenkins.

Example

相关标签:
3条回答
  • 2020-12-31 22:01

    With Pipeline Utility Steps plugin this is very easy to achieve.

        jsonfile = readJSON file: 'path/to/your.json'
        jsonfile['environment'] = 'ENV2'
        writeJSON file: 'path/to/your.json', json: jsonfile
    
    0 讨论(0)
  • 2020-12-31 22:03

    Config File Provider Plugin doesn't allow you to pass parameters to configuration files. You can solve your problem with any scripting language. My favorite approach is using Groovy plugin. Hit a check-box "Execute system Groovy script" and paste the following script:

    import groovy.json.*
    
    // read build parameters
    env = build.getEnvironment(listener)
    environment = env.get('environment')
    filename = env.get('filename')
    
    // prepare json
    def builder = new JsonBuilder()
    builder environment: environment, filename: filename
    json = builder.toPrettyString()
    
    // print to console and write to a file
    println json
    new File(build.workspace.toString() + "\\job.json").write(json)
    

    Output sample:

    {
        "environment": "ENV2",
        "filename": "abc.txt"
    }
    
    0 讨论(0)
  • 2020-12-31 22:04

    I will keep it simple. A windows batch file or a shell script (depending on the OS) which will read the environment values and open the JSON file and make the changes.

    0 讨论(0)
提交回复
热议问题