在centos上搭建git伺服器並自動同步程式碼
參考文章
CentOS安裝Git實現多人同步開發
centos中GIT伺服器搭建及使用金鑰連線
簡述
1、伺服器上安裝Git依賴及Git
2、建立Git使用者及所屬組
3、伺服器上初始化Git倉庫
4、安裝Git客戶端並生成公鑰
5、建立證照登入
6、使用Git Bash克隆伺服器上的空倉庫
7、將本地庫專案推送到伺服器
1、伺服器上安裝Git以及依賴
1.1安裝Git依賴
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
1.2.安裝Git
yum install -y git
2、建立”使用者組“和”使用者“,用來執行git服務
2.1建立使用者組
groupadd git
2.2新增git使用者組下的使用者
adduser phper -g git
2.3為使用者名稱為phper 的使用者設定密碼
passwd phper
Changing password for user git.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
3、建立git倉庫
mkdir gitroot
chmown phper:git gitroot
cd /gitroot
git init --bare project.git
chmown -R phper:git project.git
chmod 774 -R project.git
cd ../
chmod 750 gitroot
4、安裝Git客戶端並生成公鑰
4.1下載git客戶端安裝好後右鍵選擇Git GUI Here->Help->Show SSH Key


就能得到私鑰和公鑰

開啟Puttygen



得到格式化後的私鑰,點選儲存私鑰


4.2建立證照登入
切換到phper目錄
cd /home/phper
mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
然後將客戶端的公鑰上傳到.ssh目錄
cd .ssh
rz
將公鑰新增進authorized_keys中
cat id_rsa.pub >> authorized_keys
5、克隆到本地

這樣就把伺服器的空倉庫拉取下來了。

現在我們能夠正常的提交程式碼到伺服器的git,但是還不能自動同步,我們還需要修改伺服器的hooks/post-receive檔案。具體post-receive內容
#!/bin/sh
unset GIT_DIR
DeployPath="/alidata/www/project"
LogPath="/alidata/gitroot/project.git/hooks"
echo -e "\n================= `date +"%Y-%m-%d %H:%M:%S"` ===============\n" >> $LogPath/gitsync.log 2>&1
cd $DeployPath
#git stash
#先拉取再合併
git pull origin master >> $LogPath/gitsync.log 2>&1
#強制與遠端伺服器同步,不與本地合併,只能通過提交的客戶端提交的方式修改程式碼。
#git fetch --all
#git reset --hard origin/master
#time=`date`
#echo "web server pull at webserver at time: $time."
echo "================================================" >> $LogPath/gitsync.log 2>&1
更改post-receive的所有者和許可權
chmod -R 774 post-receive
chown phper:git post-receive
建立gitsync.log日誌檔案
touch gitsync.log
chmod 770 gitsync.log
chown phper:git gitsync.log
最後在www下
cd /alidata/www
git clone /gitroot/project.git
chown -R phper:git project
chmod -R 774 project
好了git的同步就弄好了
如果hooks同步不起作用,開啟hooks/gitsync.log檢視錯誤日誌
可能預見的錯誤
1、fatal: /usr/libexec/git-core/git-pull cannot be used without a working tree.
同步的專案資料夾沒有建立---------------解決辦法:在www下建立project專案資料夾
2、fatal: Not a git repository (or any of the parent directories): .git
專案檔案裡沒有git初始化------------------解決辦法:在www路徑下執行git clone /alidata/gitroot/project.git
3、error: cannot open .git/FETCH_HEAD: Permission denied
git在專案目錄沒有寫入許可權---------------解決辦法:修改所有者以及許可權 chown -R phper:git project / chmod -R 774 project
4、每次pull push的時候還是要輸入密碼
image.png
祕鑰沒有起作用-----------------------------解決辦法:/var/log/secure檢視一下日誌,是否是.ssh的許可權問題
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
相關文章
- 在CentOS上搭建git伺服器CentOSGit伺服器
- 私有伺服器上搭建 Git 伺服器,並實現客戶端提交程式碼後自動同步到站點目錄伺服器Git客戶端
- 自動佈署伺服器環境,並利用 Git 實現原生程式碼自動同步到伺服器!伺服器Git
- 在centos7搭建git程式碼倉庫CentOSGit
- Centos搭建git伺服器CentOSGit伺服器
- 使用certbot在centos7搭建ssl證書並且自動續約CentOS
- 在伺服器上使用 smart http 搭建 Git 伺服器伺服器HTTPGit
- 在CentOS上安裝GitCentOSGit
- 使用 git hook 實現伺服器程式碼自動部署GitHook伺服器
- git 自動上傳程式碼到遠端倉庫Git
- 使用 git 自動部署程式碼Git
- Centos系統伺服器設定時間自動同步!CentOS伺服器
- 手摸手教你搭建簡單的 Git 的程式碼自動釋出Git
- 工程管理系列之----從零開始搭建git伺服器,及程式碼同步完整版Git伺服器
- 在Visual Studio 中使用git——同步到遠端伺服器-上(十一)Git伺服器
- centos7 時間自動同步CentOS
- Git 如何同步上游分支程式碼?Git
- 在CentOS系統搭建NFS伺服器CentOSNFS伺服器
- 在centos上搭建《異星工廠》(Factorio)伺服器端小記CentOS伺服器
- 在CentOS 7上搭建Docker環境CentOSDocker
- 手摸手帶你部署git伺服器實現自動釋出程式碼Git伺服器
- CentOS7使用NTP搭建時間同步伺服器CentOS伺服器
- 在CentOS7上搭建Jenkins+Maven+Git持續整合環境的方法CentOSJenkinsMavenGit
- 同步線上伺服器程式碼到web節點指令碼伺服器Web指令碼
- git 同步fork的倉庫程式碼Git
- CentOS 7 jenkins 程式碼構建同步CentOSJenkins
- phpstorm2020將原生程式碼實時自動同步到遠端伺服器PHPORM伺服器
- 利用 webhook 實現 Git 自動部署 Laravel 程式碼WebHookGitLaravel
- Git 倉庫遷移至其他伺服器並實現 hooks 自動化部署Git伺服器Hook
- Travis-CI自動化測試並部署至自己的CentOS伺服器CentOS伺服器
- 搭建安卓原始碼伺服器,repo+gerrit+git環境,程式碼稽核安卓原始碼伺服器Git
- 程式碼風格統一: 使用husky, prettier, eslint在程式碼提交時自動格式化,並檢查程式碼。EsLint
- fir.im自動上傳apk並在釘釘通知APK
- Gitee Webhook 實現自動拉取程式碼並編譯程式碼GiteeWebHook編譯
- CentOS7 jenkins 程式碼構建同步CentOSJenkins
- Git 伺服器搭建Git伺服器
- Linux構建Git程式碼倉庫與自動部署LinuxGit
- 在CentOS7.5上搭建Hadoop3.xCentOSHadoop
- CentOS上搭建SFTPCentOSFTP