Centos6下搭建git服務器,并完成git自動部署

搭建git服務器

sever

1.首先安裝git

yum install git

2.新建一個linux用戶,起名為git

adduser git

3.在git用戶目錄中新建目錄 .ssh

cd /home/git
mkdir .ssh

4.在/home/git/.ssh/目錄中新建authorized_keys文件,并將客戶端提供的公鑰(id_rsa.pub)黏貼到該文件中

vim authorized_keys

5.在項目目錄創(chuàng)建一個git裸倉庫,假如當前項目目錄為/home/git/project.git

git init --bare project.git

6.將項目目錄和git用戶目錄下的.ssh目錄的所有者和所屬組都設置成git

chown -R git.git project.git  
chown -R git.git /home/git/.ssh/

7.為了安全考慮,禁用git用戶的shell登錄

vim /etc/passwd
注釋 #git:x:500:500::/home/git:/bin/bash 
改為 git:x:500:500::/home/git:/usr/bin/git-shell

8.git服務器打開RSA認證

vim /etc/ssh/sshd_config
下面3個打開
 1.RSAAuthentication yes     
 2.PubkeyAuthentication yes     
 3.AuthorizedKeysFile  .ssh/authorized_keys

client

1.查看公鑰

cat ~/.ssh/id_rsa.pub

如果沒有的話,可以執(zhí)行以下命令

ssh-keygen -t rsa

2.在本地新建git倉庫

git init

3.新建一個文件并推送到服務器

touch readme.txt
git add readme.txt
git commit -m "readme"
git remote add origin git@xxx.xxx.xxx.xxx:/home/git/project.git
git push origin master

注:如果提示需要密碼,請檢測公鑰是否配置成功或RSA是否開啟。

報錯信息為ssh: connect to host 104.224.152.22 port 22: Connection refused的時候注意下,sshd服務是否開啟(一般都是默認開啟的)

sever端解決辦法

這個時候,我們要檢查sshd服務的端口是否為22

netstat -lnp|grep 22

sshd服務的端口號不為22,我們可以在/etc/ssh/sshd_config修改默認端口

client端解決辦法

1.直接修改URL為SSH://開頭

git remote set-url origin ssh://git@domain.com:3022/~/Projects/p1.git

2.修改本地配置文件

vim ~/.ssh/config
# 映射一個別名
host newdomain
hostname domain.com
port 3022

git自動部署

添加鉤子文件post-receive

#!/bin/bash 
IS_BARE=$(git rev-parse --is-bare-repository) 
if [ -z "$IS_BARE" ]; 
then echo >&2 "fatal: post-receive: IS_NOT_BARE" exit 1 
fi unset GIT_DIR DeployPath="/var/www/blog" #這里寫項目實際部署的目錄 
cd $DeployPath 
git fetch --all
git reset --hard origin/master

服務器端創(chuàng)建部署項目的文件

cd /var/www
git clone /home/git/project.git 項目名

注:權限問題
在實際使用的時候,會遇到Permission Denied 之類的事情。
那么你要檢查下 /var, /var/www, /var/www/your_git 三個目錄的權限是否至少開到了 770 上. 然后還要考慮是否有 SELinux 在擋道。
我的處理方式是權限全部開啟(因為是個人的服務器,而且也沒什么人訪問,主要是拿來玩的)

chmod -R 777 /var/www/xxx

還有一個問題,就是push和clone的時候,倉庫是最開始創(chuàng)建的空倉庫(本文是/home/git/project.git)

最后還有一個問題
Git: push 出錯的解決 master -> master (branch is currently checked out)
在使用Git Push代碼到數(shù)據(jù)倉庫時,提示如下錯誤:

[remote rejected] master -> master (branch is currently checked out)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git@xxx.xxx.xxx.xxx:/xxx/xxxx/xxxx
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@xxx.xxx.xxx.xxx:/xxx/xxxx/xxxx'

這是由于git默認拒絕了push操作,需要進行設置,修改.git/config添加如下代碼:

git config receive.denyCurrentBranch ignore

參考資料:

Centos搭建GIT服務器---老高的技術博客
利用Git自動部署環(huán)境
GIT服務器實現(xiàn)web代碼自動部署
Git服務器端代碼自動部署

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容