使用PHP自动部署GIT代码
最近在使用Coding的代码托管,顺便设置了WebHook自动部署,过程还是挺艰辛的,主要还是没搞懂Linux的权限控制,不过好在弄好了,分享一下获益最深的一篇文章,供大家参考,原文是英文版的,我的英语也不行,勉强能看懂,大家凑合着看吧
原文链接:http://jondavidjohn.com/git-pull-from-a-php-script-not-so-simple/
I intended to set up a repository (hosted on BitBucket) to initiate a pull on a dev server when new commits are pushed up.
It seemed like a simple enough process. BitBucket has a service that will fire off a POST request as a post-receive hook. So I set up a receiving php script to check a randomized token and then initiate the git pull
. Looking something like this...
<?php
define('PRIVATE_KEY', 'XXXXXXXXXXXXXXXXxxx');
if ($_SERVER['REQUEST_METHOD'] === 'POST'
&& $_REQUEST['thing'] === PRIVATE_KEY)
{
echo shell_exec("git pull");
}
Didn't end up being as simple as I had anticipated...
There were a few considerations that I did not take into account. Documenting them here will hopefully help you avoid some obstacles in trying to get something like this set up.
(Missed) Considerations
the binary (git
in this case)
The user that is attempting to execute git pull
is the apache user (www
in our case). This user did not happen to have git
in their path.
This took a while to track down because the exec()
family of functions simply fail silently because they only report STDOUT and not STDERR. To get the function to report STDERR you can route it into STDOUT by adding 2->&1
at the end of your command.
After I realized this I logged in and found the full path of the git binary with which git
, which is /full/path/to/bin/git
.
<?php
...
echo shell_exec("/full/path/to/bin/git pull 2>&1");
...
Now it was reporting the next issue...
permissions
error: cannot open .git/FETCH_HEAD: Permission denied
The apache user also needs read and write access to the entire repository.
chown -R ssh_user:www repository/
It's also a good idea to make sure any files/directories inherit this ownership if being created by others by setting the group sticky bit.
chmod -R g+s repository/
"Host key verification failed"
Next, you need to do an intial git pull with the apache user to make sure the remote is added to the apache user's known_hosts
file
sudo -u www git pull
ssh key
Another consideration created by this command being run by the apache user is the ssh key it uses to communicate with the remote repository.
First, I went down the path of attempting to use the GIT_SSH
environment variable to set the ssh -i
option to tell it to use a specific ssh key I had generated with the ssh user. I never got this to work, most likely because there are a lot of rules ssh uses to determine the safety of a given key. It requires some specific permissions regarding the user that is attempting to use the key.
An easier way I discovered was to give the apache user a home directory (via /etc/passwd
) and a .ssh
directory and then run the ssh-keygen
command as the apache user (www
)
sudo -u www ssh-keygen -t rsa
This creates the keys and puts them in their expected location with the proper permissions applied.
Then I added the key as a read-only key for the BitBucket repository and everything worked as expected.
使用PHP自动部署GIT代码的更多相关文章
- 利用WebHook实现PHP自动部署Git代码
平时项目代码都托管在Coding,然后每次提交了代码之后都要SSH到服务器上去git pull一次,很是繁琐,在看了OverTrue的<使用PHP脚本远程部署git项目>后就尝试在自己服务 ...
- Jenkins之自动部署、代码安全扫描、自动化接口测试
搭建Jenkins wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.reporpm --i ...
- 基于webhook方案的Git自动部署方案
之前已经用Git实现了自己博客的提交自动部署,并自动提交到GitHub和coding以备不时之需.平时项目代码都托管在Coding或者GitHub上,也已经用上了coding提供的webhook功能, ...
- coding.net------WEBHOOK自动部署实战
使用WebHook自动部署项目今天在laravist.com看到了这个 Webhook 自动部署Git项目 这个教学视频,以前自己也想做这样做一个利用Git WebHook的自动化部署,但总是不成功, ...
- Visual Studio Git代码管理环境部署
Visual Studio 2010 部署Git代码管理环境. 第一:首先做Git的安装和环境部署 1.下载并安装Git软件,在windows环境下的Git叫做“msysGit”,官网地址为https ...
- Webhook 实践 —— 自动部署
https://segmentfault.com/a/1190000007892407 安装nodejs 安装nodejs建议直接下载二进制包,把官网上的64位二进制版本下载地址复制下来,执行 wge ...
- 做了一个简易的git 代码自动部署脚本
做了一个简易的git 代码自动部署脚本 http://my.oschina.net/caomenglong/blog/472665 发表于2个月前(2015-06-30 21:08) 阅读(200 ...
- git的安装使用和代码自动部署
1.安装 http://www.cnblogs.com/sunada2005/archive/2013/06/06/3121098.html http://www.cnblogs.com/zhcncn ...
- centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署
安装git服务器先安装依赖软件:yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gettext-devel open ...
随机推荐
- POJ 1190 生日蛋糕(DFS)
生日蛋糕 Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit Status Descrip ...
- nginx中配置跨域支持功能
vi /etc/nginx/nginx.conf 加入如下代码 http { ###start#### add_header Access-Control-Allow-Origin *; add ...
- apache主目录,配置文件目录结构说明
apache服务安装成功后,主要的目录结构如下: |-- bin 程序命令目录[apache执行文件的目录如apachectl,htpassed] |-- build |-- cgi-bin 预设给一 ...
- BufferedStream类 - 缓冲流
BufferedStream常用于对其他流的一个封装,它必须和其他流结合一起使用.MemoryStream将所有的内容都放入内存中,而BufferedStream不是.BufferedStream在基 ...
- Axure快捷键大全
基本快捷键: 打开:Ctrl + O 新建:Ctrl + N 保存:Ctrl + S 退出:Alt + F4 打印:Ctrl + P 查找:Ctrl + F 替换:Ctrl + H 复制:Ctrl + ...
- 【转】内核编译时, 到底用make clean, make mrproper还是make distclean(转载)
原文网址:http://dongyulong.blog.51cto.com/1451604/449470 内核编译时, 到底用make clean, make mrproper还是make distc ...
- spring-boot+nginx+tomcat+ssl配置笔记
如果你的tomcat应用需要采用ssl来加强安全性,一种做法是把tomcat配置为支持ssl,另一种做法是用nginx反向代理tomcat,然后把nginx配置为https访问,并且nginx与tom ...
- Safari HTML5 Canvas Guide: Creating Charts and Graphs
Safari HTML5 Canvas Guide: Creating Charts and Graphs Bar graphs are similar to data plots, but each ...
- 对中级Linux 用户非常有用的20 个命令
也许你已经发现第一篇文章非常的有用,这篇文章是继对初级Linux用户非常有用的20个命令的一个延伸. 第一篇文章的目的是为新手准备的而这篇文章则是为了Linux的中高级用户.在这里你将学会如何进行自定 ...
- android-读取Assets图片资源保存到SD - 随心
public class ReadBitmap { public void readByte(Context c, String name, int indexInt) { byte[] b = nu ...