Jenkins CI: How to trigger builds on SVN commit
Asked Answered
O

4

80

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.

Overreact answered 4/4, 2012 at 15:25 Comment(1)
possible duplicate of Trigger hudson build when svn commitDruggist
T
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).

Tendinous answered 4/4, 2012 at 15:32 Comment(7)
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.Ingest
@Ingest 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.Tendinous
@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?Artemis
@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.Tendinous
@mxdsp Updated the link. It isn't as pretty as the old wandisco one, but it will get the job done. Thank you!Tendinous
2nd link (commit hooks) isn't workingAdis
@Adis Well, it only took them 10 years to break the link. It's updated! Thank you.Tendinous
R
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
Rik answered 18/10, 2017 at 6:31 Comment(1)
This answer is more or less identical to this answer.Incommunicable
C
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.

Color answered 19/8, 2015 at 14:50 Comment(0)
D
-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.

Diaphragm answered 1/4, 2016 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.