简体   繁体   中英

Build Particular Commit with Jenkins API

I want to be able to ask Jenkins to do a build on a particular commit through the API.

The best approach I can find so far would be to use an "api-build" branch and force push the desired commit there, and then trigger a build in the typical way. I would be surprised if there isn't something better, but I haven't been able to find it myself so far. What's the "right" way to do this (if there is one)?

You can use git hooks in conjunction with Jenkins remote builds to accomplish this.

I use the post-receive hook for this type of thing.

In the code to the hook (it's a shell script, but you can have it wrap something else, such as a Python script), you can examine the last commit (keep merges in mind, in which case you'd have to go back to the last non-merge commit to find the actual last commit) to see if it's the type of commit on which you want to trigger a build (ie contains changes within a particular subdirectory).

The changeset can be obtained through standard git command line tools, and then regexes used to see if the changeset contains any paths of interest.

Once it's determined that a build should be triggered, you can shell out to curl to hit the URL necessary to trigger the Jenkins remote build (remotely triggered builds need to be enabled on the job, and, optionally, a token set).

For more info on remote builds, see: https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

Edit in response to comment from OP:

If you use the post-receive hook, I believe it will work no matter what the branch or commit (although I have personally only done this with master in mind). It's specific to the *git r*epo*, since the post-receive hook code lives inside of that repo. You can control, via the code that the hook wraps, how you determine if you want to trigger a build based on what has changed. This hook code would run with every git push .

Edit in response to follow-up comment from OP:

I don't believe this would trigger when there are no changes, as git wouldn't actually do a push in that case. If you want to trigger a build when there are no changes at all, then a curl via the API as I mentioned previously would work -- a build can always be triggered at any time regardless of changes via a curl command if remote builds are enabled on the job.

In terms of telling Jenkins what to build, that is done by the Build > Execute shell Command , where you can specify the build action, such as running a particular shell script, or an ant command, etc.

I don't think you would want to create new jobs via the hook (offhand, I don't think that's actually something you can do via the API -- it works with existing jobs, IIRC).

Edit with some additional info:

In terms of specifying which branch to build on, that's something that I don't think can be arbitrarily specified, even through a git hook. You can specify multiple branches to build on in the SCM section of the configuration on the Jenkins job, however.

So if you had a set of branches you wanted to build across on each commit, you could define multiple branches in that section, and then it would build on each of them (although when a given build was triggered, presumably only one branch would have that commit).

It may be more practical to clone a handful on Jenkins jobs, one for each branch, which would build just that branch, and then the git hook can target just that job based on the branch of the commit, instead of building across a bunch of branches, most of which the commit wouldn't apply to.

While that doesn't get you to arbitrary branches, it would be a way to do a handful of them, say, master , dev , test , or something along those lines (or perhaps a handful of release versions).

If you truly need arbitrary branches, you could look at using the Job DSL Plugin ( https://wiki.jenkins-ci.org/display/JENKINS/Job+DSL+Plugin ) which is supposed to allow for very specific control over job creation. That could be done from within the git hook by invoking another script (the DSL uses Groovy ).

I have not tried this plugin myself, but it sounds promising. It also facilitates the use of templates, which can then be specified, which also sounds like it might be good for your case, where the branch, say, would vary, but much of the other content would be fairly static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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