Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Setup Automatic Deployment with Git with a VPS

Table Of Contents


    ## Table of Contents
    – [Introduction](#introduction
    – [Server Setup](#server-setup
    – [Setting up Live Server Repository](#setting-up-live-server-repository
    – [Hooks Directory](#hooks-directory
    – [Creating a Post Receive File](#creating-a-post-receive-file
    – [Setting up a Local Repository](#setting-up-a-local-repository
    – [Setting up Beta Directory](#setting-up-beta-directory
    – [Going Live from the Server](#going-live-from-the-server

    ## Introduction
    In this guide, we will walk you through how to use Git when deploying your applications. Despite the existence of a myriad ways which you can use Git to deploy your applications, we shall focus on the most straightforward way of them all. This guide also assumes that you already know how to install Git and create your own repositories on your PC.
    As you use Git, workflow is generally geared towards version control only. This is where you have a local repository as your workspace as well as a remote repository whereby everything is synced, and this allows collaboration with team members using their own PCs. Let’s see how you can use Git to push your application onto a production environment.

    ## Server setup
    To start off, we are going to have 2 repositories
    Your server’s live directory – /var/www/systemongrid.com
    Your server’s repository – /var/grid-repo/grid.git

    ## Setting up Live Server Repository
    From the command-line prompt, log in to your VPS
    Navigate to the /var directory
    “`
    # cd /var
    “`
    Create the server’s repository directory
    “`
    # mkdir grid-repo
    “`
    Navigate to the newly created directory
    “`
    # cd grid-repo
    “`
    Create the git directory
    “`
    # mkdir grid.git
    “`
    Navigate into it
    “`
    # cd grid.git
    “`
    Run the command below to ensure that the folder is just for version control and not for source code files.

    “`
    # git init –bare
    “`

    Output
    ![](http://

    To list the contents, use the ls command

    “`
    # ls
    “`
    ![](http://

    ## Hooks Directory
    As we have seen earlier, there exists a folder in our git repository called hooks. This folder contains some sample files which can be used for possible actions which can be used to hook and use customized actions.
    ![](http://

    For more documentation, visit [Git – Git Hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks.

    There are 3 server hooks:

    ## Pre-receive
    Pre-receive is the first script that is run when a client initiates a ‘push’ request.
    ## Update
    Update executes only once per branch
    ## Post-receive
    Post-receive is executed once a ‘push’ is completely done with and can be used to notify other services & users.

    ## Creating a Post-receive file

    To create a post-receive file, navigate to the hooks folder and run the command below

    “`
    touch post-receive
    “`

    Using your favorite text editor, open the file and add the following line

    ![](http://

    Give the file execute permissions

    “`
    chmod +x post-receive
    “`

    To confirm that the file now has execute permissions run the ls -l command

    “`
    ls -l
    “`
    ![](http://

    ‘git-dir’ will be the path to the repository. However, a different path to the location where your files will be transferred to can be defined. Once a ‘push’ request is completed the post-receive file will be consulted and will give you a prompt that your files should be in /var/www/systemongrid.com

    ## Setting up a Local Repository
    Now let’s get back to our local PC and create own repository
    Create your own repository as shown

    “`
    # mkdir my_space
    “`

    Navigate into it

    “`
    # cd my_space
    “`
    Initialize with Git

    “`
    # git init
    “`
    Output

    ![](http://

    Set our repository’s remote path

    “`
    # git remote add live ssh://user@mydomain.com/var/grid-repo/grid.git
    “`
    Later, add the file
    “`
    # git add.
    “`
    then finally commit
    “`
    # git commit -m “My project is ready”
    “`

    Finally, ‘push’ the contents to the server

    “`
    # git push live master
    “`

    ## Setting up BETA Directory
    If you prefer testing first without deploying everything at once , you can opt to create a Beta directory. All you need to do is to create yet another repository.
    So once again, log in to your VPS and create a new directory as shown
    “`
    # cd /var/www/
    “`
    Create the directory
    “`
    # mkdir beta
    “`

    AS before, create the repository
    “`
    # cd /var/grid-repo
    # mkdir beta.git
    “`
    Navigate into the directory
    “`
    # cd beta.git
    “`
    Initialize the directory
    “`
    # git init –bare
    “`
    Cd into the hooks directory
    “`
    # cd hooks
    “`

    Now create the post receive file since we want our project to appear in the Beta directory
    “`
    # touch post-receive.
    “`
    Using the text editor of your choice, edit the file by adding the following script
    “`
    #!/bin/sh
    git –work-tree=/var/www/beta –git-dir=/var/repo/beta.git checkout -f
    “`

    Save and exit.

    Give the file execute permissions as we saw in our previous set up

    “`
    # chmod +x post-receive
    “`
    Head back to the local repository

    “`
    # cd my_space/
    “`
    Create a new directory

    “`
    # mkdir project
    “`
    Configure another remote to point to the Beta directory
    “`
    # git remote add ssh://user@mydomain.com/var/grid-repo/beta.git
    “`

    Next, run the following commands in succession

    “`
    # git add.
    # git commit -m “latest version”
    “`

    Push to beta
    “`
    # git push beta master
    “`

    Then push to Live
    “`
    # git push live master
    “`

    ## Going Live from the Server
    Now it’s time to link the Beta to the Live repository. This will enable your team to collaborate on the same project remotely. To accomplish this, log in as root and create a beta repository
    “`
    # cd /var/grid.repo/beta.git
    “`
    Next run
    “`
    # git remote add live ../site.git
    “`
    At this point you should be able to push your content to the live server from Beta. Run
    “`
    # cd /var/grid.repo/beta.git
    “`
    Finally push to Live server
    “`
    # git push live master
    “`
    Well done! You have now set up your VPS to automatically deploy content with Git