Tech

En till Good Old Blogs webbplats

Git workflow – going live

This post describes part of our workflow: the process of moving a Drupal site from a development server to a live setup using git, and other cli tools. With small adaptations it’s also a good guide to moving any kind of site (wordpress, zend, codeignite…) from your local machine to a dev server, or dev server to staging server, et cetera. It will be one in a series of posts describing how things should be done here at Good Old. It’s primary function is as internal documentation/manual, but in the spirit of sharing and exchanging idéas we’ve chosen to publish it here instead of letting it gather dust in our office. All feedback, thoughts and workflow-replies are welcome.

There are some assumptions made in this article. I assume that you:

  • CAN SUBSTITUTE PLACEHOLDER NAMES, PATHS AND PASSWORDS WITH THE REAL THING
  • have git and ssh access to both dev and live servers
  • have a local checkout of the project you’re working on
  • have created a database on the live server
  • have created a ~/devsite direcory to place database dumps and files in

Getting the database

First of all we need to get a database dump of the development server. The command for this is as follows:

$ mysqldump -v --opt -hmysql.dev.com -udev_user -pdev_pass dev_db > ~/dbdump.sql

The password can be omitted and you will then be prompted to enter it instead.

$ mysqldump -v --opt -hmysql.dev.com -udev_user -p dev_db > ~/dbdump.sql

Download this dump to your local machine using your client of choice, or run the following on your local machine:

$ scp dev_user@dev.com:~/dbdump.sql ~/devsite/dbdump.sql

Getting the file directory

To get the file directory from the dev server, create a dev folder in your home directory and run the following:

$ rsync -vzr dev_user@dev.com:~/webapps/devsite/sites/default/files ~/devsite/

This will create a files directory inside ~/devsite containing all the files from your dev server file directory.

Setting up the live server

First of all you need to get the public key for the key pair you’re using for authentication into the authorized_keys file on the live server:

$ scp ~/.ssh/goodold_rsa.pub live_user@live.com:~/goodold_rsa.pub
-enter password
$ ssh live_user@live.com
-enter password
$ mkdir .ssh
$ cat goodold_rsa.pub >> .ssh/authorized_keys
$ chmod go-w .ssh/authorized_keys

Setting up git on live server

Then, while still logged in at the live server enter the web root:

$ cd ~/public_html

…and remove any placeholder content you might find there. If there is anything there that looks like it’s needed by the host (and won’t collide with anything in the Drupal root) that you’d like git to ignore you should just add it to the ~/.gitignore file:

$ echo "/cgi-bin" >> ~/public_html/.gitignore
$ echo "/wusage" >> ~/public_html/.gitignore

When that’s done, run the following to set up a empty git repo:

$ git init

Add the live server as a remote on your local machine

Go to the git repositiory on you local machine:

$ cd ~/Projects/devsite/public_html

And add the live server:

$ git remote add live ssh://live_user@live.com/~/public_html
$ git push live master

Finishing touches on the live server

Return to your terminal session or the live server or open a new one. What we need to do now is to create a new branch for the live server, because you should never push to the active branch, as this will cause the index and working directory to go out of sync. All submodules will also have to be initialized and brought up to date.

$ cd ~/public_html
$ git checkout -b live master
$ git submodule update --init

Post-recieve-automation

Optionally, you can also add a post-recieve hook that automatically merges the
live branch with the master branch when pushed to.

$ curl "http://gist.github.com/raw/143470/25aba5c210820c80124dc0e831d6dc80bccb347f/post-receive" > .git/hooks/post-receive
$ chmod u+x .git/hooks/post-receive

Getting the files and database to the live server

Starting up from your local machine execute the following to get the files and database up on your live server.

$ scp ~/devsite/dbdump.sql dev_user@dev.com:~/dbdump.sql
$ rsync -vzr ~/devsite/files dev_user@dev.com:~/webapps/devsite/sites/default/
$ ssh ssh://live_user@live.com
$ cat dbdump.sql | mysql -hmysql.live.com -ulive_user -plive_pass live_db
$ rm dbdump.sql

Wrapping it up

All that’s left now is to copy the default.settings.php file and enter the database details:

$ cp ~/public_html/sites/default/default.settings.php ~/public_html/sites/default/settings.php
$ nano ~/public_html/sites/default/settings.php

Now you should be good to go. One thing that you should be aware of is that if you left out the post-recieve hook you have to ssh to the live server to update it after a push.

$ ssh ssh://live_user@live.com
$ cd ~/public_html
$ git merge master

Kommentarer

 

Här var det tomt!

Ingen har skrivit en kommentar på den här artikeln. Bli den första!