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

搭建git服務器

sever

1.首先安裝gitphp

yum install git

2.新建一個linux用戶,起名爲githtml

adduser git

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

cd /home/git
mkdir .ssh

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

vim authorized_keys

5.在項目目錄建立一個git裸倉庫,假如當前項目目錄爲/home/git/project.gitweb

git init --bare project.git

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

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

7.爲了安全考慮,禁用git用戶的shell登陸vim

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.查看公鑰bash

cat ~/.ssh/id_rsa.pub

若是沒有的話,能夠執行如下命令服務器

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

服務器端建立部署項目的文件

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的時候,倉庫是最開始建立的空倉庫(本文是/home/git/project.git)

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

[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自動部署環境
GIT服務器實現web代碼自動部署
Git服務器端代碼自動部署

相關文章
相關標籤/搜索