使用Jenkins在工作区中写入json文件 [英] Writing to a json file in workspace using Jenkins

查看:548
本文介绍了使用Jenkins在工作区中写入json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置很少参数的jenkins作业,并且在工作区中有一个JSON文件,该文件必须使用通过jenkins传递的参数进行更新.

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.

示例:

我有以下参数,我将从触发作业的用户那里获取输入:

I have the following parameters which I'll take input from user who triggers the job:

  • 环境(考虑用户选择"ENV2")
  • 文件名(考虑用户的默认值)

我的工作区中有一个json文件,位于run/job.json下,内容如下:

I have a json file in my workspace under run/job.json with the following contents:

{
    environment: "ENV1",
    filename: "abc.txt"
}

现在,用户在触发作业之前给定的任何值都必须在job.json中替换.

Now whatever the value is given by user before triggering a job has to be replaced in the job.json.

因此,当用户触发作业时,job.json文件应为:

So when the user triggers the job, the job.json file should be:

{
    environment: "ENV2",
    filename: "abc.txt"
}

请注意必须更新的json中的环境值.

Please note the environment value in the json which has to be updated.

我尝试了 https://wiki.jenkins- ci.org/display/JENKINS/Config+File+Provider+Plugin 插件.但是我无法找到有关参数化值的任何帮助.

I've tried https://wiki.jenkins-ci.org/display/JENKINS/Config+File+Provider+Plugin plugin. But I'm unable to find any help on parameterizing the values.

请建议配置此插件,或者建议其他任何可以满足我目的的插件.

Kindly suggest on configuring this plugin or suggest any other plugin which can serve my purpose.

推荐答案

配置文件提供程序插件不允许您将参数传递给配置文件.您可以使用任何脚本语言来解决您的问题.我最喜欢的方法是使用 Groovy插件.选中执行系统Groovy脚本"复选框,然后粘贴以下脚本:

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)

输出示例:

{
    "environment": "ENV2",
    "filename": "abc.txt"
}

这篇关于使用Jenkins在工作区中写入json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆