Topic in Version Control

Setting up Git for 1-Click Deployment

Last Updated -

Git is one of the best ways to deploy your changes. Since most developers are already committing their code to version control using Git, it’s a seamless way to integrate deployments into an existing workflow.

In this article, we’ll show you how to create a Git repository on your server and automatically deploy those changes using a post-receive hook.


Creating a Remote Repository

First, we’ll need a Git repository to push our code to. Let’s go ahead and create an empty Git repository now.

  1. First, begin by logging into your Pagely VPS via SSH. If you don’t know how to log in using SSH, you can follow our article on logging into your VPS via SSH.

  2. Next, you’ll need to create an empty location for your files to reside. To do so, run the following command:

    mkdir /data/git/my_repo_name.git
  3. Once the directory is created, go to it with the following command:

    cd /data/git/my_repo_name.git
  4. Now that you’re inside the repository location, initialize it with the following command:

    git init --bare

Your new Git repository has now been created and is ready to house any code that you push to it.

Creating a Post-Receive Hook

In the previous section, you will have created an empty Git repository for housing your code changes.

In this section, we’ll be creating a post-receive hook that will automatically move your files from the repository to your environment when the changes are pushed.

  1. Access your VPS via SSH.

  2. When creating the post-receive hook, you’ll need to know the absolute path to the site you’ll be deploying. To get the absolute path to a site, enter the following:

    ls -l ~/sites
  3. After running the command in the previous step, you’ll see all of your sites listed with the absolute path to the right of them. Make a note of any paths that you’ll be deploying to. You’ll need them in the next steps.

  4. Next, navigate to the location of your Git hooks:

    cd /data/git/my_repo_name.git/hooks
  5. Now let’s create a post-receive file:

    nano post-receive
  6. Inside the post-receive hook, enter the following and edit any of the paths that you see listed here in red to match the path that you obtained in step 3:

    ## store the arguments given to the script
    read oldrev newrev refname

    ## Where to store the log information about the updates
    # The deployed directory (the running site)
    PUSHED=$(git rev-parse --symbolic --abbrev-ref $refname)

    case $PUSHED in
    echo "branch $PUSHED must first be configured in post-receive"
    exit 1

    ## 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"
  7. Once you’re finished inserting the code for your post-receive hook, save and close it by pressing Ctrl+X, and saving at the prompt.

  8. Finally, make the post-receive script executable by running the following:

    chmod +x post-receive

Your post-receive hook will now deploy your site when pushed to the repo. To learn about how to configure your local Git client to push to this repository, continue on the the next section.

Configuring Your Local Environment

Now that the Git repository is active on the server and a post-receive hook is set up to move your changes to your different environments, it’s time to configure your local environment to use the new repository.

This section assumes that you already have Git installed on your local environment. If not, you’ll want to learn to do that first.

  1. Begin by opening up a terminal window on your local machine.

  2. Next, go to your local copy of your site.

    cd /my/local/path/here
  3. If this directory is already from a Git repository, you can skip to the next step. If not, initialize Git with the following:

    git init
  4. You’ll now need to tell your local version that it needs to push files to your new repository (in this example, we’ll call it “pagely”) on the server.

    To do that, run this command from inside your local directory:

    git remote add pagely

You’re now configured to push to the server when using the staging branch. Simply commit and push your changes and they’ll be uploaded to the site.

Ignoring Core Files

In most cases, you’ll want to ignore certain files, such as WordPress core files, to prevent them from deploying. Inside your .gitignore file, add the following:

/* ignore WP core and misc*/

Deploying Your Changes

Now that your repository is configured, it’s time to test it out and deploy your code. Here’s how to do it:

  1. Open a terminal window and navigate to your working directory like you did in the last section.

  2. Switch to your staging branch like this:

    git checkout -b staging
  3. Next, add your files and commit your changes like you would any other git repository:

    git add .
    git commit -m "My commit message"
  4. Finally, push your changes like normal:

    git push pagely staging

Of course, if you prefer to use a GUI, you can always use your preferred Git client to make changes. Just be sure to commit to the correct branch for deployment to occur.

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.

Powered by Zendesk