How to publish your website with Git Print

  • 0

If you have a Git repository for your website then you can use Git to publish and update your website, this is much faster than manually uploading changes using FTP or the File Manager.

This article assumes that you already have a Git repository for your site and are familiar with using the command line including text editors such as vi or vim.

To achieve this you will create a remote for your repository on the server and then add a post-receive hook script to it that will copy the changes into your website folder and run any other commands required such as updating composer, running Laravel migrations or SilverStripe dev/build. Then whenever you wish to publish changes you will simply push to this remote.

Step 1: Configure password-less SSH access

Before you go any further please make sure you have configured password-less ssh access using your public key, see this article: How to access the server over SSH

Step 2: Create an empty repository on the server

1. SSH into the server:

ssh [email protected] -p <port number>

2. Change to the directory for your domain:

cd domains/yourdomain.com/

3. Create the bare repo:

mkdir website.git
cd website.git
git init --bare

4. Create a hooks file that will copy the files into the public_html dir and run any other publish commands you need:

vi hooks/post-receive

Add the following content of hooks/post-receive, adding things like composer etc is optional (replace username with your username and domain.com with your domain):

#!/bin/bash
TARGET="/home/username/domains/domain.com/public_html"
GIT_DIR="/home/username/domains/domain.com/website.git"
BRANCH="main"
while read oldrev newrev ref
do
    # only checking out the main (or whatever branch you would like to deploy)
    if [[ $ref = refs/heads/$BRANCH ]];
    then
        echo "Ref $ref received. Deploying ${BRANCH} branch to server..."
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
        cd $TARGET
        # Add or uncomment any extra lines below as required:
        # echo "Running composer install..."
        # composer install
    else
        echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
done

5. Make file executable:

chmod +x hooks/post-receive

Step 3: Add the new remote repo to your local repository

1. Now back on your local machine add the new remote to your local repository. Make sure you replace username with your username, <port number> with our custom port number and domain.com with your website domain. I have called the remote repository "live" but you may wish to use a different name:

git remote add live ssh://[email protected]:<port number> /home/username/domains/domain.com/website.git

Step 4: Publish

Now when you are ready to publish your website changes simply push the branch to the live remote (or whatever you called the remote in the above step):

git push live main

Was this answer helpful?

« Back