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.
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:
To make this mechanism work, we need to perform the following steps:
$ mkdir deployment.git $ cd deployment.git $ git init --bare
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.
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
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:
And we can do better by:
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.
13.58.50.156