V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
garsday
V2EX  ›  问与答

关于 gitbook 使用 githook 脚本自动化部署的问题

  •  
  •   garsday · 2016-12-28 18:47:59 +08:00 · 3352 次点击
    这是一个创建于 2675 天前的主题,其中的信息可能已经有所发展或是发生改变。

    hook 脚本的内容如下,想要实现,更新服务器上的项目,然后,用 gitbook build ,生成新的静态文件。

    #!/bin/sh
    
    echo "$USER"
    
    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="/Data/www/java_guide"
    
    echo "==============================================="
    cd $DeployPath
    echo "deploying the test web"
    
    #git stash
    
    #git pull origin master
    
    echo "$USER"
    expect -c "
            set timeout 5;
            spawn git pull;
            expect {
                    "Username" { send \"username\r\"; exp_continue }
                    "Password" { send \"123456\r\" }
            };
            expect eof;
            spawn gitbook build;
            expect "success";
            expect eof;
    "
    
    echo "$USER"
    sleep 10
    
    time=`date`
    echo "web server pull at webserver at time: $time."
    echo "================================================"
    

    然后,我自己电脑上进行 push 后,显示的结果

    Username for 'http://git.domain.dev': username
    Password for 'http://[email protected]': 
    Counting objects: 3, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 372 bytes | 0 bytes/s, done.
    Total 3 (delta 1), reused 0 (delta 0)
    remote: www-data
    remote: ===============================================
    remote: deploying the test web
    remote: www-data
    remote: spawn git pull
    remote: Username for 'http://git.domain.dev': username
    remote: Password for 'http://[email protected]': 
    remote: spawn gitbook build
    remote: /usr/local/lib/node_modules/gitbook-cli/node_modules/fs-extra/lib/mkdirs/mkdirs-sync.js:41
    remote:         if (!stat.isDirectory()) throw err0
    remote:                                  ^
    remote: 
    remote: Error: EEXIST: file already exists, mkdir '/var/www/.gitbook'
    remote:     at Error (native)
    remote:     at Object.fs.mkdirSync (fs.js:794:18)
    remote:     at Object.mkdirsSync (/usr/local/lib/node_modules/gitbook-cli/node_modules/fs-extra/lib/mkdirs/mkdirs-sync.js:22:9)
    remote:     at Object.init (/usr/local/lib/node_modules/gitbook-cli/lib/config.js:21:8)
    remote:     at Object.<anonymous> (/usr/local/lib/node_modules/gitbook-cli/bin/gitbook.js:36:9)
    remote:     at Module._compile (module.js:410:26)
    remote:     at Object.Module._extensions..js (module.js:417:10)
    remote:     at Module.load (module.js:344:32)
    remote:     at Function.Module._load (module.js:301:12)
    remote:     at Function.Module.runMain (module.js:442:10)
    remote: expect: spawn id exp7 not open
    remote:     while executing
    remote: "expect eof"
    remote: www-data
    remote: web server pull at webserver at time: 2016 年 12 月 28 日 星期三 16:54:08 CST.
    remote: ================================================
    To http://git.domain.dev/java_guide.git
       b44864b..42e4c63  master -> master
    

    结果是pull没有成功,gitbook build也没有成功。

    再然后,我就到服务器上,运行了一下脚本,使用的是命令是:

    sudo -u www-data ./post-receive
    

    得到的结果如下

    www-data
    ===============================================
    deploying the test web
    www-data
    spawn git pull
    Username for 'http://git.domain.dev': username
    Password for 'http://[email protected]': 
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (3/3), done.
    remote: Total 3 (delta 1), reused 0 (delta 0)
    展开对象中: 100% (3/3), 完成.
    来自 http://git.domain.dev/java_guide
       b44864b..42e4c63  master     -> origin/master
    更新 b44864b..42e4c63
    warning: 不能访问 '/home/user/.config/git/attributes':权限不够
    warning: 不能访问 '/home/user/.config/git/ignore':权限不够
    warning: 不能访问 '/home/user/.config/git/attributes':权限不够
    Fast-forward
    warning: 不能访问 '/home/user/.config/git/attributes':权限不够
     .gitignore | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    spawn gitbook build
    info: 7 plugins are installed 
    info: 6 explicitly listed 
    info: loading plugin "highlight"... OK 
    info: loading plugin "search"... OK 
    info: loading plugin "lunr"... OK 
    info: loading plugin "sharing"... OK 
    info: loading plugin "fontsettings"... OK 
    info: loading plugin "theme-default"... OK 
    info: found 17 pages 
    info: found 1 asset files 
    info: >> generation finished with success in 2.1s ! 
    www-data
    web server pull at webserver at time: 2016 年 12 月 28 日 星期三 17:02:04 CST.
    ================================================
    

    虽然有几个权限 warning ,但是pullgitbook build都成功了。

    那么问题来了,我错在哪里了~~~

    PS :用的 bind9 做的域名解析,

    用的 nginx ,版本 1.10.0 ( Ubuntu ),确认了 nginx 的权限是www-data

    Git版本 2.7.4 ,

    GitBook 版本 3.2.2 ,

    nodejs 版本 4.2.6 ,

    npm 版本 3.5.2

    4 条回复    2016-12-29 17:45:50 +08:00
    SpicyCat
        1
    SpicyCat  
       2016-12-28 19:08:57 +08:00
    从 log 看,你手动执行脚本的时候, git pull 是成功了的。而你用 githook 执行 git pull ,虽然没有报错,但是没有任何输出,你先看看 git pull 有没有成功。

    另外,我觉得你这个任务用 githook 做并不合适, 用 CI 做比较合适。或者弄个 crontab 定时任务, 5 分钟刷一次,发现有推送就 build 。
    garsday
        2
    garsday  
    OP
       2016-12-28 19:20:30 +08:00
    补充一下,我觉得问题应该在,使用 sudo 调用 www-data 权限的时候,和 nginx 调用 www-data 权限,还是有区别的。

    @SpicyCat hook 去执行 git pull ,没有成功。在 hook 里面写脚本,没什么学习成本,而且,这个东西,更新的频率也不高,可能写好了,几个月更新一次,所以,就放在 push 的时候,进行部署,还有 api 文档什么的,基本都是更新频率不高的东西。
    SpicyCat
        3
    SpicyCat  
       2016-12-28 22:16:53 +08:00
    对 gitbook 不熟啊。那就从错误信息着手吧。 报错说 /var/www/.gitbook 已经存在,看看为啥会报这个错吧。
    garsday
        4
    garsday  
    OP
       2016-12-29 17:45:50 +08:00
    @SpicyCat www-data 用户,默认对应的文件夹是 /var/www ,可是这个文件夹的权限是 root:root ,哎,改了之后, gitbook 命令可以了,但是,使用 hook 运行 git pull , git fetch ,都返回 504 错误。。。

    有没有脚本玩的好的~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5512 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 06:48 · PVG 14:48 · LAX 23:48 · JFK 02:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.