写在前面,此文承接 在LINUX(CENTOS7.4)上搭建GIT服务器和配置GITOLITE权限管理  ,这里主要用shell脚本自动创建仓库,和自动部署网站目录:

例如上文提到的gitosis.conf中的project1的项目为例:
[group mobile]
writable = project1
members = test1 test2

此项目已经配好,才能如下操作

这里写了两个脚本先执行 aftergit.sh 再执行 hookgit.sh 执行之前先修改符合自己的配置

下面贴一下代码

aftergit.sh :

#!/bin/bash

action=$1
gitDirRoot="/data/gits"

Display_Menu()
{
    echo ""
    echo "#####################################################"
    read -p "请输入仓库名称(0或exit直接退出): " action
}


echo "+-----------------------------------------------------------------------+"
echo "|            创建git仓库,前提是gitosis-admin中本地有权限                 |"
echo "+-----------------------------------------------------------------------+"

    
    Display_Menu

    case "${action}" in
            0|[eE][xX][iI][tT])
                exit 1
                ;;
            *)
                echo "创建项目:${action}"
                cd $gitDirRoot
                mkdir ${action}
                cd ${action}
                touch README
                git init
                git remote add origin git@localhost:${action}.git
                git add .
                git commit -am '初始化'
                git push origin master
                echo "创建成功"
                exit 0
                ;;
    esac

下面是:hookgit.sh

#!/bin/bash

#第一个参数来填写要配置的仓库名称
action=$1
#真实仓库的根目录
GITROOT="/home/git/repositories/"
#web所有项目根目录
WEBROOT="/home/wwwroot/"

Display_Menu()
{
    echo ""
    echo "#####################################################"
    read -p "请输入仓库名称(0或exit直接退出): " action
}


echo "+-----------------------------------------------------------------------+"
echo "|            此程序是git自动部署项目(目录依赖gitosis)                    |"
echo "+-----------------------------------------------------------------------+"

    
#初始化action
Display_Menu

case "${action}" in
        0|[eE][xX][iI][tT])
            exit 1
            ;;
        *)
            echo "部署web项目:${WEBROOT}${action}"
            cd $WEBROOT
            git clone "git@127.0.0.1:${action}.git"
            chown git:git -R $action
            cd "${GITROOT}${action}.git/hooks/"
            cat >post-receive<&1
cd \$CURRETNPATH
exit 0
EOF
            chmod +x post-receive

            exit 0
            ;;
esac

注意:cat中的\是为了不转义变量 打包好的文件 git

温馨提醒:如果上面的代码部署好之后,在运行的时候出现这种错误:

git.exe push –progress “origin” master:master

Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: HEAD 现在位于 af9cecc pro
remote:
remote: fatal: Could not read from remote repository.
remote:
remote: Please make sure you have the correct access rights
remote: and the repository exists.
To 192.168.0.181:project1.git
af9cecc..5a74a94 master -> master

成功 (750 ms @ 2018/12/24 10:10:45)

问题是你的git用户并没有生成ssh密钥和公钥,先生成一个,然后放入gitosis-admin的keydir并在gitosis.conf配置好权限。

并且你要在web项目目录下用git账号手动 git pull 会有一个提示

The authenticity of host ‘127.0.0.1 (127.0.0.1)’ can’t be established.
ECDSA key fingerprint is SHA256:K7ts/5QEaRsz8ksf1Cwul9ILHOw25yPZntZUE1jGWx4.
ECDSA key fingerprint is MD5:e4:a6:ad:ee:6c:77:44:76:17:3a:3f:a5:a0:13:e6:3f.
Are you sure you want to continue connecting (yes/no)?

输入yes并回车就可以了。(感觉不是很智能,不过以后就不用手动pull了)

生成密钥参考:LINUX实现SSH免密码登录

Comments are closed.

Post Navigation