zoukankan      html  css  js  c++  java
  • 使用git做服务器端代码的部署

    传统部署方案
         windows 远程桌面
         FTP/SFTP
         登录服务器pull github代码
         Phing(PHP专业部署工具)

    git 自动部署流程图
         

     



    服务器端准备工作:
         0. 这些工作都在root或有管理权限的帐号下进行,下面以root为用户,切换到其他用户的时候会提示
         1. 确保安装了git
         2. 为了安全起见,新建一个专门用于代码部署的无特权用户
                    useradd -m deployuser
                    passwd deployuser #设置该用户的密码,也可根据喜好配置成免密码登陆
         3. 新建一个目录作为要部署代码的根目录,如:
                    mkdir /var/www/html/deploy
         4. 将这个目录的属主和属组都改为上面新建的用户deployuser
                    cd /var/www/html
                    chown deployuser:deployuser deploy
         5. 切换到部署代码的专用用户
                    su deployuser
         6. 进入项目根目录,初始化为git仓库
                    cd deploy
                    git init
         7. 【重要】让仓库接受代码提交
                    git config receive.denyCurrentBranch ignore
                    [可选] git config core.worktree ~/www
                    [可选] git config --bool receive.denyNonFastForwards false #禁止强制推送
         至此,一个空的git仓库就在服务器上建好了,仓库的地址为:
                    ssh://deployuser@ipaddress/var/www/html/deploy/.git

    本地仓库准备工作:
         1. 通过 git clone 或 git pull 从 github 仓库上将代码获取到本地
         2. 将服务器添加到远程仓库列表,使用名字来区分不同的服务器,如测试服务器可以叫做testing
                    git remote add testing ssh://deployuser@ipaddress/var/www/html/deploy/.git
         3. 将本地代码提交到测试服务器上面
                    git push testing master

    回到服务器端:
        1. 更新服务端 git 仓库状态并检出文件
                    cd /var/www/html/deploy
                    git update-server-info

                    git checkout -f
            OR:
                    git checkout branch_name # 如果push的不是master分支

        2. 检查是不是将文件更新进来了
        3. 设置服务器端更新钩子 post-update
                  cd .git/hooks
           新建 post-receive 或将 post-receive.sample 重命名为 post-receive
                  touch post-receive
           OR:
                  mv post-receive.sample post-receive
                  vim post-receive
           将如下内容复制到文件中
                     #!/bin/sh
                     unset GIT_DIR
                     cd ..
                     git checkout -f
           注: 第3步的操作将post-receive 替换为 post-update也可以, 不过需要先将post-update中的exec git update-server-info这一行删掉

    后续代码的更新:
         1. github 有更新的时候 pull 更新本地部署仓库
         2. 然后本地先 push 到测试服务器进行测试
         3. 测试通过之后 push 到正式服务器进行上线
         4. 代码的回滚:
                   *服务器端回滚:推荐 git reset --hard HEAD^
                   本地仓库回滚: 无需登陆服务器即可实现代码回滚,git reset HEAD^ 保留代码回滚,然后使用 git push remote_name local_branch_name -f 强制推送

    使用过程中需要注意的问题:
         1. 需要约定好 git 不能更新的操作要怎么处理,比如新增数据库的字段,新安装必要的扩展等,
         2. 充分利用好 git 的钩子功能,比如 pre-commit 可用于提交代码前进行单元测试等,但是钩子做的操作要尽量简单

    git其他操作
         存储当前的版本号,如:git log –pretty -1 | awk ‘/^commit/{print $2}’

  • 相关阅读:
    mac c++编译出现segmentation fault :11错误
    ssh 连接缓慢解决方法
    237. Delete Node in a Linked List
    203. Remove Linked List Elements
    Inversion of Control Containers and the Dependency Injection pattern
    82. Remove Duplicates from Sorted List II
    83. Remove Duplicates from Sorted List
    SxsTrace
    使用CCleaner卸载chrome
    decimal and double ToString problem
  • 原文地址:https://www.cnblogs.com/navysummer/p/7818438.html
Copyright © 2011-2022 走看看