听起来您想在Git工作流程的某个特定点执行任意功能。Git hooks是解决该问题的方法。
如果您查看任何Git仓库(位于.git
文件夹内),您将看到一个hooks
文件夹。其中有许多不同名称的示例钩子文件。根据您上面的说明,您想编辑post-receive
钩子文件,因为该文件将在远程仓库中更新新引用后立即调用(由本地推送导致)。有关更多信息,请阅读官方文档上的hooks或阅读这篇也许更易懂的解释。
您可以在钩子文件中放置任何您想要的shell命令。将文件名从post-receive.sample
更改为简单的post-receive
,添加您需要启动Rack,Mongrel,Apache等所需的命令,然后使用快速的chmod +x post-receive
使文件可执行即可。
它不基于任何提交或帖子钩子,但我相信如果你真的需要这样做,那么你一定能找到一些示例配方来完成类似的工作。Capistrano是一种实用程序和框架,可通过SSH在多台远程计算机上并行执行命令。它使用一个简单的特定领域语言(DSL),部分借鉴自rake工具。 Rake类似于C世界中的make,允许您定义任务,并将其应用于某些角色的机器。它还支持通过某些网关机器进行隧道连接,以使操作在VPN和防火墙后执行。
Capistrano最初旨在简化和自动化Web应用程序部署到分布式环境,并最初捆绑了一组旨在部署Rails应用程序的任务。自 Capistrano 2.0以来,部署任务现在是可选的,并要求客户端在其食谱中显式放置“load 'deploy'”。
一种在您的远程存储库上安装有用git钩子的工具,以在您的主机上启用基于push的、类似于Heroku的部署。
git push origin production
的操作(其中production
是您要自动部署的任何分支)。然后,它将在服务器端使用可爱的Git钩子自动执行部署操作(哪些操作由代码更改而确定,并在Github页面的“部署”部分中详细描述)。 - bhamby#!/bin/bash
while read oldrev newrev ref
do
if [ "$ref" = "refs/heads/master" ] ; then
echo "Master branch pushed, deploying to staging"
# seams to be set to "." for hooks, unset to make things more normal
unset GIT_DIR
# deploy path, where "current", "releases", "shared" etc are
DEPLOYDIR="/home/user/deploy/staging"
# default path for :deploy_via :remote_cache is shared/cached-copy
cd "$DEPLOYDIR/shared/cached-copy"
# update cache to pushed revision, will be done by capistrano too
git fetch origin && git fetch --tags origin && git reset --hard "$newrev"
# load rvm
source ~/.rvm/scripts/rvm
rvm use 1.9.2
# make sure correct gems are installed
# this will also create a .bundle directory
bundle install --gemfile Gemfile --path "$DEPLOYDIR/shared/bundle" --deployment --without development test
# run capistrano
# if you use deploy:migrations instead of deploy you should probably add
# after "deploy:migrations", "deploy:cleanup"
# to your deploy.rb
bundle exec cap staging deploy:migrations
fi
done
如果不使用:remote_cache
,也可以采用更简单的设置,但这将使用先前(当前已部署)的配置运行capistrano,并且可能会更加脆弱。
post-receive
钩子脚本:
#!/bin/bash
while read oldrev newrev ref
do
if [ "$ref" = "refs/heads/master" ] ; then
echo "Master branch pushed, deploying to staging"
# seams to be set to "." for hooks, unset to make things more normal
unset GIT_DIR
source ~/.rvm/scripts/rvm
rvm use 1.9.2
cd /home/user/deploy/staging/current && bundle exec cap staging deploy:migrations
fi
done
git remote add myheroku dokku@DOMAIN.com:your-app
git push myheroku master
http://your-app.DOMAIN.com
dokku domains:set your-app NEWDOMAIN.com
值得一提的是,还有一个名为Flynn的新项目GitHub:flynn / flynn,它比Dokku拥有更多的功能。在此处阅读关于Flynn和Dokku的历史...