Drupal 7: Implementing a Subversion pre-commit hook to integrate with Drupal
In this article I’ll show an example of how to implement a Subversion pre-commit hook to integrate with Drupal. Pre-commit hooks can be used to execute any arbitrary code, such as deployment procedures, archiving databases, etc. For this example, I will show how to check for the creation of a subversion tag and archive the database.
To get started I created a local subversion repository.
Upon creating a new local svn repository, a hooks directory will be created. Example: /var/subversion/project/hooks
Inside this directory will be a bunch of sample scripts ending in “.tmpl” which contain example hook scripts. Here are the contents of the example pre-commit hook without comments:
As noted in the pre-commit.tmpl file, there are 2 arguments being passed to the pre-commit script:
I created a new file called “pre-commit” and added the following contents:
I then made the file executable.
The above script simply passes the arguments to a PHP script contained with the Drupal project.
In my scripts folder (/var/www/vhosts/project.vm/scripts), I created the PHP script “svn-pre-commit.php” with the following contents:
I also made this script executable:
Now assuming that my Drupal site is integrated with the subversion repository, and development is at a point to deploy/create a new tag, I executed the following command to create the subversion tag:
To verify, I entered the directory containing my database dumps to checkout the result: