zoukankan      html  css  js  c++  java
  • git设置hooks 钩子

    github是可以设置hooks的,看:在设置webhooks & services,可在Just the push event.是设定向你的服务器发请求,然后再做相应的处理。

    https://help.github.com/articles/creating-webhooks

    看文档:man githooks

    NAME
    githooks - Hooks used by Git

    SYNOPSIS
    $GIT_DIR/hooks/*

    DESCRIPTION
    Hooks are little scripts you can place in $GIT_DIR/hooks directory to trigger action at certain points. When git init is run, a handful of example hooks
    are copied into the hooks directory of the new repository, but by default they are all disabled. To enable a hook, rename it by removing its .sample
    suffix.

    Note
    It is also a requirement for a given hook to be executable. However - in a freshly initialized repository - the .sample files are executable by
    default.

    This document describes the currently defined hooks.

    git hook 自动布署代码

    假设你的项目也是跑在此台服务器上,那自动布署代码就很简单了,比如你的在线服务代码在 /var/www/demo 文件夹中。

    /var/www/demo也要有写权限

    你先初始化代码库:

    $ git clone /opt/git/gitdemo /var/www/demo
    

    然后你可以通过 git pull 来更新代码。

    当然这样是手动了,我想要的是本地提交更新后,服务器能自动的 git pull代码到最新,于是我们就要借助 git hook了。

    进入到 /opt/git/gitdemo 文件夹中,会发现 .git/hook 文件夹在里面,进入到 hook 中,里面有很多的 sample 脚本,这里我们只需要用到 post-update。

    $ mv post-update.sample post-update $ vim post-update
    

    可以看到里面其实就是一些shell脚本,你要做的就是把 git pull写进去。当用户提交后,便会调用post-update脚本的。

    比如我在服务器的代码库更新后,要求对应的服务代码也要更新(进行pull操作),
    则在bare仓库的hooks中的post-receive添加如下内容即可

    #!/bin/sh 
    unset $(git rev-parse --local-env-vars)
    cd WEB_DIR
    git pull
    

    这些脚本显然是可以做很多事的,只要你想得到,要了解各脚本何时调用,google吧。 

    注:服务器中与git用户有关的文件夹及文件,

    $ chown -Rh git:git /your/git/dirs

    另外文章:

    最佳工具 git hook 

    post-update.sample 改名为post-update

    然后加几行简单的代码就能实现你的需求了

    例:

    gitdir=/****

    cd $gitdir

    git checkout 对应分支

    git pull

    end...

  • 相关阅读:
    ASP.NET 2.0中使用webpart系列控件
    ASP.NET AJAX入门系列(11):在多个UpdatePanle中使用Timer控件(转)
    WebService SoapHeader的使用
    (转)对程序员职业的一些建议
    android组件间通信
    关于android library project
    (转)我6个月的学习编程经历:从”大齿怪“到“狂欢者”
    DOM的一个要注意的地方
    android的休眠对service,activity的影响
    java的字符串和char数组
  • 原文地址:https://www.cnblogs.com/youxin/p/5514645.html
Copyright © 2011-2022 走看看