Trigger Hudson build on SVN commit
Asked Answered
H

4

12

I have already set up a version control system (Subversion) which is going to be used by developers to commit and update their code (let's say that its address is https://server/svn/project). Only authenticated users will be able to have access to project's SVN.

On the other hand, I have installed Hudson as the project's continuous integration server (address of the project is server:8080/job/project).

I'd like to achieve the following:

  • Hudson builds to be triggered automatically when there is an SVN commit.
  • Mails are sent to the respective developers (the ones that committed code) when their committed code doesn't build, meaning that when user A commits code that doesn't build, only user A gets an email containing the notification.

I have set up matrix-based authorization for Hudson users, because I don't want to be open to anyone.

I have seen some suggestions for post-commit hooks, but none of them worked until now.

Can somebody suggest what to do regarding both issues? Specific steps would be appreciated.

Hoeve answered 25/1, 2011 at 10:39 Comment(4)
I think hudson needs read-access to your repo. ICBWT.Absent
Why didn't the post-commit hook work? Authentication? You can also set it up to periodically poll the repository for changes.Bedwarmer
when commiting sample code to svn, a "'post-commit' hook failed with error output" returned.Hoeve
@Hoeve : Do you found a solution for your problem ? I hope you have done it.Chattanooga
T
6

According to "Building a software project # Builds by changes in Subversion/CVS", Hudson needs to poll your SVN repo in order to detect changes and trigger the build.

However, that can be initiated on each commit from the SVN, like in this thread.
The official script is at the Subversion Plugin page.

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/hudson/subversion/${UUID}/notifyCommit?rev=$REV

But it is specified:

For this to work, your Hudson has to allow anonymous read access to the system.
If access control to your Hudson is more restrictive, you may need to specify the username and password, depending on how your authentication is configured.

Tramline answered 25/1, 2011 at 11:44 Comment(3)
so in my case, the "server/hudson/subversion" part of the url in the last line, what would it be?Hoeve
i have also tried this suggestion (borys.name/blog/build_with_hudson_on_subversion_commit.html) which suits to my case, but didn't work either. perhaps i am messing with what needs to be replaced in the provided URL.Hoeve
@canadiangeo: If I read wiki.hudson-ci.org/display/HUDSON/Subversion+Plugin, you only replace server by your server (in your case: http://server:8080), but you keep /hudson/subversion/...Tramline
V
4

To trigger a build when there is a commit in SVN you have to (1) set your hudson job to build remotely and (2) make a SVN hook...

the first part is fairly straightforward... to make the hook go to /var/lib/svn//hooks and rename post-commit.tmpl to post-commit there you can do something like

#!/bin/bash
# Este script comprueba si se han hecho cambios en un directorio concreto,
# y en tal caso lanza una build en Jenkins

REPOS="$1"
REV="$2"
JENKINS_JOB="$3"
JENKINS_USER=admin
JENKINS_PASSWORD=****
JENKINS_HOST=<hostname>

if [ -n $(svnlook dirs-changed $REPOS --revision $REV | fgrep "tags\/") ];then
 wget --quiet --auth-no-challenge --no-check-certificate --http-user=$JENKINS_USER --http-password=$JENKINS_PASSWORD http://$JENKINS_HOST/job/$JENKINS_JOB/build?token=TOKEN
fi

exit 0

Look at this article http://blogsyntagma.blogspot.com.ar/2012/04/hook-de-subversion-para-ejecutar-un-job.html (it's in spanish)

Vehement answered 10/4, 2012 at 13:35 Comment(0)
D
1

Here are the required steps:

  • Create a SVN user that Hudson can use to gain read-only access to your repository
  • Configure Hudson to use this SVN user when accessing the repository
  • Create a new job to use the repository at a specified address (i.e. a specific branch)
  • Configure your job to poll the repository at least once a minute for any changes
  • Configure your job to build what is required
  • Configure your job to send an e-mail on build failure

I would recommend e-mailing all developers so that they are notified that the build is unstable, not just the culprit. Not only does this provide more visibility, but it will motivate the culprit to fix the problem immediately or otherwise accept a scolding from their fellow developers. Trust me, this is effective.

Donkey answered 25/1, 2011 at 17:34 Comment(2)
thanks for your answer. perhaps you are right that it would be more effective if every developer gets notified in unstable builds. however, i think that your suggestion in step 3 is the easy one. :) i would like to have build triggered when there is something committed in svn, instead of polling the repository every minute. i guess that a lot of time would be required in this case, and possible conflicts (when 2 developers commit their code at the same time) could arise. or am i wrong?Hoeve
I still suggest configuring Hudson to poll the repository. That way, you control and configure your builds in one place. You could add post-commit hooks to SVN if you'd like, but I haven't had any issues with using the steps I outlined above.Donkey
A
0

This is how I got Jenkins 2.157 to start a build after a commit to an SVN repo.

1. Allow read access in Jenkins

Using Jenkins' web interface, go to Manage JenkinsConfigure Global Security and check Allow anonymous read access:

allow anonymous read access screenshot

If you skip this step, you'll get the following response when trying to trigger a build using an HTTP request (described in step three):

Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:

Permission you need to have (but didn't): hudson.model.Hudson.Read
... which is implied by: hudson.security.Permission.GenericRead
... which is implied by: hudson.model.Hudson.Administer
-->

2. Configure your build trigger

Still in Jenkins' web interface, go to your build job and define that you want to trigger a build using a script (this is going to be the SVN commit hook in the next step):

configure build trigger

3. Create the post-commit hook

Finally, go to your repository's hooks directory and add a shell script named post-commit (the name is important, otherwise SVN won't execute it after a commit):

#!/bin/sh

# Name of the Jenkins build job
yourJob="your_job"

# You defined this in Jenkins' build job
build_token="yourSecretToken"

jenkins_address_with_port="localhost:8090"

curl $jenkins_address_with_port/job/$yourJob/build?token="$build_token"

Make the script executable: chmod +x post-commit.


Here's an extended version of post-commit that logs data about the commit, such as the commit's author.

#!/bin/sh

# The path to this repository
repo_path="$1"
# The number of the revision just committed
rev="$2"
# The name of the transaction that has become rev
transaction_name="$3"

# See http://svnbook.red-bean.com/en/1.7/svn.ref.svnlook.c.author.html
commit_author="$(svnlook author --revision $rev $repo_path)"
# The UUID of the repository, something like e3b3abdb-82c2-419e-a100-60b1d0727d12
repo_uuid=$(svnlook uuid $repo_path)

# Which files were changed, added, or deleted. For example:
# U   src/main/java/com/bullbytes/MyProgram.java
what_has_changed=$(svnlook changed --revision $rev $repo_path)

log_file=/tmp/post_commit.log

echo "Post-commit hook of revision $rev committed by $commit_author to repo at $repo_path with ID $repo_uuid was run on $(date). Transaction name: $transaction_name. User $(whoami) executed this script. This has changed: $what_has_changed" >> $log_file

# Name of the Jenkins build job
yourJob="your_job"

# You defined this in Jenkins' build job
build_token="yourSecretToken"

jenkins_address_with_port="localhost:8090"

curl $jenkins_address_with_port/job/$yourJob/build?token="$build_token"

To learn more about commit hooks, head to the docs.

Ats answered 14/1, 2019 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.