git hook service code

// The git webhook automatic deployment script 
// The project stores the physical path. When cloning for the first time, the directory must be empty 
$ savePath = "/ data / wwwroot / www" ;
 $ gitPath = "[email protected]: test / spa.git "; // Code warehouse 
 
$ name =" tom "; // User warehouse mailbox 
$ email =" [email protected] "; // Warehouse user name, generally the same as the mailbox 
$ password = '123456' ; 
 
$ isCloned = true ; // Set whether has 
 
cloned to local, true: cloned , pull directly, false: clone first. 
// If cloned , then directly pull the code if ( $ isCloned ) {
     $ requestBody = file_get_contents ("php: // input" );
    if ( empty ( $ requestBody )) {
         die ('send fail' ); 
    } 
 
    // Analyze the JSON information notified by the Git server 
    $ content = json_decode ( $ requestBody , true );
     if ( $ content ['password']! = $ password ) {
         exit ('deny' ); 
    } 
 
    // If it is the master branch and the number of commits is greater than 0 
    if ( $ content ['ref'] == 'refs / heads / master' && $ content ['total_commits_count']> 0 ) { 
 
        $ res = PHP_EOL. "pull start --------". PHP_EOL ;
         $ res . = shell_exec ("cd { $ savePath } && git pull { $ gitPath }"); // Pull the code 
        $ res_log = '--- ---------------------- '. PHP_EOL ;
         $ res_log . = $ Content [' user_name '].' In '. Date (' Ymd H: i: s ').' direct '. $ content [' repository '] [' name '].' project '. $ content [' ref '].' branch pushed '. $ content [' total_commits_count '].' number commit: ' ;
        $res_log .= $res . PHP_EOL;
         $ res_log . = "pull end --------". PHP_EOL ;
         file_put_contents ("git-webhook_log.txt", $ res_log , FILE_APPEND); // Write log to log file 
    } 
} else {
     $ res = "clone start --------". PHP_EOL ;
     // Note: You need to set the user's mailbox and user name here, you can't pull the code if you don't then face 
    $ res . = shell_exec ("git config --global user .email { $ email }} "). PHP_EOL ;
     $ res . = shell_exec (" git config --global user.name { $ name }} "). PHP_EOL;
     $ res . = shell_exec ("git clone { $ gitPath } { $ savePath }"). PHP_EOL ;
     $ res . = "clone end --------". PHP_EOL ;
     file_put_contents ("git-webhook_log.txt ", $ res , FILE_APPEND); // write log to log file 
}

Reprinted: https://blog.haohtml.com/archives/17553

Guess you like

Origin www.cnblogs.com/hardykay/p/12712322.html