linux - 如果我从本地机器推送更改,post-receive 钩子(Hook)无法执行某些命令

标签 linux git ssh githooks

我对 post-receive 有疑问钩。钩子(Hook)的任务是将刚刚收到的代码部署到 webserver 目录并仅将最新的提交消息发送到 PHP 脚本。
Hook 的代码(在服务器机器的裸仓库中):

#!/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"

本地网络中有两台机器:
  • 带有 Ubuntu 18.04 LTS 的“服务器”机器;
  • 本地机器 Windows 10,使用“git-bash”管理存储库

  • “服务器”机器(Ubuntu)
    /srv/git/repo.git 中配置了一个裸仓库目录和那个 repo 克隆到 /home/bob/projects/repo .群gitusers拥有 /srv/git目录并具有 rwx 权限。 bob也是 gitusers 的成员团体。

    当我从这台机器上的本地仓库推送更改时,没有问题。 Hook 正在执行,推送的代码被部署到 webserver,php 脚本被调用,最新的提交消息被添加到 DB。美好的。

    问题出在装有 Windows 的本地机器上。

    本地机器 (Windows)

    有一个repo通过 SSH 克隆。我 SSH 为 bob谁是 gitusers 的成员并拥有权限。 (事实上​​ bob 不是 gitusers 的成员是我之前的 SSH 连接问题)。远程也被添加为 ssh://bob@servermachineip/srv/git/repo.git .

    此时我收到以下错误(我已推送示例更改):
    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
    

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

    此外,hook 无法执行 mkdir、rm 或 move 等 linux 命令,但它执行 curl 命令并将最新的提交消息正确发送到 DB:O

    我该如何解决给定的问题?

    先感谢您

    编辑:

    /srv权限
    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存在但为空
    total 0
    

    /var/www/html/repo权限
    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
    

    最佳答案

    用户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然后递归地设置每个目录的写权限。

    关于linux - 如果我从本地机器推送更改,post-receive 钩子(Hook)无法执行某些命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59003027/

    相关文章:

    linux - 如何在 Linux 上编译 QScintilla 和 Eric6?

    linux - 如何将发布文件转换为 git repo

    混帐 : Push to different remote branch

    Git:将更改应用于两个分支

    linux - linux ubuntu 16 32bit 上,DrPaulcarter 汇编语言教程,undefined reference 错误

    linux - 需要在 linux 中使用一个衬里来触摸多个目录中的文件

    git - 关于 Karma 提交消息约定的问题

    linux - 通过 Putty 进行 SSH 会出现 "no such file or directory"

    Git 推送到谷歌云计算引擎

    windows - Git 在 Windows 上通过 SSH 推送将无法工作