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

1、先git clone 代碼

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

3、在代碼目錄創建 webhook.php (隨意命名)

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

4、配置碼雲webhook,url爲 http://yourdomain/webhook.php

5、碰到問題了:git pull 需要輸入賬號密碼

解決辦法:

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

6、創建密鑰

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
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公鑰裏面

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

Git 常見問題解決

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章