Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger build in Jenkins immediately after the code check-in

Tags:

svn

jenkins

How do I trigger the build in Jenkins once the code is committed?

  1. Jenkins is installed and the build is can be triggered manually.
  2. Below are the SVN version details.

    • TortoiseSVN 1.8.1, Build 24570 - 32 Bit , 2013/07/22 18:28:29
    • Subversion 1.8.1, -release
    • apr 1.4.8
    • apr-util 1.5.2
    • serf 1.3.0
    • OpenSSL 1.0.1e 11 Feb 2013
    • zlib 1.2.8
  3. Jenkins URL: "localhost:8080/job/TestProject/"

like image 429
user2077937 Avatar asked Feb 27 '14 01:02

user2077937


1 Answers

To have the build triggered immediately once the code is committed, you'll need to have something notifying Jenkins.

However you can configure your job to poll your SCM regularly (at my shop we poll our Mercurial repositories every 5 minutes) for new changes and build if there are new changes.

For this, in the Build Triggers section, you check the Poll SCM checkbox, and fill in a cron expression in the textarea. For polling every 5 minutes, we use

H/5 * * * *

Note that we used to use

*/5 * * * *

but replacing the left * by the H allows Jenkins to avoid polling for all projects at the same time (it will then poll every 5 minutes, but not necessarily at 0, 5, 10, etc. minutes.

One can also trigger builds remotely by checking Trigger builds remotely and specifying a token in the textbox. Then a simple contact from a script to the relevant URL will trigger the build.

JENKINS_URL/job/JOB_NAME/build?token=TOKEN_NAME

(This option might require some particular permissions to set up, or some particular global configuration, as it seems it's not available to OP.)

For the post-commit hook solution, see Jenkins' Subversion Plugin documentation, with the important parts below:

Jenkins can poll Subversion repositories for changes, and while this is reasonably efficient, this can only happen up to every once a minute, so you may still have to wait a full minute before Jenkins detects a change.

To reduce this delay, you can set up a post commit hook so the Subversion repository can notify Jenkins whenever a change is made to that repository. To do this, put the following script in your post-commit file (in the $REPOSITORY/hooks directory):

REPOS="$1"
REV="$2"
UUID=`svnlook uuid $REPOS`
/usr/bin/wget \
  --header "Content-Type:text/plain;charset=UTF-8" \
  --post-data "`svnlook changed --revision $REV $REPOS`" \
  --output-document "-" \
  --timeout=2 \
  http://server/subversion/${UUID}/notifyCommit?rev=$REV

(It is really recommended to read the full documentation for information related to the configuration or for a more robust script.)

like image 135
Gorkk Avatar answered Oct 07 '22 14:10

Gorkk