配置文件管理方案

2017-09-29  本文已影响0人  天空中的海豚

git hooks 使用

配置管理示意图

image.png

git hooks 使用说明

服务器 功能 目录结构
10.90.11.227 系统管理员本地仓库 /software/process_config
172.31.4.123 中转仓库/数据存放仓库 中转仓库: /software/workspace/project 数据存放仓库: /software/workspace/project-local/project
  1. 初始化中转仓库

    git init 和 git init --bare 的区别
    初始化出来的仓库是不一样的,前者初始化的是一个普通的仓库,其中 .git 文件夹是隐藏的,并且能看见该仓库下所有的源码。而后者初始化出来的仓库中的文件,就是 .git 中的文件夹,但不能像前者那样直接浏览或修改仓库中的代码。

    $ mkdir /software/workspace/project
    $ cd /software/workspace/project
    $ git init --bare
    **结果:**
    -rw-r--r--  1 root root   23 Apr 25 06:10 HEAD
    drwxr-xr-x  2 root root    6 Apr 25 06:10 branches
    -rw-r--r--  1 root root   66 Apr 25 06:10 config
    -rw-r--r--  1 root root   73 Apr 25 06:10 description
    drwxr-xr-x  2 root root 4096 Apr 25 10:27 hooks
    drwxr-xr-x  2 root root   20 Apr 25 06:10 info
    drwxr-xr-x 41 root root 4096 Apr 25 10:26 objects
    drwxr-xr-x  4 root root   29 Apr 25 06:10 refs
    
  2. 初始化数据存储仓库--其实就是做中转仓的检出

    $ cd /software/workspace/project-local
    $ git clone /software/workspace/project
    drwxr-xr-x 6 root root 72 Apr 26 00:08 project
    
  3. 配置中转仓-- hooks

    $ cd /software/workspace/project/hooks
    $ cat post-receive # 系统管理员每次提交时指定 remote 仓库 后自动触发 hooks 执行以下代码 必须有执行权限
    #!/bin/sh
    cd /software/workspace/project-local/project  || exit
    echo ${GIT_DIR}
    unset GIT_DIR
    /usr/bin/git  pull
    echo "pull successful 172.31.4.123"
    
  4. 系统管理员本地仓库添加remote 源

    # 克隆版本库的时候,所使用的远程主机自动被Git命名为origin。如果想用其他的主机名,需要用git clone命令的-o选项指定。
    $ git clone -o source http://gitlab.product.co-mall:10080/liuyulong/process_configure.git
    $ git remote add deploy root@172.31.4.123:/software/workspace/project # root为系统账户
    $ git remote -v
    deploy  root@172.31.4.123:/software/workspace/project (fetch)
    deploy  root@172.31.4.123:/software/workspace/project (push)
    source  http://gitlab.product.co-mall:10080/liuyulong/process_configure.git (fetch)
    source  http://gitlab.product.co-mall:10080/liuyulong/process_configure.git (push)
    
  5. 系统管理员本地仓库提交测试

    $ vim nginx.conf
    $ git add .
    $ git commit -m "test hooks"
    [master 7b05314] test hooks
     1 file changed, 1 insertion(+)
    $ git push deploy # 此处如果想直接全部提交到远程仓库进行如下配置
    ########
     git remote set-url --add --push origin http://gitlab.product.co-mall:10080/liuyulong/process_configure.git
     git remote add both root@172.31.4.123:/software/workspace/project
      git remote set-url --add --push both http://gitlab.product.co-mall:10080/liuyulong/process_configure.git
     git remote set-url --add --push both root@172.31.4.123:/software/workspace/project
    git push both
    ########
    Counting objects: 4, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 457 bytes | 0 bytes/s, done.
    Total 4 (delta 1), reused 0 (delta 0)
    # 以下都是自定义 hooks 脚本返回的信息
    remote: .
    remote: 来自 /software/workspace/project
    remote:    7c07381..7b05314  master     -> origin/master
    remote: 更新 7c07381..7b05314
    remote: Fast-forward
    remote:  nginx/nginx.conf | 1 +
    remote:  1 file changed, 1 insertion(+)
    remote: pull successful 172.31.4.123
    To root@172.31.4.123:/software/workspace/project
       7c07381..7b05314  master -> master
    lonny@LonnyLiuMacPro ~/Documents/process_configure/nginx (git)-[master] %
    
  6. 配置文件管理完毕

上一篇 下一篇

猜你喜欢

热点阅读