A Simple Script for Deploying Code With Github’s WebHooks
2016-05-06 11:01
525 查看
Learning source control with Git has completely changed how I build things. I was recently trying to hunt down an easy way to push up new code to my servers rather than sitting in an SSH session repeatedly running
Upload the script somewhere on your server where it’s accessible via http.
The apache user must own this script. On my Ubuntu server, this was a matter of running
If the repo already exists, the directory and all files in it need to be owned by the same Apache user on step 3. You can do that by running
Go into your Github Repo’s Settings > Service Hooks > WebHook URLs and add the public URL (i.e., http://example.com/webhook.php)
And voilà! Each time you make a commit to the branch you configured in your
源地址: http://behindcompanies.com/2014/01/a-simple-script-for-deploying-code-with-githubs-webhooks/
git pullto grab the latest files.Enter WebHooks. The idea is that when code gets committed to a repo, Github will send a POST request to a URL. In this (very simple) case, you can have it hit a script that can execute your
git pullcommand.I originally came across Jonathan Stark’s article, but it took some hacking to get everything working, so I thought I’d share how to get this working.The latest version of the PHP script will be maintained at this Gist.
<?php/** * This script is for easily deploying updates to Github repos to your local server. It will automatically git clone or * git pull in your repo directory every time an update is pushed to your $BRANCH (configured below). * * INSTRUCTIONS: * 1. Edit the variables below * 2. Upload this script to your server somewhere it can be publicly accessed * 3. Make sure the apache user owns this script (e.g., sudo chown www-data:www-data webhook.php) * 4. (optional) If the repo already exists on the server, make sure the same apache user from step 3 also owns that * directory (i.e., sudo chown -R www-data:www-data) * 5. Go into your Github Repo > Settings > Service Hooks > WebHook URLs and add the public URL * (e.g., http://example.com/webhook.php) * **/// Set Variables$LOCAL_ROOT = "/path/to/repo/parent/directory";$LOCAL_REPO_NAME = "REPO_NAME";$LOCAL_REPO = "{$LOCAL_ROOT}/{$LOCAL_REPO_NAME}";$REMOTE_REPO = "git@github.com:username/reponame.git";$BRANCH = "master";if ( $_POST['payload'] ) { // Only respond to POST requests from Github if( file_exists($LOCAL_REPO) ) { // If there is already a repo, just run a git pull to grab the latest changes shell_exec("cd {$LOCAL_REPO} && git pull"); die("done " . mktime()); } else { // If the repo does not exist, then clone it into the parent directory shell_exec("cd {$LOCAL_ROOT} && git clone {$REMOTE_REPO}"); die("done " . mktime()); } }?>How the script works is that if the Github hook hits the script, it’ll check if your repo exists. If it does, it’ll
cdinto the directory and run a
git pull. If the repo doesn’t exist, it’ll clone it.Here’s how to get things working:Download the script, and configure the variables at the beginning.
Upload the script somewhere on your server where it’s accessible via http.
The apache user must own this script. On my Ubuntu server, this was a matter of running
sudo chown www-data:www-data webhook.php, where www-data is your apache user and webhook.php is the path to your script (or just the filename if you are in the script’s directory).
If the repo already exists, the directory and all files in it need to be owned by the same Apache user on step 3. You can do that by running
sudo chown -R www-data:www-data /path/to/repo.
Go into your Github Repo’s Settings > Service Hooks > WebHook URLs and add the public URL (i.e., http://example.com/webhook.php)
And voilà! Each time you make a commit to the branch you configured in your
$BRANCHvariable, it should automatically update. To test that things are working, just push up a commit and see if your changed files are updated on the server.One quick point: changing directory ownership to the apache user means you won’t be able to FTP into your server using your personal account and make changes to your file. I set this up a long time ago, but I believe it would help to add your personal user account to the www-data group to allow you to be able to edit your files.If you’re using WordPress and a caching plugin, you will probably have to flush the cache to get changes to show up.If you’re having any issues (I did while I was testing this), you can run
tail -f /path/to/apache/error/log, and visit your WebHook URL setting page where you can test the hook. This error log will show if there are any problems such as syntax errors in your script.
源地址: http://behindcompanies.com/2014/01/a-simple-script-for-deploying-code-with-githubs-webhooks/
相关文章推荐
- RPC failed; result=22, HTTP code = 411
- git更新已經刪除的文件
- 提取Git每次提交后Commit的文件
- GIT迁移服务器
- 分布式版本管理git入门指南使用资料汇总及文章推荐
- git终极指南:在实际开发中的应用
- Git远程操作详解
- 25个 Git 进阶技巧(翻译)
- 详解版本控制利器Git,SVN的异同以及适用范围
- Ruby实现的删除已经合并的git分支脚本分享
- 在 Shell 提示符中显示 Git 分支名称的方法
- Git使用基础篇(一些常用命令和原理)
- git eclipse 插件的安装
- git fork同步是什么意思?
- Git使用小坑 Out of memory错误的解决方法
- Python的高级Git库 Gittle
- 使用GIT进行源码管理――GUI客户端小结
- 使用git代替FTP部署代码到服务器的例子
- linux系统安装git及git常用命令
- 分享下自己总结的Git常用命令