Deploying Websites with Git

If you're developing websites, you'll want to apply continuous delivery and deployment techniques. Git is a popular Version Control System (VCS) that allows you to track all of your shiny new features while enabling clean and simple collaboration with other crafty developers. With continuous delivery, you can setup a steady release-schedule tailored to your web product's business requirements. Continuous small batch releases will help you and your team pinpoint any issues introduced into the code base—and with Git you can easily revert back to an issue-free state. Practicing continuous deployment simply means your end-users will receive the freshly-coded features that have past testing, right-away. In the following guide, I will show you how to automate your deployment step using Git's post-receive hook—you'll never use FTP again!


1. Get Git

Git is a distributed version control system application available for free with public repositories or as a monthly subscription model for private repositories. If you haven't already, Create A GitHub Account.


2. Command Line Commando

Becoming comfortable with the command line interface (CLI) is the key to unlocking faster website deployment. If you're less familiar, on Mac the command line tool is a program called Terminal that lives in your Applications/Utilities folder. On a PC, the command line tool can be found by navigating to All Programs/Accessories/Command Prompt from your Start menu. Alternatively, you can also search for "command."


3. SSH Keys to a Secure Castle

Secure Shell (SSH) enables a secure channel over an unsecured network in a client-server architecture. SSH keys provide a more secure way of logging into your server or GitHub account than solely using passwords due to SSH keys being nearly impossible to crack via brute force.


Check to see if you have existing SSH keys by entering the following into your command line:

$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist


Filenames for public keys will be one of the following:
id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub

To generate a new SSH key pair, enter the following:

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Generates a new public/private rsa key pair


The public key is located in /home/username/.ssh/id_rsa.pub.
The private key is located in /home/username/.ssh/id_rsa.


Next, you'll add your SSH key to your GitHub account:

$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

Add a New SSH Key to GitHub by pasting the contents of your clipboard. This will allow you to bypass authenticating with GitHub over and over again. You'll also want to set up your live production server to allow SSH access—this method varies by host.


Tip: If pbcopy method isn't working, you can locate the hidden .ssh folder, open the file in your choice of text editor and copy it to your clipboard.


4. Create your Local Git Repository

For a fresh new website, you'll first need to create the root folder:

$ mkdir new_website && cd new_website
# Creates and enters a new website directory


Once you've navigated to the root folder of your local website application, run the commands:

$ git init
# Initializes empty git repo /home/username/new_website/.git/
$ git add README.md
$ git add -A
# Adds all pre-existing files to Git
$ git commit -m “First Commit”
# Commits your changes locally with a new "commit message"


Now, link to your remote GitHub repository and push your changes:

$ git remote add origin https://github.com/username/respository.git
# Update path to your GitHub repo
$ git push -u origin master
# Pushes your local commit to your remote git repo


To update your Git remote's url:

$ git remote -v
# View the current url
$ git remote set-url origin https://github.com/username/repository.git


5. Setup your Remote Live Git Repository

On your live production server, you will create a new --bare repository to mirror the local one via the command line. A bare repository will only contain the version history of your code, and none of the source files. This detached work tree will live outside of your remote web root.

$ mkdir website.git && cd website.git
$ git init --bare
# Initializes empty Git repository in /home/username/website.git/


6. Add a Post-Receive Hook

In the next steps, you will define and enable a post-receive hook that checks out the latest tree into the web server's root. This hook will instruct Git where to push your production source files.


Create a file named post-receive in the hooks directory of your live production server's repository:

ls
# Lists directories
$ cd hooks
# Changes directory to hooks
$ nano post-receive
# Creates and opens up a new file named "post-receive"


Add the following code to your new file:

$ #!/bin/sh
$ GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f
# Update path to your web root


Save and exit the file, and from the hooks directory, make it executable:

$ chmod +x hooks/post-receive


7. Link your Remote Live Git Repository

From your local git repository, define a name for the remote mirror, and then mirror to it, creating a new "master" branch.

$ git remote add live ssh://server1.example.com/home/user/mywebsite.git
# Configures and names the remote path of our repository
$ git push live +master:refs/heads/master

Your live production server now contains a copy of your files, independent of .git metadata.


To update your live url:

$ git remote -v
# View the current url
$ git remote set-url live ssh://server1.example.com/home/user/mywebsite.git


8. Push Local Changes Live

You can now push local commits to your remote live production repository, where the post-receive hook will immediately update the web root. This method is more efficient than running "git pull" by hand or from a cron job.


To push your local commits live:

$ git push -u live master


Notes:

The detached work tree doesn't need to correspond to your live production root. Your repository may represent a subdirectory of it, or even contain the root as a subdirectory.


Setting receive.denycurrentbranch to "ignore" on your remote server eliminates a warning issued by recent versions of git when you push an update to a checked-out branch.


You may push to more than one remote repository by adding additional URLs to the [remote "live"] section in your .git/config. This can be helpful with pushing to multiple remote servers when working with load balanced websites.



Comments


Be the first to comment.




Add Comment

Posted by Lindsey
February 27, 2018
16min read