简体   繁体   English

詹金斯:如何从Groovy脚本中获取价值以用于下游项目?

[英]Jenkins: How to get value from a Groovy Script to be used in a downstream project?

I need to be able to get some slave information to be used in one of my jobs. 我需要能够获取一些从属信息以在我的一项工作中使用。

I have a Groovy system script to access the slave information 我有一个Groovy系统脚本来访问从站信息

for (aSlave in hudson.model.Hudson.instance.slaves) {
  println('====================');
  println('Name: ' + aSlave.name);
  println('getLabelString: ' + aSlave.getLabelString());

  ... In here I can dig out the information that I need
}

Is there a way how I can get the information back to use in a Post Build Job? 有没有一种方法可以让我将信息重新用于构建后的工作中?

I need to add the output to a parameter or something that can be used by a Post Build Job? 我需要将输出添加到参数或可以由Post Build Job使用的东西吗?

If you are running windows I have got a solution for you: You can save your settings into environment varables, which are usable for the currently running job. 如果您正在运行Windows,我为您提供了一个解决方案:您可以将设置保存到环境变量中,这些变量可用于当前正在运行的作业。 They will no longer exist once the job is finished, but they are usable for post-build action. 一旦完成工作,它们将不再存在,但可用于生成后操作。 Here is an example: 这是一个例子:

//Creating String to make my example more clear
String myString = 'this is just a test!';

//Create an environment variable, so the Jenkins job can use the parameter  
def pa = new ParametersAction([new StringParameterValue('PARAMETER_NAME', myString)]);  

// Add variable to current jobs environment variables.
Thread.currentThread().executable.addAction(pa) 
println 'Script finished! \n';

After the script ran you can use %PARAMETER_NAME% (if post-build-actions etc.) to gain access to its content. 脚本运行后,您可以使用%PARAMETER_NAME%(如果有构建后动作等)来访问其内容。

Additional Hint: To see all available environment variables you can use the build step "execute windows batch command" and click on "See the list of available environment variables" on the buttom (the variables you create while executing scripts are excluded). 其他提示:要查看所有可用的环境变量,可以使用构建步骤“执行Windows批处理命令”,然后单击按钮上的“查看可用的环境变量列表”(不包括在执行脚本时创建的变量)。 But you can use these variables within your groovy script using eg: 但是您可以使用以下命令在groovy脚本中使用这些变量:

String jenkinsHome = System.getenv('JENKINS_HOME');

I used The EnjEnv plugin and it has a 'Evaludated Groovy Script' section that basically you can do anything... but it should return a property map that will be used as Environment variables. 我使用了EnjEnv插件,它有一个“评估的Groovy脚本”部分,基本上您可以做任何事情……但是它应该返回一个将用作环境变量的属性映射。 I don't know how to return a value from a Groovy script so this worked kewl for me as I can reference property (or Environment variables) from almost anyware 我不知道如何从Groovy脚本返回值,所以这对我来说非常有效,因为我可以从几乎任何软件中引用属性(或环境变量)

import hudson.model.*

String labelIWantServersOf = TheLabelUsedOnTheElasticAxisPlugin; // This is the label assosiated with nodes for which i want the server names of
String serverList = '';

for (aSlave in hudson.model.Hudson.instance.slaves) {          
  out.println('Evaluating Server(' + aSlave.name + ') with label = ' + aSlave.getLabelString());  

  if (aSlave.getLabelString().indexOf(labelIWantServersOf ) > -1) {
    serverList += aSlave.name + ' ';        
    out.println('Valid server found: ' + aSlave.name);                  
  }    

}

out.println('Final server list where SOAP projects will run on = ' + serverList + ' which will be used in the environment envInject map');

Map<String, String> myMap = new HashMap<>(2);
myMap.put("serverNamesToExecuteSoapProjectOn", serverList );
return myMap;

And then I write the environment variable serverNamesToExecuteSoapProjectOn to a property file using a windows batch script and pass the property file to the next build as a parameterized build 然后,我使用Windows批处理脚本将环境变量serverNamesToExecuteSoapProjectOn写入属性文件,并将该属性文件作为参数化版本传递给下一个版本

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

相关问题 带有Jenkins的Groovy脚本用于下游参数 - Groovy script with Jenkins for Downstream Parameters 如何从詹金斯管道作业中的groovy脚本变量获取默认值 - How to get default value from groovy script variable in jenkins pipeline job 使用Groovy Script从master获取Jenkins的工作区目录 - Get Jenkins workspacedirs from master with Groovy Script 如何使用 Groovy 控制台脚本/Jenkins API 从每个 Jenkins 作业获取脚本路径 - How to get Script Path from each Jenkins Jobs using Groovy Console Script/Jenkins API 如何在Groovy脚本中获取Jenkins构建参数? - How to get Jenkins build parameters in the Groovy script? 如何从 groovy 脚本运行 jenkins 作业 - How to run jenkins job from groovy script groovy脚本仅从jenkins项目角色中删除用户? - groovy script to remove users from jenkins project role alone? 如何将值从一个 Shell 块传递到另一个 Shell 块或将该值分配给 GroovyADF35AB22E54ACEE5ADAD3 中 GroovyADF35AB22E54ACEE4C0 中的变量 - How to Pass Value from One Shell Block to Another Shell Block or assign that value to the Variable of the Groovy Script in Jenkins jenkins-如何将值从bash传递到groovy? - jenkins - how to pass a value from bash to groovy? 如何在脚本控制台中使用groovy脚本获取jenkins作业列表 - how to get jenkins jobs list using groovy script in script consol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM