首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果我从本地机器上推送更改,则Post-Receive钩子无法执行某些命令。

如果我从本地机器上推送更改,则Post-Receive钩子无法执行某些命令。
EN

Stack Overflow用户
提问于 2019-11-22 23:19:11
回答 1查看 46关注 0票数 1

我对post-receive钩子有个问题。钩子的任务是将刚刚接收到的代码部署到webserver目录,并只向PHP脚本发送最新的提交消息。钩子的代码(在服务器机器上使用裸回购):

代码语言:javascript
运行
复制
#!/bin/sh

# prod dir
TARGET="/var/www/html/repo"

# temp dir
TEMP="/srv/tmp/repo"

# git repo
REPO="/srv/git/repo.git"

mkdir -p $TEMP
git --work-tree=$TEMP --git-dir=$REPO checkout -f

cd $TEMP

cd /
rm -rf $TARGET
mv $TEMP $TARGET

cd $REPO
read oldrev newrev _branch
tail=$(git log -1 --pretty=format:'%s%b' $newrev)
curl -G 'http://example.com/phpscript.php' -d "msg=$tail"

本地网络中有两台机器:

带有Ubuntu18.04LTS的

  • "server“计算机;使用"git-bash”管理存储库

  • 本地机器Windows 10

“服务器”机器(Ubuntu)

/srv/git/repo.git目录中配置了一个repo,并将其克隆到/home/bob/projects/repo中。组gitusers拥有/srv/git目录并具有rwx权限。bob也是gitusers集团的成员。

虽然我推动改变从本地回购在这台机器,没有问题。钩子正在执行,推送代码被部署到php服务器上,调用php脚本,向DB添加最新的提交消息。很好。

问题在于Windows的本地机器。

本地机器(Windows)

有一个通过SSH克隆的repo。我以bob的身份注册,他是gitusers的成员,拥有权限。(事实上,bob不是gitusers的成员,这是我之前使用SSH连接时遇到的问题)。另外,remote被添加为ssh://bob@servermachineip/srv/git/repo.git

此时,我得到了一个下面的错误(我已经推送了示例更改):

代码语言:javascript
运行
复制
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 474 bytes | 474.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: mkdir: cannot create directory '/srv/tmp/repo': Permission denied
remote: fatal: this operation must be run in a work tree
remote: hooks/post-receive: 15: cd: can't cd to /srv/tmp/repo
remote: rm: cannot remove '/var/www/html/repo/index.html': Permission denied
remote: mv: cannot stat '/srv/tmp/repo': No such file or directory
remote:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
remote:                                  Dload  Upload   Total   Spent    Left  Speed
remote: 100    19    0    19    0     0    106      0 --:--:-- --:--:-- --:--:--   106
remote: Logged to remote db
To ssh://servermachineip/srv/git/repo.git
   24d56db..ab886dd  master -> master

所以看来钩子由于某种原因不能在通过ssh远程执行命令时执行命令?我不知道如何解决这个问题。搜索了两天的答案,尝试了设置权限,但我想我肯定错过了一些细节:/

此外,钩子不能执行像mkdir、rm或move这样的linux命令,但是它执行curl命令,并且最新的提交消息被正确地发送到DB :O。

我如何解决给定的问题?

提前谢谢你

编辑:

/srv 权限

代码语言:javascript
运行
复制
total 8
drwxr-xr-x 4 root users 4096 lis 21 23:33 git
drwxr-xr-x 2 root users 4096 lis 22 23:31 tmp

/srv/tmp 存在但为空

代码语言:javascript
运行
复制
total 0

/var/www/html/repo 权限

代码语言:javascript
运行
复制
total 1780
drwxr-xr-x 5 root root    4096 lis 22 23:31 assets
-rw-r--r-- 1 root root     702 lis 22 23:31 index.html
drwxr-xr-x 3 root root    4096 lis 22 23:31 js
EN

Stack Overflow用户

回答已采纳

发布于 2019-11-22 23:45:55

用户bob没有在/srv/tmp中创建目录的权限,也可能没有在/var/www/html/repo中删除文件的权限。

为了在目录中创建目录或删除文件,用户必须具有该目录的写权限。在这种情况下,只有root具有在/srv/tmp中创建目录的权限,因为只有root具有写入该目录的权限。假设bobusers组的一部分,您可以运行chmod g+w /srv/tmp来创建目录。或者,您可以为此目的使用/tmp,因为所有用户都有在那里编写的权限。

对于/var/www/html/repo,您可能需要执行一个chgrp -R users /var/www/html/repo,然后递归地设置每个目录的写权限。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59003027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档