Webhook Practice--php Automatic deployment
1. Deploy Gogs
Reference blog: use Gogs to build your own Git server
2. Add the SSH public key of Git
Because it is a gogs deployed with a git user, the next step is to configure the SSH public key on the server with a git account.
First generate the secret key on the host:
[[email protected] ~]$ ssh-keygen -t dsa -p "" -f ~/.ssh/id_ Dsagenerating public/private dsa key pair. created directory '/home/xiaozhenkai/.ssh '. Your identification has been saved in /home/xiaozhenkai/.ssh/id_dsa. Your public key has been saved in /home/xiaozhenkai/.ssh/id_dsa.pub.the key fingerprint is:ec:ca:56:5d:75:5a:3a:71:e2:d7:a6:1e:1e:4d:ba:eb [email protected] The key ' s randomart image is:+--[ dsa 1024]----+| | | + +| | o o.| | . . = =| | s. . o | |       &NBSP, .... . = .| |       &NBSP, .... o + | | ... + | | .o . e. |+-----------------+
Copy the host secret key
[[email protected] .ssh]$ cat ~/.ssh/id_dsa.pub ssh-dss aaaab3nzac1kc3maaacbapc /kogp7piw2hwbzredf9omnh/uqutk9pfowkw796/eroluze8on+ibzkhgjt+/ chrqbesgku1qj4bvsdaojxlogfkpzmbsweo3ainwqx44dnxgo8itg2ss6okcsuj8obiobycp4ki6gbdlsnxu4b/ bkbve0trbejevperfp40xaaaafqdct3x9teze15jwxlvspuiur/mg9waaaiea0da28/ Qdpnrvj5x2t3jubb2ekga969kwduhqv618s5doikwvqhurwlxq1/pjazeaggunfmjsxtsrxbtdnes7psosntfkbcztvnpyd5zd+ omr6znsphxtkudupk/zr6k2gristd+otnqxsux2h7wafworjytc0ichckpud1acbrwaaacay8b/zcuo0gxayd/ Wmsouszsuxa4wfvyfkfm9qvexudv91bfqhbnddpmkxgdqh2gocghd4cjx1pebmbnkysft0latekiyvn6tnvl+yoebqt77hvid/ Xdxf8wiztz0l6bl1k8xc7timhbkw9dngifyuanhww+ozfu2x9t51pvslna= [email protected]
Login Gogs, the user set the--SSH key--Add the secret key, and then added the copied master key to the Gogs.
3, Configuration Webhook
First, a server that responds to Webhook is configured on the server
A PHP file in response to Webhook, the file content is as follows:
<?php//git webhook Automatic deployment script//Project Storage physical path $path = "Your_git_path"; $requestBody =file_get_ Contents ("Php://input");if (Empty ($requestBody)) { die (' Send fail ');} $content = json_decode ($requestBody, true); Var_dump ($content);; /If the main branch and the number of commits is greater than 0//if ($content [' ref ']== ' refs/heads/master ' && $content [' Total_commits_count '] >0) {if ($content [' ref ']== ' Refs/heads/master ') { $res = Shell_exec ("cd {$path}&& git pull 2>&1");//with Nginx user run $res _log = '-------------------------'. php_eol; $res _log .= $content [' user_name '] . ' in ' . date (' y-m-d h:i:s ') . ' to ' . $content [' Repository '] [' name '] . ' project ' $content [' Ref '] . ' branch push ' . $content [' Total_commits_cOunt '] . ' a commit: ' php_eol; $res _log .= $res. php_eol; echo $res _log; file_put_contents ("Git-webhook.txt", $res _log, file_append);//Append write }
Note: PHP functions cannot disable shell_exec, and there is no way to execute system commands after disabling.
This article is from the "Ericshaw notes" blog, so be sure to keep this source http://xiaozhenkai.blog.51cto.com/1029756/1949841
Webhook Practice--php Automatic deployment