用脚本更改Jenkins的项目变量值的方法 [英] Way to change Jenkins' project variable value with script

查看:637
本文介绍了用脚本更改Jenkins的项目变量值的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建完成后,是否可以自动在Jenkins中更改项目变量值? 在我的情况下,我有一个变量VERSION,默认值为1.每次构建完成后,我都需要将此默认值递增.在这种情况下,假设由cron建立星.有插件可以帮助我吗?

Is there a way to change project variable values in Jenkins automatically when build is done? In my case i got a variable VERSION, default value is 1. And i need to increment this default value every build done. Assuming build stars by cron in this case. Any plugins can help me?

现在我有类似这样的内容:我的构建步骤. 这是在我发现的groovy脚本中获取项目变量的单一工作方式.现在如何为变量设置新值? 我在SO上读到了类似的问题,但找不到适合我的方法.

Now i have something like this: My build steps. It is a single working way to get project variable in my groovy script that i found. Now how can i set new value for variable? I read some similar question on SO, but didn't found a working way for me.

P.S.我不能使用$ BUILD_NUMBER var,因为我需要在开始构建时手动设置VERSION.

P.S. I can't use $BUILD_NUMBER var, because i need a possibility to set VERSION manually when i start build.

推荐答案

首先,安装插件 Groovy Postbuild插件.在Manage Jenkins -> Configure System下,您现在应该拥有一个名为Global Properties的零件.在那里您添加了一个新变量.在测试中,我将其称为SOME_VER.

First, of all, install the plugins Global Variable String Parameter Plugin and Groovy Postbuild Plugin. Under Manage Jenkins -> Configure System you should now have a part, called Global Properties. There you add a new variable. In my tests, I called it SOME_VER.

在您的工作中,您现在添加了一个Groovy postbuild部件,并将此代码调整为您的变量:

At your job, you now add a Groovy postbuild part with this code adjusted to your variable:

import jenkins.*;
import jenkins.model.*;
import hudson.*;
import hudson.model.*;
import java.lang.*;

instance = Jenkins.getInstance();
globalNodeProperties = instance.getGlobalNodeProperties();
envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class);

envVars = null

if (envVarsNodePropertyList != null && envVarsNodePropertyList.size() > 0) 
{
    envVars = envVarsNodePropertyList.get(0).getEnvVars()

    String value = envVars.get("SOME_VER", "0")
    int NEW_VER = Integer.parseInt(value)
    NEW_VER = NEW_VER + 1

    envVars.override("SOME_VER", NEW_VER.toString());
}

instance.save()

部分代码摘录自此处.该代码除了检索全局变量的值,对其进行更改并保存该变量的新值外,什么也不做.

Parts of this code are taken from here. This code does nothing else than retrieving the value of the global variable, change it and save the new value of the variable.

这篇关于用脚本更改Jenkins的项目变量值的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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