PHP通过exec执行git pull

目标

项目没有使用Jenkins部署,使用的是Gitlab+ECS,要实现代码的自动部署

想法

使用Gitlab的钩子,当某个分支的代码提交之后,访问一个URL,实现代码的自动部署。这里使用PHP的exec方法,执行“git pull”操作

Tips:这种方法不是百分百靠谱,万一代码冲突就......

实现

目的:访问 http://xxx.com/index.php?project=hello,程序会自动在hello这个文件下,运行“git pull”命令

1、前提条件:linux上安装好git、php、nginx(或apache)

1.1、git安装并配置

参考:https://git-scm.com/book/zh/v1/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git

1.2、php、nginx安装

参考:https://help.aliyun.com/document_detail/97251.html?spm=a2c4g.11186623.6.1107.70581014ZKSnS8

1.3、php、apache安装

参考:https://help.aliyun.com/document_detail/50774.html?spm=a2c4g.11186623.6.1123.816d14f8IRTWh0

2、启动安装好的php跟nginx(或apache)后

3、如果有改过nginx的端口的话,需要修改下iptables,编辑/etc/sysconfig/iptables,新增一个端口即可,修改后运行 service iptables restart(记得阿里云入网规则也要添加相应的端口)

4、相应的php代码如下:

<?php
#部署代码用!
$project = $_GET[‘project‘];
if (!empty($project)) {
    $shell = "cd /data0/www/{$project}/ && pwd && sudo git pull 2>&1";
    exec($shell,$out);
    echo $project."<br/>";
    print_r($out);
}else{
        echo "Well done.";
}
?>

说明:

(1)sudo,如果没有加这个的话,大概率会出现"permisson deny"

(2)2>&1,这个主要是为了查看运行结果

(3)pwd,查看目录

5、如果上面的程序无法跑起来,提示“sudo: no tty present and no askpass program specified”这类错误,那么基本上就是访问权限的问题,按照下面提示修改

6、运行“ps -ef | grep php”查看php进程的用户名,如果是nobody,你需要修改下php的配置文件

ps -ef | grep php

7、修改php配置文件

7.1、添加用户组,/usr/sbin/groupadd 用户组名

/usr/sbin/groupadd 用户组名

7.2、新增用户,/usr/sbin/useradd -g 用户组名 -s /bin/bash 用户名

/usr/sbin/useradd -g 用户组名 -s /bin/bash 用户名

7.3、修改php配置,我的是在/etc/php-fpm.d/www.conf,修改最上面的user跟group

vim /etc/php-fpm.d/www.conf

7.4、重启php,service php-fpm restart

service php-fpm restart

7.5、运行“ps -ef | grep php”查看php进程的用户名

ps -ef | grep php

8、修改sudoers

8.1、运行chmod u+w /etc/sudoers

chmod u+w /etc/sudoers

8.2、vim /etc/sudoers

vim /etc/sudoers

8.3、在"root    ALL=(ALL)       ALL"下面加上"用户组 ALL=(ALL)  NOPASSWD: ALL"

root    ALL=(ALL)       ALL用户组 ALL=(ALL)  NOPASSWD: ALL

9、继续执行上面的php文件,没提示错误基本上可以了,可能出现sudo: unable to initialize policy plugin这类错误,按照提示改即可

10、pkexec visudo可查看具体的错误信息

目的:访问 http://xxx.com/index.php?project=hello,程序会自动在hello这个文件下,运行“git pull”命令由此配合gitlab钩子实现代码的自动部署

原文地址:https://www.cnblogs.com/lyc94620/p/11825309.html

时间: 2024-08-30 12:28:42

PHP通过exec执行git pull的相关文章

执行git pull命令报告&quot;fatal: early EOF fatal: index-pack failed error: Could not fetch origin fatal: read error: Connection timed out&quot;如何处理?

答: 改变仓库的地址(<source code dir>/.git/config中的[remote "origin"]),如: .git/config中的[remote "origin"]如下: [remote "origin"] url= git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git fetch = +refs/heads/*:refs/remote

linux服务器git pull/push时避免频繁输入账号密码

1.先cd到根目录,执行git config --global credential.helper store命令 [[email protected] ~]# git config --global credential.helper store 2.执行之后会在.gitconfig文件中多加红色字体项 [user] name = wang email = [email protected] [credential] helper = store 3.之后cd到项目目录,执行git pull命

使用git pull时,项目没有更新?

进入项目目录后,执行 git pull 命令,没有将项目更新,并提示下图: 提示:there is no tracking information for the current branch. 意思是说本地分支master和远程分支master,并未关联.之前文章中曾经说过,在本地仓库和远程仓库连接后,执行一个: git push -u origin master 1 git push -u origin master 把本地库的内容推送到远程,用git push命令,实际上是把当前分支mas

解决因为本地代码和远程代码冲突,导致git pull无法拉取远程代码的问题

一.问题 当本地代码和远程代码有冲突的时候,执行git pull操作的时候,会提示有冲突,然后直接终止本次pull,查了些资料没有找到强制pull的方式,但是可以使用如下方式解决. 二.解决思路 可以先将本地内容stash到仓库中,执行stash操作后,本地代码将返回到修改前的内容.这时,就可以正常将远程代码下载到本地了.然后再通过stash操作将仓库中的内容合到本地,如果有冲突就可以进行解决了. 三.操作命令 1.git stash 将本地代码stash到仓库中. 可以使用git stash

linux服务器git pull/push时提示输入账号密码之免除设置

1.先cd到根目录,执行git config --global credential.helper store命令 [[email protected] ~]# git config --global credential.helper store 2.执行之后会在.gitconfig文件中多加红色字体项 [user] name = 天明 email = [email protected] [credential] helper = store 3.之后cd到项目目录,执行git pull命令,

git pull --rebase 和 git pull 的区别

转载:http://hungyuhei.github.io/2012/08/07/better-git-commit-graph-using-pull---rebase-and-merge---no-ff.html pull --rebase 首先是吐嘈 如果你正在 code review,看到上图(下文将称之为:提交线图)之后,特别是像我这样有某种洁癖的人,是否感觉特别难受?如果是的话,请看下文吧 :) 为什么 Git 作为分布式版本控制系统,所有修改操作都是基于本地的,在团队协作过程中,假设

git pull 部署问题一揽子问题解决

之前遇到问题 在服务器拉取一直不成功, php 的shell函数 调用 git pull 一直不成功 ,但是单独 用root 权限 在机器上面 执行 git pull 是可以的 说明语法没问题. 而 php 调用的shell的权限 其实是用户 www 说明 www的权限不成功表示用户www 没权限 所以需要对www配置权限 1首先 在 /etc/passwd 中给 www 设置主页目录  /home/uwww 2 让其荣有登录权限可执行shell,,  home/uwww /bin/bash 3

执行git命令时出现fatal: &#39;origin&#39; does not appear to be a git repository错误

在执行git pull origin master时出现: fatal: 'origin' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists 解决方案: git remote add origin [email protec

解决git pull报错的方法

第1个问题: 解决GIT代码仓库不同步 今天在执行git pull时出现: [[email protected] /data/work/www/rest/lib/Business/Inventory]# git pull Enter passphrase for key '/root/.ssh/id_rsa': Updating 70e8b93..a0f1a6c error: Your local changes to the following files would be overwritt