简体   繁体   English

使用Java代码从jenkins作业中获取参数

[英]fetching parameters from a jenkins job in java code

I have a parameterised jenkins job that is accessing my plugin. 我有一个参数化的詹金斯工作正在访问我的插件。 Inside plugin's code in java, i require these parameters using which i have to trigger another job in jenkins. 在Java的插件代码内部,我需要使用这些参数才能触发jenkins中的另一项工作。 I am not able to fetch these parameters and it is a very high priority issue now. 我无法获取这些参数,现在这是一个非常高优先级的问题。 I ahve tried multiple solutions available on stackoverflow, for eg., tried accessing the environment variables but didn't received the param's value. 我尝试了stackoverflow上可用的多种解决方案,例如,尝试访问环境变量,但未收到参数的值。 for eg, my parameter is 'REPOS' and i need its value, i have tried : 例如,我的参数是“ REPOS”,我需要它的值,我尝试过:

System.getProperty("REPOS");

but it returns null. 但它返回null。

Also, tried : 另外,尝试:

Map<String, String> env = System.getenv();
              for (String envName : env.keySet()) {
                  System.out.format("%s=%s%n", envName, env.get(envName));
              }

but it prints the jenkins' environment variables and not the job's parameters. 但是它会打印詹金斯的环境变量,而不是作业的参数。

I am referring to the value passed in the text box beside param: "url" in the below picture. 我指的是下图中param:“ url”旁边的文本框中传递的值。

在此处输入图片说明

Please assist. 请协助。

To the best of my knowledge, Jenkins parameters are exposed as environment variables. 据我所知,詹金斯参数作为环境变量公开。 However, if you use a build system like Maven that launches Java sub-processes, those variables might not be inherited. 但是,如果使用像Maven这样的构建系统来启动Java子进程,则这些变量可能不会被继承。

Probably your most reliable approach is to explicitly pass parameters as system properties in your Jenkins configuration, eg -DREPOS=${REPOS} 您最可靠的方法可能是在Jenkins配置中显式传递参数作为系统属性,例如-DREPOS=${REPOS}

One of the option to achieve this is "PASS and GET AS ARGUMENT" in your java code. 实现此目的的选项之一是Java代码中的“通过并获取参数”。

Jenkins's variables value can be retrieved like this. 詹金斯的变量值可以这样检索。

urlValue=${url} 

Then pass this value in java code. 然后在Java代码中传递此值。 This will be structure of your Execute shell in job configuration. 这将是作业配置中Execute Shell的结构。

#!/bin/bash -e
urlValue=${url} 
# run java code
java <classNameToRun> $urlValue

Thanks for the suggestions, but it worked when i did the following: I hit the 感谢您的建议,但是当我执行以下操作时它起作用了:

JENKINS_HOST_URL/job/JENKINS_SEED_JOB_NAME/api/json?pretty=true

and found the current build number, thereafter, hitting jenkins again with this number (JENKINS_HOST_URL/job/JENKINS_SEED_JOB_NAME/currBuildNum/api/json?pretty=true) , 并找到了当前的内部版本号,此后,再次使用该编号击中jenkins (JENKINS_HOST_URL/job/JENKINS_SEED_JOB_NAME/currBuildNum/api/json?pretty=true)

I was able to fetch all the parameters passed in there in this build. 我能够获取在此构建中传入的所有参数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM