feature to allow jobs started by jenkins to access their job urls without authentication

149 views
Skip to first unread message

vimil saju

unread,
Mar 31, 2011, 10:27:30 PM3/31/11
to jenkins...@googlegroups.com, jenkin...@googlegroups.com
Hi,

Here is the situation,

I wanted to generate changelog.xml for my project from an ant script that executes as one of the  build steps of a job. To do this I needed the time of the last successful build, which i found I could get by invoking the job's lastSucessfulbuild/api/xml url. However since I have enabled authentication, I need to pass the username and password to this url to get the xml response containing the last successful build time. I have setup jenkins to use active directory for authentication, and I don't like the idea of storing  the username and password in a properties file because if the password is changed in active directory then I will have to remember to change it in the properties file too. 

I was wondering if jenkins could somehow allow jobs that were started by it to access its job's urls without authentication for the duration of the build. One way I think this feature could be implemented is that jenkins could pass a unique token to the job through an environment variable which the job has to then pass back to jenkins through the url to access the response. 

I would be grateful if someone could suggest another way I could get the last successful build time for a job without storing credentials in a properties file.

Thanks
Vimil

vimil saju

unread,
Apr 1, 2011, 12:28:00 PM4/1/11
to Ben Castellucci, jenkin...@googlegroups.com, jenkins...@googlegroups.com
Thanks Ben, 

I think this will help me a lot. If I am not mistaken the groovy script runs before the ant build step right?

Vimil

On Fri, Apr 1, 2011 at 3:01 AM, Ben Castellucci <ben...@gmail.com> wrote:
I recently had a similar need and was able to get what I needed with the Groovy plugin (not the Groovy postbuild plugin) and a quick groovy script (seacrh the 'jenkinsci-dev' group for 'Last Successful BUILD_ID'). The script is listed below.
 
This would need to execute as a system groovy script (you'll see that once you install the plugin) and there is a quirk with that - it will only run on the master. I have not tested it in a master/slave situation. Anyway, basically this will 'export' the last successful build id (which is a date/time string in a known format) as a build parameter in the current run, accessable to subsequent build steps as an env var just like $BUILD_ID is.
 
You can retrieve more info than that, if needed. For example you can directly acess the ChangeLogSets from the current build and the last successful build, just do currentBuild.getChangeSets().getItems() or lastSuccussfulBuild.getChangeSets().getItems() and iterate. See hudson.model.AbstractBuild, hudson.model.Job and hudson.model.Run in the core for more info.
 
Thanks,
Ben
 
import hudson.model.AbstractBuild;
import hudson.model.ParametersAction;
import hudson.model.StringParameterValue;
 
//I suspect that if your job was started on a slave and this is running in the master then you would need to do somethin like Hudson.getInstance().getItem(jobName).something where 'something' is the way to get hold of current-running build (see the groovy script plugin page on the wiki as it has a lot of useful stuff in the comments at the bottom, like how to do just that). Never tried it - I only run on the master.
AbstractBuild currentBuild = (AbstractBuild) Thread.currentThread().executable;
 
//I use lastStableBuild as it will return only the lst succesful build, lastSuccessfulBuild includes both successful and stable ones. See hudson.model.Job and/or hudson.model.Run in the core for more info.
def lastStableBuild = currentBuild.getParent().getLastStableBuild();
 
StringParameterValue x = null;
 
if(lastStableBuild != null) {
 String s = lastStableBuild.getId();
 println "This is the last stable build id: " + s;
 x = new StringParameterValue("LAST_STABLE_BUILD_ID",s);
} else {
 x = new StringParameterValue("LAST_STABLE_BUILD_ID","null");
}
 
ParametersAction y = new ParametersAction(x);
currentBuild.addAction(y);
 
//check to see if it is there...
def envVars= currentBuild.properties.get("envVars");
println "This is the last stable build id: " + envVars["LAST_STABLE_BUILD_ID"];

Reply all
Reply to author
Forward
0 new messages