centos7.4新建git server + hook 同步代碼

我買了一臺阿里雲服務器,平時做做測試什麼的,需要代碼同步。

1.首先安裝的git.x86_64

yum install git.x86_64

2.創建用戶

groupadd git

useradd -g git -s /usr/bin/git-shell -M git

指定組和使用的shell和名稱

3.建立一個空的git倉庫

cd /home/git   //沒有就新建

git init --bare test.git

4.加權限

chown -R git:git test.git

5.git-shell-commond不能使用的問題

cp /usr/share/doc/git-1.7.4.4/contrib/git-shell-commands /home/git -R
chown git:developers /home/git/git-shell-commands/ -R
chmod +x /home/git/git-shell-commands/help
chmod +x /home/git/git-shell-commands/list

6.可以遠程拉取了

git clone [email protected]:/home/git/test.git

這樣需要輸入密碼,可以passwd git設置密碼,然後使用密碼拉取

7.不適用密碼,使用ssh拉取

在git用戶目錄,也就是/home/git下新建了/home/git/.ssh/authorized_keys文件,將想要拉取代碼的電腦裏的ssh公鑰新起一行加入剛纔服務器新建的文件裏面,一般在當前用戶~/.ssh/id_rsa.pub。

7.hook使用

我之前新建git用戶,設置只能使用git-shell,結果hook怎麼都不成功。嘗試了好多次,方法如下:

第一步生成git用戶的ssh祕鑰:

vim /etc/passwd,修改git用戶shell爲/bin/bash,最後操作完的時候需要改回/usr/bin/git-shell,爲了安全。

切換用戶:su git

生成ssh祕鑰:ssh-keygen,選擇git的用戶目錄,默認即可。

此時換回root用戶,修改git用戶的操作shell。

將git用戶的id_rsa.pub拷貝到git的authorized_keys中,

cat /home/git/.ssh/id_rsa_pub

拷貝

vim /home/git/.ssh/authorized_keys

粘貼

第二步:設置代碼權限

test目錄是我提前拉取的代碼目錄,也可以腳本里面編寫

拉取代碼:git clone git@ip:/home/git/test.git /opt/www/test

chown -R git:git /opt/www/test

第三步編寫hook腳本:

cd /home/git/test.git/hook

cp post-update.sample post-update

chmod a+x post-update

vim post-update

#!/bin/bash

unset GIT_DIR
GIT_DIR=/opt/www/test

cd $GIT_DIR
git pull

 

 

下面是新建ftp用戶的過程

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