Home >Backend Development >PHP Tutorial >Use webhook to automatically deploy PHP projects

Use webhook to automatically deploy PHP projects

藏色散人
藏色散人forward
2020-01-03 17:54:223371browse

1. Let’s first talk about the principle of automatic deployment. Generally, when we push the code, we can automatically request the url set in the webhook to complete a request and response. So as long as the content of the PHP file requested by the url address we set is to execute the command line git push command, we can push the code to the remote end once every time locally, and the server will automatically git pull to pull the latest branch.

2. Then what we need to set are two places:

①The webhook settings of the git project:

Generally, on the platform where we host the code, there will be one in the project settings. There is usually only one piece of content in the webhook settings, which is the url setting. Here I set http://www.xxxxx.com/webhook.php

(obviously, I am in the root directory of the website Throwing a webhook.php file, then every time I push code to the remote end, the webhook.php file of my website will be accessed)

②The hook file on the server side (where the code runs) ( webhook.php) settings:

$command = 'cd 代码位置';
$output = shell_exec($command);
 
$command = 'git pull';
$output = shell_exec($command);
print $output;

Here are two simple sentences. First cd command to enter the corresponding directory, and then execute the git pull command

Mainly for the shell_exec() function Use, this is a function for PHP to execute the command line. Students can introduce it in detail on Baidu.

What I want to note here is that when I executed the shell_exec() command before, the server would become unresponsive and stuck, so I used the

$output = shell_exec('ls');
print_r($output);

command to test, and found that it also timed out, which was very strange, so I kept putting it aside and couldn't figure out what was going on. After all, it was only three or five lines of code, and I didn't know why it kept requesting timeout. . Two months later, I wondered if it was a problem with the string encoding of the original print? So there are the following lines of code:

$encode = mb_detect_encoding($output, array('ASCII','GB2312','GBK','UTF-8'));
$trans = iconv( $encode, "UTF-8",$output);
print $trans;

Then it can be executed. . . I can't remember why shell_exec() kept timing out before. Friends who encountered it again can try not to print the return content, or encode the printed content. However, I believe that there are still a small number of friends who encounter this disgusting problem.

For more PHP related knowledge, please visit PHP Tutorial!

The above is the detailed content of Use webhook to automatically deploy PHP projects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete