利用webhook 實現自動化部署 【PHP版本】

一、先git clone 代碼php

二、配置nginx,域名綁定到代碼目錄html

三、在代碼目錄建立 webhook.php (隨意命名)nginx

<?php
  $pwd = getcwd();
  $command = 'cd ' . $pwd . ' && git pull 2>&1'; // 2>&1 是輸出錯誤,有利於調試
  $output = shell_exec($command);
  file_put_contents('./webhook.log', $output);// 輸出內容保存到日誌,須要注意日誌文件要有足夠的權限
  print $output;
?>

四、配置碼雲webhook,url爲 http://yourdomain/webhook.phpgit

五、碰到問題了:git pull 須要輸入帳號密碼web

解決辦法:shell

執行git config --global credential.helper store命令
而後git push origin your-branch       // your-branch就是分支的名字
//會讓你輸入用戶名和密碼,就會保存起來,下次就不須要了

六、建立密鑰服務器

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter] // 這裏填寫 /home/www/.ssh/id_rsa,由於nginx用戶組是www,以後一路回車
$ eval $(ssh-agent -s)
$ ssh-add /home/www/.ssh/id_rsa

以後就複製 /home/www/.ssh/id_rsa_pub裏面的內容,添加到碼雲->設置->SSH公鑰裏面dom

注意:/home/www/.ssh 目錄用戶組:用戶須要設置爲, chown -R www:www /home/www/.sshssh

Git 常見問題解決url

Your local changes to the following files would be overwritten by merge: index1.html Please, commit your changes or stash them before you can merge

在服務器上能夠直接執行退回上一個版本,再拉取代碼(由於沒人會在服務器開發,代碼都在碼雲,不擔憂沒了)

git reset --hard 
git pull origin your-branch
相關文章
相關標籤/搜索