【问题标题】:Why can't I run this shell-script pushing to git from another shell script?为什么我不能运行这个从另一个 shell 脚本推送到 git 的 shell 脚本?
【发布时间】:2019-07-10 07:39:31
【问题描述】:

我在运行 shell 脚本时遇到问题,该脚本基本上将目录中的更改从另一个脚本中提交到远程 git 存储库。 “超级脚本”正在以超级用户权限运行,并且我在超级用户的主目录中的 ~/.ssh/ 处生成了所需的 ssh 密钥。此外,独立运行内部脚本可以正常运行并推送到远程存储库。

这是我在运行超级脚本时遇到的错误:

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 326 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To git@url.goes.here:groupspace/projectspace.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@url.goes.here:groupspace/projectspace.git'

我用这行代码调用内部脚本,保存其退出状态以供进一步处理

SUCCESS = $(bash /path/to/push-config.sh)

这是push-config.sh的内容

#!/bin/bash
# 
stamp=$(date +"%Y-%m-%d %T")

git add .
git commit -m "Config $stamp"

if [ $? -eq 0 ]
then
  git push -u origin master
  if [ $? -eq 0 ]
  then
    echo "Successfully pushed config to repo."
    exit 0
  else
    echo "Failure while pushing to repo."
    exit 1
  fi
else
  exit 1
fi

任何帮助表示赞赏,在此先感谢。

【问题讨论】:

  • running the inner script standalone works fine: 你是以超级用户/root 运行它吗?也许显示你的 push-config.sh 也可能有帮助
  • @ikkentim 是的,我以 root 身份运行它,一切正常。将添加 push-config.sh
  • 这是错误“remote: GitLab: You are not allowed to push code to protected branches on this project.”
  • @vorac 我知道这是错误,但我不知道为什么会得到它。我可以将代码推送到上述项目的受保护分支,我有必要的密钥,并且脚本本身运行良好。

标签: bash git shell centos7


【解决方案1】:

由于您被 git pre-receive 钩子拒绝,该钩子会验证您在上标或运行上标的环境中未正确设置的内容。

比较运行上标和内部脚本时创建的提交。确保它们完全相同。

接下来,确保两次推送的 git 环境相同。 我的意思是 git 用户变量应该相同,并且您当前的工作目录应该与存储库匹配。

如果您准确检查了 pre-receive 钩子验证的内容,您会更轻松地进行调试。

【讨论】:

  • 感谢@Niklas 的回复,这实际上可能指向正确的方向。由于错误消息表明问题可能是预接收钩子,我需要在远程存储库上找到并检查这个钩子,我只能通过我们的 git 网页访问它。您知道是否可以通过这种方式访问​​服务器端挂钩?
【解决方案2】:

root 用户有自己的主目录。我希望您在自己的主文件夹中有 ssh 密钥(~/.ssh,正如您所提到的)。要么将 ssh 密钥添加到 root 用户家中的 .ssh 文件夹中,要么查看 How to specify the private SSH-key to use when executing shell command on Git?

【讨论】:

  • 抱歉,我说得不够准确。 ssh 密钥位于根用户主目录的 .ssh 中。也编辑了我的 OP。
猜你喜欢
  • 2013-04-18
  • 2012-01-11
  • 1970-01-01
  • 2020-12-14
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
相关资源
最近更新 更多