Use Code cloud, GitHub version control, and automatic deployment by WebHook

We usually need to PUSH code to the remote repository, online environment will automatically synchronize the code, this time you need to use WebHook, it will automatically callback http address our set.

By our own script written request to pull the code, the code synchronization with a remote warehouse.

First, we create a local repository

echo "# 测试" > README.md
git init
git add README.md
git commit -m "test"

Create a code on GitHub cloud or an empty warehouse, such as: test, then let the local warehouse associated with the remote repository.

git remote add origin https://gitee.com/xxx/test.git
git push -u origin master

  

Second, the local repository, add WebHook documents and submit to the repository

Cloud code version:

<PHP? 
$ = json_decode the Data (file_get_contents ( 'PHP: // the INPUT'), to true); 

password // code configured cloud WebHooks 
$ password = "123456"; 

// your local project path 
$ path = "/ Data / wwwroot / Test "; 

// password Analyzing 
IF ($ Data [ 'password'] === $ password) { 
    echo shell_exec (" ID -a "); 
    echo shell_exec (" CD} {$ && path / usr / bin / RESET Git --hard Origin / Master && / usr / bin / Git Clean && -f / usr / bin / Git pull 2> &. 1 "); 
    Exit (); 
} 

http_response_code (404);

GitHub version:

<?php

// GitHub项目 Settings/Webhooks 中的 Secret
$secret = "123456";

// 你本地的项目路径
$path = "/data/wwwroot/test";

// 验签
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE'];

if ($signature) {
    $hash = "sha1=" . hash_hmac('sha1', file_get_contents("php://input"), $secret);
    if (strcmp($signature, $hash) == 0) {
        echo shell_exec("id -a");
        echo shell_exec("cd {$path} && /usr/bin/git reset --hard origin/master && /usr/bin/git clean -f && /usr/bin/git pull 2>&1");
        exit();
    }
}

http_response_code(404);

  

Third, the login server online, SSH secret key generated for the user, and the public key code configuration items of a cloud or GitHub.

Configuring a major role in the public key is required to enter a password every time without having to git operations.

Note that here, we want to generate ssh keys to user www or nobody user, make no mistake. When webhook call, the user is currently executing php script.

Which specific user may echo shell_exec ( 'id -a'); view.

sudo mkdir -p /home/www/.ssh
sudo chown -R www.www /home/www/.ssh
sudo -Hu www ssh-keygen -t rsa

Enter all the way until the end, the system will be in the user's home directory, and id_rsa.pub generate id_rsa two documents, namely id_rsa key and id_rsa.pub public key.

cat /home/www/.ssh/id_rsa.pub

Then in the code cloud Project Management -> Public Key Management -> add the public key, add the contents of id_rsa.pub in.

In GitHub account -> Setting -> SSH and GPG keys, add the contents of id_rsa.pub in.

Then in the code are configured WebHook cloud and GitHub project, attention must be able to address the external network access.

 

Fourth, landing online server, and use the ssh protocol clone project

Notice that we configured ssh public key of the project, the project to go pull the ssh protocol, rather than https.

git clone [email protected]:xxx/test.git

Modify the permissions, users pay attention to who you are PHP runtime, usually www, there may be nobody

sudo chown -R www .
sudo chmod -R g+s .
sudo -u www git pull

  

 Fifth, submit files locally, you can see the code is automatically synchronized to the online server.

Guess you like

Origin www.cnblogs.com/jkko123/p/11620367.html