Automate the deployment of GitHub or coding code with Webhook

Source: Internet
Author: User
Tags json ssh nginx server git clone webhook

This problem is also due to their own inconvenience, usually the code is managed to GitHub or coding, on the one hand, for the convenience of code management, and then better storage code.

But recently it's a problem. After the local code commits git, it is cumbersome to go to the server to perform the following git pull.

So you want to use GitHub or coding's Webhook to automate code deployment.

Simply say the whole process
-When you push the code to a git server, the server receives the request to send a request as you set the address, and when the server receives the request, the GIT pull operation is updated with a code update below we will perform a detailed demonstration with the Nginx server. First of all, coding step 1. Generate Public key

The public key has two: 1. Git user public key, 2. Deploy public key: git user public key

Ssh-keygen-t rsa-c "admin@51growup.com"
# and then go straight to the line
# The generated file is usually/root/.ssh/id_rsa, and if the non-root user see the path on the prompt
Deploying the Public key
Sudo-hu www ssh-keygen-t RSA 
# Please select "No passphrase" and go straight down here with www user as Demo
2. Prepare the hook file Create and modify directory permissions:
#创建目录
mkdir/data/wwwroot/default/coding-webhook 
#修改目录权限
chown-r www:www/data/wwwroot/default/ Coding-webhook
To write a hook file: Create a file
Sudo-hu www touch/data/wwwroot/default/coding-webhook/webhook.php
Hook file Contents
<?php
    error_reporting (1);

    $target = '/data/wwwroot/default/test '; Production Environment Web Directory

    $token = ' photo ';
    $wwwUser = ' www ';
    $wwwGroup = ' www ';

    $json = Json_decode (file_get_contents (' Php://input '), true);

    if (Empty ($json [' token ']) | | $json [' token ']!== $token) {
        exit (' Error request ');
    }

    $repo = $json [' repository '] [' name '];

    $cmd = "CD $target && git Pull";

    echo shell_exec ($cmd);

Make sure your hook file is accessible: http://51growup.com/coding-webhook/webhook.php, hook ready to complete. Configuring the Coding Webhook 1. Add a user's public key

Copy/root/.ssh/id_rsa.pub content to the SSH public key of the Personal Settings page (Https://coding.net/user/account/setting/keys) 2. Add the deployment public key

Copy the contents of the/home/www/.ssh/id_rsa.pub and add to the deployment public key:

Select Project > Settings > Deploy Public key > New > Paste into the box below and confirm 3. Add Hook

Select Project > Settings > WebHook > New hook > Paste the URL of your coding-webhook/webhook.php. For example: http://51growup.com/coding-webhook/webhook.php, the token is optional, but the suggestion is written.

A few seconds to refresh the page to see the hook status, shown as a green tick is OK. Initialize 1. We need to clone the server first, and then we can automate the deployment:

Sudo-hu www git clone https://coding.net/u/itdream6/p/hiyyh/git/data/wwwroot/default/coding-webhook  --depth=1

This time should ask you to enter a coding account and password, because we set a permanent save the user name and password, so then git will not be required to enter the user name and password.

.. Note that the initialization of clone here must be done with www user 2. Submit a code test to coding.net:

In the repository for local clone execution:

git commit-am "test hook"--allow-empty
git push

OK, a few seconds later, normal words you in the configuration of the project directory will have your project files.

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.