git - 在 post-receive Hook 内执行推送到 github

标签 git github ssh

所以我在私有(private)服务器的远程存储库中有一个接收后脚本。我想要的是每次远程 repo 收到推送时执行到 github 的推送。我这样做是为了它可以首先通过一些安全检查,比如确保它们不会更改 master 分支中的内容等。

这是脚本:

#!/bin/sh
git --work-tree=/var/www/html/beta --git-dir=/var/repo/beta.git checkout -f
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
git push git@github.com:kevuno/mycoolrepo.git $current_branch

我的coolrepo 是GitHub 内的一个私有(private)repo。

我可以在远程服务器上的 repo 中执行手动推送,因为我已经添加了 SSH key ,所以 ssh 身份验证工作正常。但是,如果我尝试从 post-receive 钩子(Hook)中推送,它突然就不起作用了。这是我得到的错误
remote: Host key verification failed.
remote: fatal: Could not read from remote repository.
remote: 
remote: Please make sure you have the correct access rights
remote: and the repository exists.

任何帮助,将不胜感激。

最佳答案

if I try to run the command as sudo it shows an error



这意味着 root 没有相同的 ~user/.ssh/id_rsa(.pub)允许用户通过适当的身份验证访问 GitHub 的私钥/公钥。

但是,如果该脚本打算以 root 身份运行,这意味着您可以以用户身份运行该特定命令
sudo -S -u otheruser ....

the OP comments :

However, I need to change sudoers file so I allow that user to perform sudo commands without typing their password.

关于git - 在 post-receive Hook 内执行推送到 github,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45174465/

相关文章:

git - 将 Master 上的所有新代码移动到 Git 中的新分支

java - 检查github上是否有游戏的新版本

ssh - SSH 协议(protocol) channel 数据窗口大小的目的是什么?

c# - 使用 SSH.NET SshClient.RunCommand 执行的命令 (.4gl) 失败,返回 "No such file or directory"

ios - 为两个不同分支上的版本创建 Xcode 项目的 zip 存档

git - 如何 merge GitHub 上的多个 fork 存储库?

学校的Github配置

git - 适用于 Node.js 的 `.gitignore`

Git (1.7.10) 每次都询问我的用户名和密码

git - 如何处理对 Git 的非标准颠覆导入