Web site Automation deployment using Github/gitlab's Webhooks

Source: Internet
Author: User
Tags git clone repository nginx reverse proxy webhook

I've been trying to write this topic for a long time, and today we have a chance to study Git's automated deployment. The end result is that whenever there is a new commit push to the master branch, it automatically makes GIT pull fetch the latest code on the test/production server, eliminating the repetitive work of the program Ape's part-time operations SSH pull code deployment. We also want Agile development not? What agile development ah, extreme programming ah, fast iteration ah, continuous integration ah, lean entrepreneurship Ah, the slogan how high-end how to come, the end is to write an automated script ...


One, automated deployment scripts

The first thing to ensure is that the WEB directory you are deploying is a Git clone repository. This rollback is also convenient, directly checkout a certain historical version is good.

It's very simple to write a shell script.deploy.sh

#!/bin/bashWeb_path='/var/www/dev.lovelucy.info 'Web_user=' Lovelucydev 'web_usergroup=' Lovelucydev 'Echo"Start Deployment"Cd$WEB _pathEchogit Reset Span style= "color: #660033;" >--hard origin/master git clean -fgit pullgit checkout Masterecho  "Changing permissions ..."  Chown -r  $WEB _user:$ Web_usergroup  $WEB _pathecho  "finished."                

The next thing to do is to automatically call this script whenever there is a push.

Second, the monitoring Web Hooks

GitHub and GitLab natively support Webhooks settings.

The Payload URL is populated with the URL of the server that needs to be deployed, say http://dev.lovelucy.info/incoming. Then each time there is a push event GitHub will send a POST request to this address, and of course you can select any event to send a post notification to you. GitHub also has a Secret setting, which is a string that, if added, will take a Hash value in the HTTP header of the POST request to verify that the post is really from GitHub, or else anyone will go to that address POST and you don't know who it is. Right......

Then we're going to write a script to accept the POST request here at http://dev.lovelucy.info/incoming. Because I run on the machine is node, I found a nodejs middleware github-webhook-handler. If you want to deploy a PHP site, then you should find the best language in the world PHP version, or write one yourself, just to receive $_POST , good simple, not much nonsense. (?ω?)

-G Github-webhook-handler

In view of the celestial server NPM pull repo than the situation, we can choose Ali's Mirror, said 10 minutes and official synchronization. _ (: 3"∠) _

--registry=http:-g Github-webhook-handler

All right, here's the NodeJS listener.deploy.js

var http= Require(' HTTP ')var Createhandler= Require(' Github-webhook-handler ')var handler= Createhandler({Path:'/incoming ', Secret:' Myhashsecret '})The above secret remains consistent with the GitHub background settingsfunction Run_cmd(cmd, args, callback){var spawn= Require(' Child_process ').Spawn;var child= Spawn(cmd, args);Var resp=""; Child.StdOut.On(' Data ',function(Buffer){RESP+ = buffer.Tostring();}); Child.StdOut.On(' End ',function(){Callback(RESP)});} http.Createserver(function(reqRes){Handler(reqRes,function(Err){Res.StatusCode=404 Res.End(' No such location ')})}).Listen(7777) handler.On(' Error ',function(Err){Console.Error(' Error: 'Err.Message)}) handler.On(' Push ',function(Event){Console.Log(' Received a push event for%s to%s ', event.Payload.Repository.Name, event.Payload.Ref); Run_cmd(' SH '[]function) {console. (Text}) ; })    

So I could just run.

$ Nodejs Deploy.js

Of course, to prevent NodeJS from hanging up, we can enable the process Management Service forever, which is similar to supervisor in Python.

-G forever$ Forever start deploy.js

This will automatically restart a process to ensure that the service is still available, even if the NodeJS code goes wrong somewhere. Some of Forever's commands are pretty simple.

1$ Forever Restartall

The above NodeJS the Web service on port 7777, we can use Nginx reverse proxy to 80 port (optional)

server {    listen;    server_name dev.lovelucy.info;    # ...    Location/{        # ...    }    location/incoming$ {        proxy_pass http://127.0.0.1:7777;    }}
Iii. integration of third-party Service

Notice that the GITHUB project has a Service tab in the background?

This is GitHub's official integration of a number of third-party services, a bit like IFTTT rhythm, can do a lot of things. For example, if someone push code, we send a notice in our Hipchat internal chat group, and if someone mention issue, they will automatically give him a welcome e-mail ...

GitLab also have these, more play or oneself to study it ~

–eof–

Http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html

Web site Automation deployment using Github/gitlab's Webhooks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.