Retrieve custom variable from a successful jenkins

2020-05-01 06:57发布

I would like to retrieve a custom variable from a successful last stable build jenkins job. I was able to retrieve the build number using curl on this link using Execute Shell. It's an internal server. https://jenkinsci.internalsvr/view/webapps/job/common-tools/lastStableBuild/buildNumber

Now, I'd like to do the same for a custom variable using curl. But I know that I may need to save the value of the custom variable but not sure how and to where.

2条回答
该账号已被封号
2楼-- · 2020-05-01 07:58

So this is how I got it to work

I have this code in our dsl.groovy file

....

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

curl https://our-internal-server/job/theNameOfTheJob/lastStableBuild/artifact/env.properties
查看更多
贪生不怕死
3楼-- · 2020-05-01 07:58

You can do it with the API of the EnvInject plugin, either with:

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

More info here.

查看更多
登录 后发表回答