How to use Subversion to manage a live site
(This tutorial relates specifically to my experiences with my host, MediaTemple, but the basics should be the same whoever you host with.)
Following a presentation by Juan Delgado at Refresh Cambridge last week, I resolved to update my workflow on freelance projects to include some sort of version control (VC). Juan made the point that running a live site as a checkout of a VC repository removes the need to FTP changes around; instead of trying to remember exactly which files you updated, you simply SSH into the live server and
svn update the code to pick up any changes.
My ideal workflow therefore would be:
- Add a new project to a remote VC repository. (Why remote? It’s an added layer of protection in case my development machine dies, the house burns down, etc. I also backup, but still, can’t hurt.)
- Checkout the project to a working directory on my iMac at home.
- Checkout the project to the live server.
- Work on the project locally, check in changes and add files.
- Update the project on live.
Subversion and MediaTemple
I understand that some hosts provide a Subversion environment of some sort as standard. MediaTemple don’t, although they do have it installed on the (gs) GridServer package that I have. They also provide a useful page detailing how to create a Subversion repository—you only need to follow steps 1-3 to start with, though.
I had problems getting my SVN browser (Versions, although I also tried ZigVersion and Cornerstone) to connect to the new repository initially. Eventually I followed these instructions on creating public-private keys—and after that it worked perfectly.
The new workflow
So, now that I have a repository set up on my (mt) server, my workflow looks like this.
Create the project structure and import to SVN
SSH to the server using your admin email address:
Create the project structure—I use a temporary folder for this:
example.com$ cd data/temp_projects example.com/data/temp_projects$ mkdir mynewsite example.com/data/temp_projects$ mkdir mynewsite/branches example.com/data/temp_projects$ mkdir mynewsite/tags example.com/data/temp_projects$ mkdir mynewsite/trunk
And finally import the new project into the repository:
example.com/data/temp_projects$ svn import mynewsite file:///home/12345/data/<repo>/mynewsite --message "Creating new project"
(Note that the
12345 bit is whatever your server number is with MediaTemple.)
Checkout the project to your working directory
Using your SVN browser of choice, connect to the repository (over svn+ssh as described in the articles above) and checkout the new project. You can now add and edit files, remembering to check them in when you’ve finished.
Checkout the project to the live site
On MediaTemple, all the sites on your server sit in a folder named ‘domains’, and each one (e.g. ‘example.com’) has a folder named ‘html’ inside it which is the web root.
SSH to the server again and navigate to the site folder:
ssh email@example.com example.com$ cd domains/example.com
New you can checkout the project—but we don’t want a folder named ‘trunk’, so we rename the checkout folder to be ‘html’:
example:com/domains/example.com$ svn co file:///home/12345/data/<repo>/mynewsite/trunk html
Now in the future when you want to update the live site, rather than using FTP to copy updated files, you can SSH to the server, navigate to the site folder, and run:
Voila! Any changed or added files are transferred from the repository—no forgotten files or dropped connections.
As you are keeping the live site in sync with the development version, it is worth remembering that you should only check in files that do not break the trunk! If you’re in the middle of working on something and it’s currently not working, don’t check it in! Version control is not a backup solution.
Working this way also means that you can’t sneakily update a file on the live server to fix a bug; you have to fix on dev and then deploy to live. Which is how you should be working anyway. :)
Filed under: Technology.
Previously: Refresh Cambridge October: Subversion