Writing to a json file in workspace using Jenkins

后端 未结 3 757
面向向阳花
面向向阳花 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: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"
    }
    

提交回复
热议问题