I. Introduction and function of Webhook
A Webhook, as the name implies, is a hook. In short, it can trigger a specific action in a specific situation. For example, when you perform push and tag operations in the remote Git repository, the remote server automatically pulls and compiles the code. The following is a push code to the remote repository, which automatically pulls the code to compile and push the generated WebAssembly-related files (.js,.wasm) and version numbers to the demo in the remote repository.
Second, demo implementation
1. Warehouse Settings (Gitee as an example)
First, you need to have project administrator rights, click Administration at the top of the project home page, then click on WebHooks at the bottom of the left menu to go to the Settings page in the screenshot below.
In the interface above, I have added a Webhook with a push trigger type and a password. When you push code to the remote repository, you send a POST request with the password set to the URL set here. Of course, you can also check other operation types.
2. PHP code implementation
- hooks.php
<? PHP // local repository path$local = '/data/wwwroot/default/hooks/laserbox'; // Security authentication string. If it is empty, no authentication is required$token = '123456'; // Payload is a string that needs to be parsed$payload = file_get_contents('php://input');
if (!$payload) {
header('the HTTP / 1.1 400 Bad Request');
die('HTTP HEADER or POST is missing.');
}
$content = json_decode($payload.true); // If validation is enabled and fails, an error is returnedif ($token && $content['password'] != $token) {
header('the HTTP / 1.1 403 Permission Denied');
die('Permission denied.'); } // a script will be executed to compile the code, and then push the code to remote // so WebHooks are repeatedly triggered, so check here if they are local pushesif($content['commits'] [0] ['author'] ['name'] = ='handsomeTaoTao'){
header('the HTTP / 1.1 403 Permission Denied');
die('self push.'); } /* * There are a few things to note here: * * 1. Make sure PHP executes system commands properly. Write a PHP file with the following contents: * '<? phpecho shell_exec('ls -la') '* When accessed through a browser, the output directory structure shows that PHP can run system commands. * * PHP can run system commands using WWW -data or nginx. * * PHP can run system commands using WWW -data or nginx. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Git configuration file as follows: * ``` * + .ssh * - authorized_keys * - config * - id_rsa * - id_rsa.pub * - known_hosts * - .gitconfig * ``` * * 3. Git directory permissions are incorrect. Git directory permissions are incorrect. Fatal: Unable to create'/data/www/html/awaimai/.git/index.lock': Permission denied '* The PHP user does not have write Permission and needs to grant Permission to the directory: * ' '* sudo chown -r: WWW -data /data/ WWW/HTML /awaimai' * sudo chmod -r g+w /data/ WWW/HTML /awaimai * ' '* * 5. * 'Could not create directory' may be displayed if SSH authentication is used'/.ssh'*/ / The shell_exec function is disabled by default and needs to be modified in php.ini if it is not used. Add sudo permission to the user running PHP. For details, see Resourcesecho shell_exec("cd {$local} && sudo sh ./autoCompiled.sh");
die("done " . date('Y-m-d H:i:s', time()));
Copy the code
- autoCompiled.sh
#! /bin/sh
source/data/git/ emsdk_env.sh // Load the command. Otherwise, emcc and other compiled commands cannot be used in the command linecd/data/git/ webassembly-lib /Demo/ / Go to the directory where you need to run git pull rm-f src/version.h
git rev-list HEAD | sort > config.git-hash
LOCALVER=`wc -l config.git-hash | awk '{print $1}'`
if [ $LOCALVER\ > 1);then
VER=`git rev-list origin/master | sort | join config.git-hash - | wc -l | awk '{print $1}'`
if [ $VER! =$LOCALVER];then
VER="$VER+$(($LOCALVER-$VER))"
fi
if git status | grep -q "modified:" ; then
VER="${VER}M"
fi
VER="$VER $(git rev-list HEAD -n 1 | cut -c 1-7)"
GIT_VERSION=r$VER
else
GIT_VERSION=
VER="x"
fi
rm -f config.git-hash
cat version.h.template | sed "s/\$FULL_VERSION/$GIT_VERSION/g" > src/version.h
Compile and submit code
make
git add .
git commit -m 'Auto Compiled By handsomeTaoTao'
git push
Copy the code
Third, concluding remarks
At present, the demo only implements relatively simple functions, which can be further optimized. For example, when the browser accesses the address, it will display whether the last compilation was successful, and if it fails, it will display an error message, which will facilitate debugging.
Github, GitLab, and Gitee use Webhooks to automate code deployment