简体   繁体   English

Jenkins根据多个项目中的提交触发构建

[英]Jenkins trigger a build based on the commit in multiple projects

Recently I have started working on CI project, It has to build the project on SCM commit (Git/SVN). 最近我开始研究CI项目,它必须在SCM提交(Git / SVN)上构建项目。 I tried using Build trigger remotely and it is triggering the Job when I commit the code and building the application successfully. 我尝试远程使用Build触发器,当我提交代码并成功构建应用程序时,它会触发Job。

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 post-commit-hook-jenkins.vbs文件的代码

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 Post-commit.bat文件的代码

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

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

If you 如果你

  • set up the SVN/git location of the project under the "Source Code Managment" section of the job configuration 在作业配置的“源代码管理”部分下设置项目的SVN / git位置
  • and then choose "Poll SCM" in the "Build Triggers" section 然后在“构建触发器”部分中选择“轮询SCM”

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. 避免这种情况,而是让Jenkins处理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? 或者你可能只是想避免轮询,这个问题是关于如何从SVN post-commit钩子中触发不同的项目,具体取决于你提交的SVN存储库中的位置? 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. 在这种情况下,您需要编写一个更复杂的钩子脚本,该脚本分析受提交影响的路径,并根据该脚本触发正确的Jenkins作业。

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. 在post-commit.bat文件中,您应该传递提交的存储库和修订号而不是作业名,因为您还不知道要触发哪个作业。 Subversion passes the repository and revision as the first and second argument. Subversion将存储库和修订版作为第一个和第二个参数传递。

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 在.vbs脚本中,您应该替换从参数中获取jobName的语句

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. 现在,您需要使用此修订号来使用svnlook.exe检查更改。 Put the absolute path to svnlook.exe in the svnlook variable and execute something like this: 将svnlook.exe的绝对路径放在svnlook变量中并执行如下操作:

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. 现在,您已在changed变量中changedsvnlook.exe的输出。 It tells you which files in the SVN repository were affected by the commit. 它告诉您SVN存储库中的哪些文件受提交影响。

Next, it is up to you to parse the content of the changed variable to decide on the job name to trigger. 接下来,由您来解析已changed变量的内容以决定要触发的作业名称。 For example, you could do something simple like check whether it contains " foo/trunk/" and then trigger the foo-trunk job. 例如,您可以执行一些简单的操作,例如检查它是否包含“foo / trunk /”,然后触发foo-trunk作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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