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.
Want to Deploy From GitHub, GitLab, or Bitbucket?
If your code exists on a hosted version control system, such as GitHub, GitLab, or Bitbucket, you'll likely want to use one of our webhook integrations instead. For information on automatically deploying from one of these services, see the following articles:
- Automatically Deploying WordPress Sites From GitHub
- Automatically Deploying WordPress Sites From GitLab
- Automatically Deploying WordPress Sites From Bitbucket
Requirements
- A VPS/Enterprise account.
- SSH access.
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.
-
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.
-
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
-
Once the directory is created, go to it with the following command:
cd /data/git/my_repo_name.git
-
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.
-
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
-
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.
-
Next, navigate to the location of your Git hooks:
cd /data/git/my_repo_name.git/hooks
-
Now let’s create a post-receive file:
nano post-receive
-
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:
#!/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) PUSHED=$(git rev-parse --symbolic --abbrev-ref $refname) case $PUSHED in production) DEPLOYDIR=/data/sxxx/domxxx/domxx ;; staging) 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"
-
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.
-
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.
-
Begin by opening up a terminal window on your local machine.
-
Next, go to your local copy of your site.
cd /my/local/path/here
-
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
-
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 client_REPLACEWITHUSER@example.com:/data/git/my_repo_name.git
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*/
wp-content/blogs.dir/
wp-content/upgrade/
wp-content/backup-db/
wp-content/advanced-cache.php
wp-content/wp-cache-config.php
wp-content/object-cache.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
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:
-
Open a terminal window and navigate to your working directory like you did in the last section.
-
Switch to your staging branch like this:
git checkout -b staging
-
Next, add your files and commit your changes like you would any other git repository:
git add .
git commit -m "My commit message"
-
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.