php 通过exec 创建git分支失败

时间:2023-03-08 23:57:03
php 通过exec 创建git分支失败

今天给我们自己的发布系统增加一个新建分支的功能,操作比较简单,但是使用php执行shell命令的时候总是无法push分支到远程,但是登陆服务器执行却是可以的

新建分支命令如下

git fetch --all
git checkout -b pmt_20160624_v10.7.4 origin/master 
git push origin pmt_20160624_v10.7.4:pmt_20160624_v10.7.4

php大概代码如下,执行这个php文件是定时执行的

<?php

$cmd = [
    "cd /data/xxx",
    "git fetch --all",
    "git checkout -b pmt_20160624_v10.7.4 origin/master",
    "git push origin pmt_20160624_v10.7.4:pmt_20160624_v10.7.4"
];
exec( implode(" ; ",$cmd),$outputs );
var_dump( $outputs );

奇怪的是 分支创建成功了,但是push 去没有成功,打印出来的内容也没有提示什么错误,排除了半天没查到原因

开始我在$cmd中加了一个调试命令,ssh git@xxxxx,我是怀疑没有加入ssh key 导致的

$ ssh git@xxxxx
PTY allocation request failed on channel 0
Welcome to GitLab, vincent!
Connection to xxxxx closed.

但是通过php执行exec执行出来的命令提示是

Welcome to GitLab, robot!

发现 是不同的用户,一个是vincent,一个是robot,其实这个时候如果比较敏感的就知道什么问题了,后来我也是想到这点才解决的

后来我想办法将 push的命令提示打印出来了

git push origin pmt_20160624_v10.7.4:pmt_20160624_v10.7.4  --progress > /tmp/release.log 2>&1

提示错误信息如下

GitLab: You are not allowed to push code to this project.
fatal: Could not read from remote repository. Please make sure you have the correct access rights
and the repository exists.

我猛然想起来了,我是没有给robot账号 权限哇,我们git使用gitlab搭建的

php 通过exec 创建git分支失败

重点回顾

git把自己的输出 放到了  STDERR not STDOUT ,所以需要重定向下,才有了如下命令

git push origin pmt_20160624_v10.7.4:pmt_20160624_v10.7.4  --progress > /tmp/release.log 2>&1

原文地址:php 通过exec 创建git分支失败
标签:exec   git   php   push

智能推荐