Brian Wigginton libB – Notes & Tutorials on Delicious Bytes of Technology

Subversion: Continuous Integration with a PHP Application

Posted on October 27, 2009

Abstract

This tutorial explains how to update a staging site whenever there is a commit to the SVN repository. This assumes basic knowledge of Subversion, Apache and C and that the staging site is running a working copy checked out from the repository.

Problem & Solution

When working close with designers or other non-coders it's often most productive to be able to make quick changes live to the box serving up the dev site, then to commit a change then update the server over and over. The bigger issue is that there is now modified code on the dev site that's not revisioned with subversion. To solve this problem we are going to update the codebase for the development site whenever anyone commits changes to the subversion repository.

update_svn

update_svn is a C program that will make a call to the svn command with our designated username and password and update the working copy. This program should live inside your applications directory.

update.c

#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
     execl("/usr/bin/svn", "svn", "update", "--username",
     "USERNAME", "--password", "PASSWORD",
     "/var/www/website/"(const char *) NULL);
     return(EXIT_FAILURE);
}

Compile and add permissions

gcc svn_update.c -o /var/www/website/update_svn
chmod +s update_svn

Subversion post-commit hook

In our subversion repository's post-commit script we want to add a call to update_svn. Add the following line to your post-commit script:

/var/www/website/update_svn

Resources:

Filed under: PHP, Subversion No Comments

Subversion: Repository on Subdomain

Posted on October 24, 2009

This is a tutorial on how to setup a Subversion repository on a subdomain with Apache. This assumes you have Subversion and Apache already installed on your system.

Subversion Setup

First you need to create a repository somewhere in your file system. Then grant apache permissions on that directory.

svnadmin create /var/svn/repository
sudo chown -R www-data:www-data /var/svn/repository

Controlling Access

Access to the repo via the web will be controlled by an htpasswd file located at /var/svn/svn-auth-file. Use the htpasswd command to create the file.

htpasswd -c /var/svn/svn-auth-file <username>

Execute the script again without the -c argument to add more people to the list.

htpasswd /var/svn/svn-auth-file <username_two>

Apache Setup

I usually setup Apache to use Names VirtualHosts to handle multiple websites. We'll make a new named virtualhost for subversion repository.

<VirtualHost *:80>
ServerName svn.your_domain.com
<location />
DAV svn
SVNPath /var/svn/repository

AuthType Basic
AuthName "Subversion repository"
AuthUserFile /var/svn/svn-auth-file
Require valid-user
</location>
</VirtualHost>

Subversion: Commit Emails

Posted on October 24, 2009

This is a tutorial on how to setup Subversion to email a team when there is a commit to the repository.

Subversion Hooks

Hooks are what Subversion executes upon certain events. Within your SVN directory you should see the following items

  • README.txt
  • conf
  • dav
  • db
  • format
  • hooks
  • locks

Go into the hooks directory and you will see a bunch of files ending in .tmpl. These are template scripts that are prebaked for you. Within each hook file are calls to scripts you want to be executed. To get one to execute you need to remove the .tmpl extension and then make it executable.

mv post-commit.tmpl post-commit
chmod +x post-commit

The Post Commit Hook & commit-email.pl

In post-commit there is a call to commit-email.pl. (Be sure that this calls to the absolute path of the script)

I didn't have commit-email.pl on my server but acquired it from the Subversion Tools Repository here.Save that file to your hooks directory. Also, be sure to edit that file and rename all the @SVN_BINDIR@ instances to the directory of you subversion executable. To find out type which svn and use the path that returns. Mine returned /usr/bin/svn to I used /usr/bin/ for @SVN_BINDIR@.

Run commit-email.pl without any arguments to see a list of options. To test this script point it to a repository and a revision number.

./commit-email.pl /var/svn/repository 1 --from "Subversion Gate Keeper <subversion@example.com>" youremail@example.com anotheremail@example.com

I like to prepend a subject line prefix so I can filter it a little easier when it comes through to my email client. Adding the -s argument to the command will allow you to specify a subject line prefix.

./commit-email.pl /var/svn/repository 1 --from "Subversion Gate Keeper <subversion@example.com>" -s "Commit Activity: " youremail@example.com anotheremail@example.com

Here are some more resources to help you out.

Edit - October, 27: You have to call your scripts within post-commit by their absolute path or else they wont run. Code above has been modified.

Tagged as: No Comments