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 Jenkins
→ Configure Global Security
and check Allow anonymous read access
:
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):
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.