最近在使用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...

  1. <?php
  2. define('PRIVATE_KEY', 'XXXXXXXXXXXXXXXXxxx');
  3. if ($_SERVER['REQUEST_METHOD'] === 'POST'
  4. && $_REQUEST['thing'] === PRIVATE_KEY)
  5. {
  6. echo shell_exec("git pull");
  7. }

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.

  1. <?php
  2. ...
  3. echo shell_exec("/full/path/to/bin/git pull 2>&1");
  4. ...

Now it was reporting the next issue...

permissions

  1. error: cannot open .git/FETCH_HEAD: Permission denied

The apache user also needs read and write access to the entire repository.

  1. 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.

  1. 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

  1. 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)

  1. 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.

  1.  

使用PHP自动部署GIT代码的更多相关文章

  1. 利用WebHook实现PHP自动部署Git代码

    平时项目代码都托管在Coding,然后每次提交了代码之后都要SSH到服务器上去git pull一次,很是繁琐,在看了OverTrue的<使用PHP脚本远程部署git项目>后就尝试在自己服务 ...

  2. Jenkins之自动部署、代码安全扫描、自动化接口测试

    搭建Jenkins wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.reporpm --i ...

  3. 基于webhook方案的Git自动部署方案

    之前已经用Git实现了自己博客的提交自动部署,并自动提交到GitHub和coding以备不时之需.平时项目代码都托管在Coding或者GitHub上,也已经用上了coding提供的webhook功能, ...

  4. coding.net------WEBHOOK自动部署实战

    使用WebHook自动部署项目今天在laravist.com看到了这个 Webhook 自动部署Git项目 这个教学视频,以前自己也想做这样做一个利用Git WebHook的自动化部署,但总是不成功, ...

  5. Visual Studio Git代码管理环境部署

    Visual Studio 2010 部署Git代码管理环境. 第一:首先做Git的安装和环境部署 1.下载并安装Git软件,在windows环境下的Git叫做“msysGit”,官网地址为https ...

  6. Webhook 实践 —— 自动部署

    https://segmentfault.com/a/1190000007892407 安装nodejs 安装nodejs建议直接下载二进制包,把官网上的64位二进制版本下载地址复制下来,执行 wge ...

  7. 做了一个简易的git 代码自动部署脚本

    做了一个简易的git 代码自动部署脚本 http://my.oschina.net/caomenglong/blog/472665 发表于2个月前(2015-06-30 21:08)   阅读(200 ...

  8. git的安装使用和代码自动部署

    1.安装 http://www.cnblogs.com/sunada2005/archive/2013/06/06/3121098.html http://www.cnblogs.com/zhcncn ...

  9. centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署

    安装git服务器先安装依赖软件:yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gettext-devel open ...

随机推荐

  1. jQuery模板插件jsrender

    前几天学习jqm+phonegap,用到了一个jquery插件jsrender,(由于app不能用asp.net的服务端控件了,所以我也是醉了...),用于循环展示一下数据. 下面是我用到的几个简单用 ...

  2. python快速搭建WebServer

    #!/usr/bin/python import SimpleHTTPServer import SocketServer import os PORT = 7777 WEBDIR = "/ ...

  3. mysql导入.sql文件

    1. source /home/susie ...../**.sql 2. \. /home/susie/.../**.sql 批量导入.sql文件 首先新建一个main.sql,然后在main.sq ...

  4. Python变量和数据类型

    十六进制用0x前缀和0-9 a-f表示   字符串是以''或""括起来的任意文本   一个布尔值只有True和False两种值   布尔值可以用and or not运算   空值是 ...

  5. python中__init__.py文件的作用

    问题 在执行models.py时,报ImportError:No module named transwarp.db的错误,但明明transwarp下就有db.py文件,路径也没有错误.真是想不通.后 ...

  6. HDU 1180 诡异的楼梯(BFS)

    诡异的楼梯 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

  7. js智能提示代码

    <reference path = "../../../Scripts/jQuery-1.4.1.js"/>

  8. wordpress教程之get_option()

    get_option函数的作用 如果你想开发自己的一个wordpress插件,那么也许你有些数据希望保存,这些自定义数据一起都保存在 wordpress数据库的wp_options表,wp_optio ...

  9. Linux读取文件路径问题

    问题是这样的: 首先终端上有当前路径显示,我有个可执行程序代码是这样的: FILE fp  = fopen(filename, "rb"); if(fp == NULL)     ...

  10. HDU-1016-素数环

    /* 将1-n个数放在环中,保证相邻的两个数的和是素数 第一个数字永远是1 就这两个约束条件 第一个难点是计算素数: 参考文献: http://c.biancheng.net/cpp/html/254 ...