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

2016-12-28 18:47:59 +08:00
 garsday

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://username@git.domain.dev': 
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://username@git.domain.dev': 
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://username@git.domain.dev': 
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

3356 次点击
所在节点    问与答
4 条回复
SpicyCat
2016-12-28 19:08:57 +08:00
从 log 看,你手动执行脚本的时候, git pull 是成功了的。而你用 githook 执行 git pull ,虽然没有报错,但是没有任何输出,你先看看 git pull 有没有成功。

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

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

有没有脚本玩的好的~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/330817

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX