Setting up git on Pagely VPS/Enterprise for 1 click deployment

Last Updated -


Many developers enjoy the convenience of working locally and pushing their code to Github and Bitbucket for safe keeping.Some have pushed this convenience even further, creating deploy scenarios that allow them to not only push changes to a repository, but to deploy to a staging or production site on a live web server. What you may not know is that If you have access to a terminal, a Pagely VPS, and a little bit of patience this method is already available for you to utilize.

In this tutorial, we'll walk you through how to create a bare repository on your Pagely VPS, set up a post-receive hook, and connect your local working git directory to an application on your server through that bare repository.

In Git you should only use a "bare" repository to clone and pull from, and push to. It doesn't have a checked out tree, so it just does what the "server" notionally does in a centralized VCS – records commits, branches, etc when you push to it, and gives you the latest versions when you clone or pull from it.git scm book

Our Goal Today:

Run git push so your local code will be deployed into a Pagely WordPress application.

To Complete this Tutorial, Four Parts are Needed.

  1. A working repository (@local)
  2. A remote, bare (no working directory), repository (@Pagely)
  3. A working directory (your deployed site or test branch @Pagely)
  4. Most importantly an SSH/SFTP user.

Setting Up the Remote Repository 

On your Pagely VPS server your bare repo should be setup in: /data/git and you will need to use the full path to the site for git (ie /data/sxxx/domxxxx/domxxxx) you can get that easily by running:

$ ls -l ~/sites/

The bare repo must be placed in /data/git on your Pagely VPS:

$ cd /data/git
$ mkdir my_site_repo.git
$ cd my_site_repo.git
$ git init --bare
Initialized empty Git repository in /data/git/my_site_repo.git/

create-bare-repo.gif

When this is finished, if you ls (list) the /data/git/my_site_repo.git directory, you'll notice that it contains what would normally be stored in a bare git directory. 

The Post-Receive Hook

With the remote repository setup, we next add a git post-receive hook. (named post-receive, in your "hooks" directory) Sample post-receive below:

#!/bin/bash
#
## store the arguments given to the script
read oldrev newrev refname
## Where to store the log information about the updates LOGFILE=./post-receive.log # The deployed directory (the running site) DEPLOYDIR=/data/sxxx/domxxx/domxx ## Record the fact that the push has been received
date +"Date : %d/%m/%Y Time : %H.%M.%S" >> $LOGFILE echo " - Old SHA: $oldrev New SHA: $newrev Branch Name: $refname" >> $LOGFILE ## Update the deployed copy echo "Starting Deploy" >> $LOGFILE echo " - Starting code update" GIT_WORK_TREE="$DEPLOYDIR" git checkout -f echo " - Finished code update" echo "Finished Deploy" >> $LOGFILE

This script, written in bash, will be fired off after the push is received by the remote repository from the master branch. If we want to set cases for different branches, we'd modify the code above to something like this...

#!/bin/bash
#
## store the arguments given to the script
set -x
read oldrev newrev refname

## Where to store the log information about the updates
LOGFILE=./post-receive.log
# The deployed directory (the running site)
#DEPLOYDIR=/data/sxxx/domxxx/domxx
PUSHED=$(git rev-parse --symbolic --abbrev-ref $refname)

case $PUSHED in
master)
DEPLOYDIR=/data/sxxx/domxxx/domxx
;;
deploystage)
DEPLOYDIR=/data/sxxx/domxxx/domxx
;;
deploytest)
DEPLOYDIR=/data/sxxx/domxxx/domxx
;;
*)
echo "branch $PUSHED must first be configured in post-receive"
exit 1
;;
esac

## Record the fact that the push has been received
date +"Date : %d/%m/%Y Time : %H.%M.%S" >> $LOGFILE
echo " - Old SHA: $oldrev New SHA: $newrev Branch Name: $refname" >> $LOGFILE

## Update the deployed copy
echo "Starting Deploy" >> $LOGFILE
echo " - Starting code update"
GIT_WORK_TREE="$DEPLOYDIR" git checkout -f ${PUSHED}
echo " - Finished code update"

It manages the entire deployment process for us on three branches (master, deploystage, deploytest). We're going to use the latter post-receive code sample.

Let's look at it piece by piece:

read oldrev newrev refname

Here we store the four arguments passed to the post-receive script when it's called. These are:

  1. The previous commit SHA1 hash
  2. The latest commit SHA1 hash
  3. The refname (containing branch information
  4. we parse this into our $PUSHED variable, and then through our case statements for a matching statement. If it doesn't match, we we echo out that the branch must be configured before post-receive).
LOGFILE=./post-receive.log
#DEPLOYDIR=/data/sxxx/domxxx/domxx
PUSHED=$(git rev-parse --symbolic --abbrev-ref $refname)

case $PUSHED in
master)
DEPLOYDIR=/data/sxxx/domxxx/domxx
;;
deploystage)
DEPLOYDIR=/data/sxxx/domxxx/domxx
;;
deploytest)
DEPLOYDIR=/data/sxxx/domxxx/domxx
;;
*)
echo "branch $PUSHED must first be configured in post-receive"
exit 1
;;
esac

The variable above contains our post-receive logs, and if we are using our case statements, we don't need to hardcode DEPLOYDIR here (this is why its commented out the first time we see it), but in every potential case (our three branches).

create-post-receive-new.gif

be sure to make post-receive executable!

chmod +x post-receive

post-receive-executable.gif

Please make sure to update DEPLOYDIR to the proper applications absolute path

date +"Date : %d/%m/%Y Time : %H.%M.%S" >> $LOGFILE
echo " - Old SHA: $oldrev New SHA: $newrev Branch Name: $refname" >> $LOGFILE
echo "Starting Deploy" >> $LOGFILE
echo " - Starting code update"
GIT_WORK_TREE="$DEPLOYDIR" git checkout -f ${PUSHED} echo " - Finished code update"

We log the date and time to our post-receive.log file, and tell git where the working tree is. We then instruct git to checkout the latest copy of the code to that directory, appending any changes which may have manually been made.

echo "Finished Deploy" >> $LOGFILE

The last step is indicating that the deploy's now complete. You'd likely do a lot more than this in a standard project, including:

  • Clearing and pre-warming cache directories
  • Running database migrations
  • Running unit & regression tests
  • Checking permissions on key directories

 

Setting up our Local Environment

After that, we're pretty much finished. In your working directory, you have a couple of options...

1. turn a directory into a git repository...

git-init-local.gif

3. Clone an existing repo on github

git-clone.gif

Whichever option you decide is fine, but one will be necessary to complete the next step...

Adding The Remote Repository

run the following command to add the new remote repository, replacing client_USER with your SSH/SFTP user, and my_site_repo with whatever you named your repository on your Pagely server...

git remote add deploytest client_USER@pagelyexample:/data/git/my_site_repo.git

git-remote-add.gif

Note to ensure your local repository uses the proper ssh key, we recommend setting up your ~/.ssh/config file like so...

$cat ~/.ssh/config
Host pagelyexample.vps.pagelyhosting.com
HostNamepagelyexample.vps.pagelyhosting.com
User client_USER
IdentityFile ~/id_rsa_deploytest
ForwardAgent yes
HashKnownHosts no

make sure do chmod to 644

chmod 644 ~/.ssh/config

With that in place, all we need to do is then start developing our application and push as required.

Git Ignore

Since our system manages the core WordPress files you should be sure to utilize .gitignore in your repo to avoid damaging your managed WordPress application

/* ignore WP core and misc*/

wp-content/blogs.dir/
wp-content/upgrade/
wp-content/backup-db/
wp-content/advanced-cache.php
wp-content/wp-cache-config.php
sitemap.xml
*.log
/wp-*.php
/index.php
license.txt
readme.html
wp-admin/
wp-includes/
xmlrpc.php
wp-content/cache/
wp-content/backups/
wp-content/mu-plugins/pagely*
wp-config-hosting.php
db-config.php sitemap.xml.gz .DS_Store

gitignore.gif

We've made it! When you're ready to push to your Pagely VPS, a simple $ git push pagely_test should do it, but first lets add a test file into our bare repository...

first lets switch to another branch other than master...

git checkout -b deploytest

next, lets add a simple html file...

$cat test.html
<h1>Hello World\!</h1>

test-file.gif

Lets add and commit this on our branch...

git add test.html
git commit -m "added test.html"

test-file-two.gif

The moment of truth! Let's deploy...

git-push-deploy.gif

Success!!! Lets check the server...

show-test-html.gif

There you have it. We covered quite a bit in this tutorial. Please note that Pagely created this tutorial as a courtesy to our customers. While we may provide assistance as a courtesy on this subject matter, we generally see this as out of our purview. If we do assist, its usually during on boarding to help our customers get comfortable with a workflow suitable to our environment. If you'd like to see other options, please take a look at the following articles...

Deploying Changes with Git and Deploybot

The Pagely Clone Tool 

Related Topics

Pagely is the Managed WordPress Hosting Platform designed to exceed the needs of media, business, and Enterprise customers alike. We help the world's biggest brands scale WordPress.

Copyright © 2006-2017 Pagely, Inc. All rights reserved.
Pagely® and WordPress® are registered trademarks.

Pagely
Powered by Zendesk