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

时间:2016-04-23 15:11:22

标签: rest curl jenkins groovy

我想从成功的最后一个稳定构建jenkins作业中检索自定义变量。我能够使用Execute Shell在此链接上使用curl检索内部版本号。这是一个内部服务器。 https://jenkinsci.internalsvr/view/webapps/job/common-tools/lastStableBuild/buildNumber

现在,我想使用curl对自定义变量执行相同的操作。但我知道我可能需要保存自定义变量的值,但不确定如何以及在哪里。

2 个答案:

答案 0 :(得分:1)

所以这就是我开始工作的方式

我在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')
  }
}

因此,如果我需要访问它,http url应该像这样形成

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

答案 1 :(得分:0)

您可以使用EnvInject plugin的API来做到这一点,

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

更多信息here

相关问题