Git服务器端代码自动部署

偶尔善良 提交于 2019-12-05 16:19:37

创建服务器端代码托管仓库(远程连接仓库)

给服务器创建git用户

adduser git

# 给git用户添加密码
passwd git

添加要登录用户的公钥

mkdir /home/git
mkdir .ssh
touch authorized_keys

#使用vim将公钥添加到authorized_keys文件中,一行一个

Ubuntu公钥生成:http://blog.csdn.net/mrgong_/article/details/78693283
Windows公钥:C:\Users\用户名.ssh

选定目录作为服务器端代码托管文件夹(裸仓库)

mkdir /opt/code
cd /opt/code
git init --bare gongsc.git

添加钩子文件

1、创建post-receive

    cd /opt/code/gongsc.git 
    touch post-receive 

2、使用vim打开post-receive将如下shell写入文件中

    #!/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/gongsc"#这里写项目实际部署的目录 
    cd $DeployPath 
    git fetch --all 
    git reset --hard origin/master

3、修改post-receive文件权限

    chmod +x post-receive

服务器端创建部署项目的文件夹

这个目录是实际运行的线上代码

cd /var/www/
git clone /opt/code/gongsc.git

Git客户端设置

将远程仓库进行克隆

git clone git@IP:/opt/code/gongsc.git

创建测试文件,上传到远程仓库

#进入gongsc目录创建一个测试文件index.html 
git add . 
git commit -m 'create new file' 
git push

转:http:/ /www.imooc.com/article/17874?block_id=tuijian_wz

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!