简体   繁体   English

如何在 Jenkins 中永久使用 Groovy PostBuild 更新全局环境变量并在其他后续构建中使用它?

[英]How can I update a global Environment Variable using Groovy PostBuild in Jenkins permanently and use it in other subsequent builds?

I have a requirement where I want to create an Service Now Incident only when a previous similar incident is closed.我有一个要求,我只想在以前的类似事件关闭时才创建一个 Service Now 事件。 I am using Jenkins freestyle Job.我正在使用 Jenkins 自由式工作。 The approach I have taken is as below:-我采取的方法如下:-

  1. Create a Global environment variable to store Incident Number and set a default value.创建全局环境变量以存储事件编号并设置默认值。
  2. Check the state of the incident in an upstream job and pass the state to the downstream job.在上游作业中检查事件的 state 并将 state 传递给下游作业。
  3. In the downstream job, if the incident is still open, do nothing.在下游工作中,如果事件仍然存在,则什么也不做。 If it is Closed/Resolved, create another Incident with some defined descriptions.如果它已关闭/已解决,请创建另一个带有一些已定义描述的事件。
  4. Update the global environment variable with the current Incident no.使用当前事件编号更新全局环境变量。 and store it permanently.并永久保存。
  5. Check the state of the updated Incident Number(by accessing the global Environment Variable) and then follow the same process during subsequent builds.检查更新事件编号的 state(通过访问全局环境变量),然后在后续构建中遵循相同的过程。

Till now I have achieved till step 3 by using various plugins.到目前为止,我已经通过使用各种插件实现了第 3 步。 To achieve step 4, I am using groovy post-build plugin, so that I can update the global environment variable permanently, so that it is available in my next build, so that I can check the status of the incident before creating a new one.为了实现第 4 步,我正在使用 groovy 构建后插件,这样我就可以永久更新全局环境变量,以便在我的下一个构建中可用,这样我就可以在创建新的之前检查事件的状态. I have come-up with a code, but it does not seem to update the variable permanently(Verified by printing the variable in a subsequent build).我想出了一个代码,但它似乎并没有永久更新变量(通过在后续构建中打印变量来验证)。 The script also has other parts as per my requirement but SNOW_INC is the global variable that I want to update permanently, till the next build.根据我的要求,该脚本还有其他部分,但 SNOW_INC 是我想要永久更新的全局变量,直到下一次构建。

import groovy.json.JsonSlurper
import hudson.model.*

def workspace = manager.build.getEnvVars()["WORKSPACE"]
def console = manager.listener.logger.&println

build = Thread.currentThread().executable
String jobName = build.project.getName()
job = Hudson.instance.getJob(jobName)
my_env_var = job.getLastBuild().getEnvironment()["SNOW_INC"]
console(my_env_var)

try{
def inputFile = new File(workspace, 'response.json')
if (inputFile != null){
JsonSlurper slurper = new JsonSlurper()
parsedJson = slurper.parse(inputFile)
String number= parsedJson.result.number
console("${number}")

System.setProperty("hudson.model.ParametersAction.safeParameters", "INC_NUMBER")

manager.build.addAction(
        new ParametersAction(
            new StringParameterValue("INC_NUMBER", "${number}")
        )
    )

manager.build.addAction(
        new ParametersAction(
            new StringParameterValue("SNOW_INC", "${number}")
        )
    )
console("Environment Variable set")
}
} catch (Exception e){
println("response.json file was not ")
}

Any help would be appreciated!任何帮助,将不胜感激!

Anyhow, found out the solution in few days.无论如何,几天后找到了解决方案。 So in order to modify the global environment variable that can be used across any jobs or any builds is as follows(this was used in Groovy Postbuild as mentioned):因此,为了修改可在任何作业或任何构建中使用的全局环境变量,如下所示(如前所述,在 Groovy Postbuild 中使用):

nodes = Jenkins.getInstance().getGlobalNodeProperties()
nodes.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
if ( nodes.size() != 1 ) {
    console("error: unexpected number of environment variable containers: "
        + nodes.size()
        + " expected: 1")
} else {
    envVars= nodes.get(0).getEnvVars()
    envVars.put("SNOW_INC", "${number}")
    Jenkins.getInstance().save()
    
}

So after any run where a new incident is being created, I am updating the environment variable "SNOW_INC" with the updated number.因此,在创建新事件的任何运行之后,我将使用更新后的编号更新环境变量“SNOW_INC”。 And then it is being used in another job to verify whether that incident is open or not according to the scenario.然后在另一个工作中使用它来根据场景验证该事件是否开放。 Works like a charm!奇迹般有效!

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

相关问题 如何使用Groovy Postbuild强制工作获得成功? - How can I use Groovy Postbuild to force a job to success? 我可以从管道脚本更新Jenkins全局环境变量吗? - Can I update a Jenkins Global Environment Variable from a pipeline script? 如何从groovy Postbuild插件访问Jenkins构建工件? - How can I access Jenkins build artifacts from the groovy Postbuild plugin? 如何通过 jenkinsfile 在 jenkins 中永久更新 ENV 变量,以用于成功构建 - How to Update ENV variable permanently in jenkins through jenkinsfile, to be used in successful builds 如何在部署作业中使用 groovy 脚本列出成功的 jenkins 构建? - How can I list successful jenkins builds using a groovy script in a deploy job? 如何标记jenkins构建不稳定与groovy postbuild - How to mark the jenkins build unstable with groovy postbuild Jenkins Groovy - 如何获取给定作业的所有正在运行的构建的 params 全局变量? - Jenkins Groovy - How to get params global variable of all running builds of a given job? 使用 Groovy 创建 Jenkins 环境变量 - Creating a Jenkins environment variable using Groovy 如何覆盖 jenkins groovy 中的全局变量 - How to overwrite global variable in jenkins groovy Jenkins Groovy Postbuild使用静态文件而不是脚本 - Jenkins Groovy Postbuild use static file instead of script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM