Home > Development, English, Tech Crunch, Techie > Automatically updating a working copy on a remote server using Mercurial

Automatically updating a working copy on a remote server using Mercurial


Imagine you have two servers: on one server, you have a Mercurial repository with all your code, accessible through http. On the other one, you have a working copy of this code that you are using to test a website. Let’s call the first server REPOS and the second one DEVEL. This article describes how to automatically update the working copy on DEVEL, after an hg push on REPOS.


In order to do this, the idea is to write a Mercurial hook that will execute after a hg push on REPOS and will connect through ssh to DEVEL in order to issue the necessary hg pull and hg update. It’s fairly easy to do, but it needs some setup on both DEVEL and REPOS in order to work.

Setting up DEVEL

The following steps will be needed in order to setup DEVEL:

  1. Create a user named hgagent on DEVEL. The account of this user will be used to connect by ssh from REPOS in order to do the hg pull and hg update:
    sudo adduser hgagent
  2. If REPOS requires a username and password (or an SSH key) in order to do an hg pull, configure the auth section of the hgrc file of the hgagent user you just created in order to allow it to do an hg pull without needing to provide his username and password interactively
  3. Do an hg clone of the repository you want to update using the hgagent user (this assumes, obviously, that the repository is already accessible on REPOS), so that the files in the clone belong to hgagent, which will therefore be able to do hg pull and hg update

Setting up REPOS

The following steps are needed in order to set up REPOS:

  1. If your repository is not publicly accessible, you will need to set it up so that hgagent can access it using a username and password or an SSH key
  2. You will need to generate a pair of SSH keys, in order to allow a non-interactive connection to DEVEL. If your repository uses http or https, I strongly advise you to generate these SSH keys as the user under which your http server runs (most probably www-data), as when the hook is executed, it will be executed under the www-data user (or whatever user your http server runs on). You can use sudo -u www-data ssh-keygen in order to generate the keys. Note that you shouldn’t create a passphrase for your SSH key.
  3. Once the keys are generated, you need to copy the public key to the /home/hgagent/.ssh/authorized_keys. You can use the following command in order to do so:
    ssh-copy-id -i /var/www/.ssh/id_dsa.pub hgagent@DEVEL
  4. Finally, last but not least, you will need to create the hook in the repository on REPOS. In the .hg/hgrc file of the repository, add the following lines:
    changegroup = ssh hgagent@DEVEL "cd /path/to/working/copy; hg pull; hg update"


That’s it ! When you issue a hg push on REPOS, it should now automatically connect by ssh to the DEVEL server and update your working copy.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: