User Tools

Site Tools


scm:svn:hook_scripts

SVN Hook Scripts

Read more about hook scripts: hook scripts in redbean (SVN book)

Short how to

On the Subversion server

Go to your repository location, here you will find the “hooks” subfolder:

hooks/
post-commit.tmpl
post-lock.tmpl
post-revprop-change.tmpl
post-unlock.tmpl
pre-commit.tmpl
pre-lock.tmpl
pre-revprop-change.tmpl
pre-unlock.tmpl
start-commit.tmpl

Edit the different hooks to execute commands or programs, whatever you want…

Example for a post-commit hook:

  1. cp post-commit.tmpl to post-commit
  2. edit the script and execute what ever you want
  3. chmod ugo+x post-commit

Restrictions

  • :!: No environment variables are set, you have to give absolute paths.
  • :!: Commands are execute as the svn user (i.e. Apache)

Example to make a update on a remote server with every svn commit

post commit

  • Execute a ruby script:
export RUBYOPT="rubygems"
/usr/bin/ruby /srv/svn/repos/example.com/hooks/remote-co-svn.example.com.rb

remote-co-svn.example.com.rb

  • Here is the ruby script which logs in to the test server and executes a bash script:
#!/usr/bin/env ruby
 
###############################################################
#
# Ruby Script for logging into a remote server per SSH
# and execute a "svn update" command
#
# used for svn hooks: post-commit
#
# Nik Wolfgramm | 21.02.2008, created
# Nik Wolfgramm | 18.06.2008, updated for certificate auth
#
###############################################################
 
require 'rubygems'
require 'net/ssh'
 
# define home for .ssh (pub key  authentication)
ENV['HOME'] = "/srv/svn/shared_scripts"
 
def remote_svn_update(host, user, dir)
    Net::SSH.start(host, user, :auth_methods => [ "publickey" ], :verbose => :fatal) do |ssh|
        puts ssh.exec!("#{dir}/svn_update.sh\n")
    end
end
 
# execute the command
remote_svn_update('192.168.63.233', 'root', '/mnt/srv/www/example.com/')
  • The corresponding bash script looks like this:
#!/bin/bash
cd /mnt/srv/www/example.com
sudo -u apache svn up --username admin-reader --password "<password>" --config-dir ./htdocs/.svn/ ./htdocs/
chown -R ftpexample:example ./htdocs
find htdocs/ -name "*.svn" -exec chown -R apache:apache '{}' \;
chmod 777 ./htdocs/templates_c ./htdocs/cache ./htdocs/cache/png ./htdocs/cache/xml

NET:SSH certificate authentication

The script above uses certificate authentication per default (if no password defined).

This script works only if the sudo settings have been adapted

If you get the following error message…

sudo: sorry, you must have a tty to run sudo

Uncomment the following line with visudo:

#Default requiretty
/srv/wiki.niwos.com/data/pages/scm/svn/hook_scripts.txt · Last modified: 2009/08/15 12:14 (external edit)