3
votes

Recently I have started working on CI project, It has to build the project on SCM commit (Git/SVN). I tried using Build trigger remotely and it is triggering the Job when I commit the code and building the application successfully.

Now I have multiple projects in a single repository, So based on my commit it has to find the respective project in the repository and start executing the specific job.

Is there any way to do this in Jenkins?

Code for post-commit-hook-jenkins.vbs file

Set args = WScript.Arguments
JobName = args.Item(0)
Token = args.Item(1)

' URL to open
sUrl =  "http://builduser:a844e9e505bfc5e6e9ce6e953ba5443a@localhost:8080/buildByToken/build?job=" + JobName + "&token=" + Token

' POST Request to send.
WScript.Echo "sUrl: " + sUrl
sRequest = ""

HTTPPost sUrl, sRequest

Function HTTPPost(sUrl, sRequest)
    set oHTTP = CreateObject("Microsoft.XMLHTTP")
    oHTTP.open "POST", sUrl,false
    oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    oHTTP.setRequestHeader "Content-Length", Len(sRequest)
    oHTTP.send sRequest
    HTTPPost = oHTTP.responseText
End Function 

Code for Post-commit.bat file

SET CSCRIPT=%windir%\system32\cscript.exe
SET VBSCRIPT=E:\Repositories\CICD\hooks\post-commit-hook-jenkins.vbs

"%CSCRIPT%" "%VBSCRIPT%" "JobName" "AuthenticationToken"
1

1 Answers

3
votes

If you

  • set up the SVN/git location of the project under the "Source Code Managment" section of the job configuration
  • and then choose "Poll SCM" in the "Build Triggers" section

then it should work pretty much as you want.

The fact that you are asking this question probably means that you are doing the source code checkout from the build script, right? Avoid this and instead let Jenkins handle the svn checkout/git clone.

Or maybe you are just trying to avoid polling, and this question is about how to trigger different projects from the SVN post-commit hook depending on where in an SVN repository you committed? In that case you need to write a more sophisticated hook script which analyzes the paths affected by the commit and triggers the correct Jenkins job(s) based on that.

edit:

In your post-commit.bat file, you should pass on the repository and revision number of the commit instead of the job name, as you don't know yet which job to trigger. Subversion passes the repository and revision as the first and second argument.

SET CSCRIPT=%windir%\system32\cscript.exe
SET VBSCRIPT=E:\Repositories\CICD\hooks\post-commit-hook-jenkins.vbs
SET REPOS=%1
SET REVISION=%2

"%CSCRIPT%" "%VBSCRIPT%" %REPOS% %REVISION% "AuthenticationToken"

In your .vbs script, you should then replace the statement where the jobName is taken from an argument by

repos = args.Item(0)
revision = args.Item(1)
token = args.Item(2)

Now you need to use this revision number to inspect the changes with svnlook.exe. Put the absolute path to svnlook.exe in the svnlook variable and execute something like this:

Set changedExec = shell.Exec(svnlook & " changed --revision " & revision & " " & repos)
Do Until changedExec.StdOut.AtEndOfStream
    changed = changed + changedExec.StdOut.ReadLine() + Chr(10)
Loop

Now you have the output of svnlook.exe changed in the changed variable. It tells you which files in the SVN repository were affected by the commit.

Next, it is up to you to parse the content of the changed variable to decide on the job name to trigger. For example, you could do something simple like check whether it contains " foo/trunk/" and then trigger the foo-trunk job.