79

What plugins and plugin features do I need to set in order to get my Jenkins job to trigger a build any time code is committed to an SVN project?

I have installed both the standard SVN plugin as well as the SVN tagging plugin, but I do not see any new features that allow trigger configuration.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 1
    possible duplicate of [Trigger hudson build when svn commit](http://stackoverflow.com/questions/4792466/trigger-hudson-build-when-svn-commit) – highlycaffeinated Apr 04 '12 at 15:28

4 Answers4

89

There are two ways to go about this:

I recommend the first option initially, due to its ease of implementation. Once you mature in your build processes, switch over to the second.

  1. Poll the repository to see if changes occurred. This might "skip" a commit if two commits come in within the same polling interval. Description of how to do so here, note the fourth screenshot where you configure on the job a "build trigger" based on polling the repository (with a crontab-like configuration).

  2. Configure your repository to have a post-commit hook which notifies Jenkins that a build needs to start. Description of the plugin here, in the section "post-commit hooks"

The SVN Tag feature is not part of the polling, it is part of promoting the current "head" of the source code to a tag, to snapshot a build. This allows you to refer to Jenkins buid #32 as SVN tag /tags/build-32 (or something similar).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • post-commit hooks rock. Faster response times, and (at scale) your CI system doesn't crush the source repo with hundreds of change log requests. – EricMinick Apr 05 '12 at 15:22
  • 5
    @EricMinick They are great, but sometimes they can melt the CI server, if you get a lot of small commits in a short period of time. It is all horses for courses, and it is hard to know which method is favourable without analysing how your dev process is currently functioning. – Edwin Buck Apr 05 '12 at 15:27
  • 1
    @EdwinBuck: Do you know if there is a way to configure Jenkins to build only selective commits? I will explain, I have a tortoiseSVN, and a Jenkins project which keeps polling the repository. Now if I wish that the commit I make should not trigger Jenkins, is there a way to do that? – SandBag_1996 Jan 05 '15 at 20:04
  • @UnderDog If you have matured to the point where you have a post-commit trigger in maven, then I prefer a solution which looks for special "key words" in the post-commit trigger and upon discovering them, opts to NOT tell the Jenkins server to launch a new build. For example "^DONTBUILD -.*" might be used as a "dont build" flag if provided in the first line of the commit. – Edwin Buck Jan 05 '15 at 22:23
  • @mxdsp Updated the link. It isn't as pretty as the old wandisco one, but it will get the job done. Thank you! – Edwin Buck Mar 06 '19 at 13:24
  • 2nd link (commit hooks) isn't working – EvilSmurf Nov 03 '21 at 09:40
  • 1
    @EvilSmurf Well, it only took them 10 years to break the link. It's updated! Thank you. – Edwin Buck Nov 03 '21 at 13:17
2

You need to require only one plugin which is the Subversion plugin.

Then simply, go into Jenkins → job_name → Build Trigger section → (i) Trigger build remotely (i.e., from scripts) Authentication token: Token_name

Go to the SVN server's hooks directory, and then after fire the below commands:

  1. cp post-commit.tmpl post-commit
  2. chmod 777 post-commit
  3. chown -R www-data:www-data post-commit
  4. vi post-commit

    Note: All lines should be commented Add the below line at last

Syntax (for Linux users):

/usr/bin/curl http://username:API_token@localhost:8081/job/job_name/build?token=Token_name

Syntax (for Windows user):

C:/curl_for_win/curl http://username:API_token@localhost:8081/job/job_name/build?token=Token_name
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hemin Patel
  • 153
  • 1
  • 5
  • This answer is more or less identical to [this answer](https://stackoverflow.com/questions/29432076/how-do-i-trigger-jenkins-build-with-svn-post-commit/46885434#comment89791853_46885434). – Peter Mortensen Jul 18 '18 at 21:01
1

I made a tool using Python with some bash to trigger a Jenkins build. Basically you have to collect these two values from post-commit when a commit hits the SVN server:

REPOS="$1"
REV="$2"

Then you use "svnlook dirs-changed $1 -r $2" to get the path which is has just committed. Then from that you can check which repository you want to build. Imagine you have hundred of thousand of projects. You can't check the whole repository, right?

You can check out my script from GitHub.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1

You can use a post-commit hook.

Put the post-commit hook script in the hooks folder, create a wget_folder in your C:\ drive, and put the wget.exe file in this folder. Add the following code in the file called post-commit.bat

SET REPOS=%1   
SET REV=%2

FOR /f "tokens=*" %%a IN (  
'svnlook uuid %REPOS%'  
) DO (  
SET UUID=%%a  
)  

FOR /f "tokens=*" %%b IN (  
'svnlook changed --revision %REV% %REPOS%'  
) DO (  
SET POST=%%b   
)

echo %REPOS% ----- 1>&2

echo %REV% -- 1>&2

echo %UUID% --1>&2

echo %POST% --1>&2

C:\wget_folder\wget ^   
    --header="Content-Type:text/plain" ^   
    --post-data="%POST%" ^   
    --output-document="-" ^   
    --timeout=2 ^     
    http://localhost:9090/job/Test/build/%UUID%/notifyCommit?rev=%REV%    

where Test = name of the job

echo is used to see the value and you can also add exit 2 at the end to know about the issue and whether the post-commit hook script is running or not.

Megha
  • 1