简体   繁体   English

通过REST API调用从成功的jenkins作业中检索自定义变量

[英]Retrieve custom variable from a successful jenkins job run via REST api call

I would like to retrieve a custom variable from a successful last stable build jenkins job. 我想从成功的上一次稳定构建jenkins作业中检索自定义变量。 I was able to retrieve the build number using curl on this link using Execute Shell. 我可以使用Execute Shell在此链接上使用curl来检索内部版本号。 It's an internal server. 这是一台内部服务器。 https://jenkinsci.internalsvr/view/webapps/job/common-tools/lastStableBuild/buildNumber https://jenkinsci.internalsvr/view/webapps/job/common-tools/lastStableBuild/buildNumber

Now, I'd like to do the same for a custom variable using curl. 现在,我想对使用curl的自定义变量执行相同的操作。 But I know that I may need to save the value of the custom variable but not sure how and to where. 但是我知道我可能需要保存自定义变量的值,但不确定如何以及在何处。

So this is how I got it to work 所以这就是我如何使其工作

I have this code in our dsl.groovy file 我的dsl.groovy文件中有此代码

....

parameters {
    stringParam('CUSTOM_VAR1', '', 'Custom Variable')
    stringParam('CUSTOM_VAR2', '', 'Custom Variable')
}

shellCommands = sprintf('''#/bin/bash
echo "CUSTOM_VAR1=\${%s}" > env.properties
echo "CUSTOM_VAR2=\${%s}" >> env.properties
''', ['CUSTOM_VARIABLE1','CUSTOM_VARIABLE1'])

shell(shellCommands)

// This is extremely important
environmentVariables {
  propertiesFile('env.properties')
}

// This allowed me to retrieve env.properties via http call from browser or curl.

publishers {
  archiveArtifacts {
    pattern('env.properties')
  }
}

So if I need to access it, the http url should be formed like this 因此,如果我需要访问它,则http网址应如下所示

curl https://our-internal-server/job/theNameOfTheJob/lastStableBuild/artifact/env.properties

You can do it with the API of the EnvInject plugin , either with: 您可以使用EnvInject插件的API来执行此操作 ,方法是:

curl <jenkins-host>/job/<job_name>/<buildNumber>/injectedEnvVars/export
curl <jenkins-host>/job/<job_name>/<buildNumber>/injectedEnvVars/api/python

More info here . 更多信息在这里

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

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