About Git Webhook

Source: Internet
Author: User
Tags git client node server using git webhook

Using Git as the Code base management tool, if you want to implement automatic deployment, online search will appear a lot of tutorials, here to record their implementation.

Because the Gitlab server is a standalone machine, but it needs to be automatically deployed on another test site server, there is no way to use Git's service-side hooks, so look to Webhook.

Premise: A compiler has been listening to file changes, changes are compiled, so only need to update to the code in a timely manner

First install the GIT client on the test server and then put down the code (nonsense here).

The site uses LNMP, so you want to use PHP to execute the shell script, and then use the following code

/* webhook.php */$dir = '/www/site ';//The directory is checked out directory for git $handle = Popen ("CD {$dir} && git pull > Debug.log", ' R '); $read = Stream_get_contents ($handle);p rintf ($read);p close ($handle);

    

Then open the URL (before this first on their own computer to submit the code, otherwise how to see the effect, hehe), and view Debug.log, hehe, and no log output. Okay, is it a code problem? Simple, verify that you know, immediately use the root login using the shell directly run the command PHP webhook.php, and then look at the log file, there is output ...

OK, so to comb the whole process of what is the difference. The root account is used with the command line, but the URL is used in Nginx's account and two account permissions are different. Need to elevate user rights? Good trouble, I don't want to do that. What else can I do? Run a listener directly with root permission to perform a git pull operation no, that's fine. Next I use the Nodejs to deal with this problem, as follows

Server.jsvar http = require ("http"), Http.createserver (function (request, response) {    Response.writehead (200, { "        content-type": "Text/plain"    });    Response.Write ("Wait ...");    var exec = require (' child_process '). exec;    var cmdstr = ' cd/www/site && git pull > debug.log ';    EXEC (CMDSTR, function (err, stdout, stderr) {        if (err) {            Console.log ("error");        } else {            console.log (" Success ");        }    );    Response.End ();}). Listen (8081);

  

Next, commit the code, and then run node server.js under the root account. Take a look at Debug.log, well, it's already there. Next we open up port 8081? Of course no, I chose the use of Nginx Proxy (is so wayward, not to bite me), nginx configuration file is added as follows

location/xxx {    Proxy_pass http://127.0.0.1:8081;    break;}

  

Done, visit the URL test, www.mysite.com/xxx is OK, next open your project management interface setting put the URL into the webhook inside, OH.

It turned out that I was not fit to write a document and felt like I could not read it. Ashamed

About Git Webhook

Related Article

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.