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钩子实现代码的自动部署
PHP通过exec执行git pull的更多相关文章
- VSTS 执行git pull报错问题修复
VSTS中进行双向同步配置的git pull指令如下: 运行时报错,Log如下图所示: 原因说的很清楚了,需要提前执行以下两条git config指令: git config --global use ...
- git 系统中 post-receive 钩子不能正常执行 git pull 解决方法
有一个需求是本地git在push到远程 git repo 之后,在远程服务器上自动在/dir/foo下执行 git pull 的操作.想来是一个很简单的需求,不就是在远程的 foo.git 仓库中的 ...
- 批处理遍历文件夹执行git pull
echo off & color 0A for /d %%f in (D:\www\*) do ( D: cd %%f chdir git pull ) pause 遍历D:\www\这个文件 ...
- 执行git pull时提示Connection reset by 13.229.188.59 port 22
问题如下图: 解决办法: 1. 2. 3. 4. 5. 6.
- linux服务器git pull/push时提示输入账号密码之免除设置
1.先cd到根目录,执行git config --global credential.helper store命令 [root@iZ25mi9h7ayZ ~]# git config --global ...
- git pull 部署问题一揽子问题解决
之前遇到问题 在服务器拉取一直不成功, php 的shell函数 调用 git pull 一直不成功 ,但是单独 用root 权限 在机器上面 执行 git pull 是可以的 说明语法没问题. 而 ...
- 使用git pull时,项目没有更新?
进入项目目录后,执行 git pull 命令,没有将项目更新,并提示下图: 提示:there is no tracking information for the current branch. 意思 ...
- 执行git命令时出现fatal: 'origin' does not appear to be a git repository错误
在执行git pull origin master时出现: fatal: 'origin' does not appear to be a git repository fatal: Could no ...
- linux git pull/push时提示输入账号密码之免除设置
1.先cd到根目录,执行git config --global credential.helper store命令 [root@iZ25mi9h7ayZ ~]# git config --global ...
随机推荐
- VSCode编辑器用户设置
{"gitlens.advanced.messages": {"suppressCommitHasNoPreviousCommitWarning": false ...
- BZOJ 1901 洛谷 P2617 ZOJ 2112 Dynamic Rankings
以下时空限制来自zoj Time limit 10000 ms Memory limit 32768 kB OS Linux Source Online Contest of Christopher' ...
- beforeRouterEnter与replace的使用
这次使用beforeRouterEnter来判断是一定条件下才执行相应的页面跳转. beforeRouterEnter:组件内路由,跟data,methods同级 beforeRouteEnter ( ...
- 2018-2019-2 20165235《网络对抗技术》Exp7 网络欺诈防范
2018-2019-2 20165235<网络对抗技术>Exp7 网络欺诈防范 实验目的 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法 实验内容 (1)简单应 ...
- centos64位编译32位程序
test.c #include <stdio.h> int main() { printf("sizeof long is %d\n",sizeof(long)); ; ...
- leetcode-easy-array-189 Rotate Array
mycode 75.59% class Solution(object): def rotate(self, nums, k): """ :type nums: Lis ...
- c/c++二级指针动态开辟内存
c版: #include <stdio.h> #include <stdlib.h> #define row 4 #define col 8 int main() { int ...
- 如何使用Jetbrains Clion 在一个工程里 编译单个C++源文件 (实现一键编译且运行)
这篇文章主要在下面这篇文章的基础上,先是实现了一键编译和一键运行两个单个功能,最后又进一步使用Clion自带的Custom Build Application实现编译且运行一键实现. https:// ...
- QT Desinger设计窗体应用程序框架
目录 目录 前言 系统软件 QT Designer Using QT Designer Open QTDesigner Tool Widget Box QT Designer的布局 属性栏 示例 i ...
- 测开之路一百二十八:flask之重定向和404
a.b两个视图,分别返回a的页面和b的页面 重定向:redirect 重定向到路由:请求/a/时,重定向到/b/ 重定向到视图函数:url_for(“函数名“),访问/a/时,重定向到函数b() 主动 ...