Deployment automation

Rather than a version control tool, Git is also popular for relatively simple deployment automation. And in this section, we'll get our hands on and configure automated deployment based on Git.

Passive deployment based on Git server side hooks

The idea of passive deployment is simple: when a client pushes commits to the bare repository on the server, a post-receive hook of Git will be triggered. And thus we can add scripts checking out changes and start deployment.

The elements involved in the Git deployment solution on both the client and server sides includes:

Passive deployment based on Git server side hooks

To make this mechanism work, we need to perform the following steps:

  1. Create a bare repository on the server with the following command:
          $ mkdir deployment.git
          $ cd deployment.git
          $ git init --bare
    

    Note

    A bare repository usually has the extension .git and can be treated as a centralized place for sharing purposes. Unlike normal repositories, a bare repository does not have the working copy of source files, and its structure is quite similar to what's inside a .git directory of a normal repository.

  2. Add deployment.git as a remote repository of our project, and try to push the master branch to the deployment.git repository:
         $ cd ../demo-project
         $ git remote add deployment ../deployment.git
         $ git push -u deployment master
    

    Note

    We are adding a local bare repository as the remote repository in this example. Extra steps might be required to create real remote repositories.

  3. Add a post-receive hook for the deployment.git repository. We've already worked with the client side Git hook pre-commit, and the server side hooks work the same way.

But when it comes to a serious production deployment, how to write the hook could be a hard question to answer. For example, how do we minimize the impact of deploying new builds?

If we have set up our application with high availability load balancing, it might not be a big issue to have one of them offline for minutes. But certainly not all of them in this case. So here are some basic requirements of the deploy scripts on both the client and server sides:

  • The deployment should be proceeded in a certain sequence
  • The deployment should stop running services gently

And we can do better by:

  • Building outside of the previous deployment directory
  • Only trying to stop running services after the newly deployed application is ready to start immediately

Proactive deployment based on timers or notifications

Instead of using Git hooks, we can have other tools pull and build the application automatically as well. In this way, we no longer need the client to push changes to servers separately. And instead, the program on the server will pull changes from a remote repository and complete deployment.

A notification mechanism is preferred to avoid frequent fetching though, and there are already tools like PM2 that have automated deployment built-in. You can also consider building up your own using hooks provided by cloud-based or self-hosted Git services.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.50.156