Accessing Jenkins global property in Groovy

后端 未结 4 1467
死守一世寂寞
死守一世寂寞 2021-02-10 18:20

At this moment I defined a environment variable called GLOBAL_VAR with the value \'test\' under Manage Jenkins -> Configure System -> Global Properties. I have a shell script wh

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-10 18:46

    You can use:

    import jenkins.model.*
    instance = Jenkins.getInstance()
    globalNodeProperties = instance.getGlobalNodeProperties()
    envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
    
    newEnvVarsNodeProperty = null
    envVars = null
    
    if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
      newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
      globalNodeProperties.add(newEnvVarsNodeProperty)
      envVars = newEnvVarsNodeProperty.getEnvVars()
    } else {
      envVars = envVarsNodePropertyList.get(0).getEnvVars()
    
    }
    
    envVars.put("FOO", "foo")
    instance.save()
    

    The solution was found here

提交回复
热议问题