0
votes

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
As a workaround you can save its value in a text file and then archive that file as an artifact. Then curl/wget it. - izzekil
Cool but when I archive it, what's the link that I wil curl or wget? Let's say the filename is info.txt I haven't tried archiving that's why I'm kinda lost. Thanks! - devwannabe
I finally got it working! WOOHOO! I archived it and accessed the artifact url. - devwannabe
@devwannabe By "custom variable" do you mean job's parameter or variable from build step? - luka5z
Please add your solution or workaroind as answer, so others can learn from it. - Dominik Gebhart

2 Answers

1
votes

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
0
votes

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.