Ruby on rails Git和Rails的自动部署,它是如何工作的?

Ruby on rails Git和Rails的自动部署,它是如何工作的?,ruby-on-rails,git,bitbucket,Ruby On Rails,Git,Bitbucket,我在bitbucket中有一个Git存储库,在我的live rails服务器上有另一个,我如何使它在我推到bitbucket时,live服务器也从bitbucket中拉出来 我在网上找到了这个片段,并将其作为post receive放在我的live server的git挂钩上,但我不知道下一步该怎么做: #!/bin/sh name=$1 if [ -z "$name" ] ; then echo "need to give name of checkout dir on com

我在bitbucket中有一个Git存储库,在我的live rails服务器上有另一个,我如何使它在我推到bitbucket时,live服务器也从bitbucket中拉出来

我在网上找到了这个片段,并将其作为
post receive
放在我的live server的git挂钩上,但我不知道下一步该怎么做:

#!/bin/sh
name=$1
if [ -z "$name" ] ; then
        echo "need to give name of checkout dir on command line"
        exit 1
fi

dir=/srv/web/$name
if [ ! -d $dir ] ; then
        echo "the directory $dir does not exist"
        exit 1
fi

cd $dir
env -i git pull
rake db:migrate
touch $dir/tmp/restart.txt

有人能指出我应该怎么做才能做到这一点吗?我见过bitbucket上的post钩子(类似于github),但我不知道该怎么办。

如果你想在推到bitbucket时发生事情,我想你需要以某种方式使用它们。可能会将更新后的引用发布到服务器上的某个服务上,该服务会进行拉取、重置等操作

但是如果您直接推送到自己的服务器,您可以使用标准的git post接收挂钩。

Analysis BitBucket有很多,但我没有 显式用于触发部署脚本。你可以扮演一个角色 您自己使用的,但这通常不是 正确的做法

公认的智慧是使用来自continuous的部署脚本 集成,以便您只部署成功的构建。注意 我说的是“部署”,而不是“推动”,因为你不能推动一个 具有工作树的存储库

然而,在没有任何限制的情况下触发Rails更新当然是可能的 持续集成、接收后挂钩或部署工具,如 . 投票是一种选择

解决方案 而post-receive或service钩子可以在 提交时,最简单的方法就是从web上轮询Git 服务器。例如,您可以每分钟运行一个cron作业来拉动 当前主分支进入web根目录下的工作树

首先,安装并测试轮询脚本。我通常使用变体 其中:

因为我们在脚本中使用独占锁定,所以每分钟都进行轮询 应该没问题。如果您不使用锁定,或者希望减少服务器上的负载 然后在crontab中设置更长的轮询间隔

就这样!现在,当您从开发或QA转向origin时,您的 Rails服务器应该在大约一分钟内自我更新。通常是这样
对于大多数目的来说,这已经足够了,希望你的也足够了。

谢谢!这正是我要找的。顺便说一句,如果发生新的更改,此脚本是否会重新启动服务器?它应该与Phusion Passenger一起启动。其他设置可能需要显式重新启动。您的链接已断开,先生。
#!/bin/bash
# Script:
#     git_poll.sh <appname>
# Purpose:
#     Poll your Rails repository for changes.

set -e

WEBROOT='/var/www'
MY_RAILS_APPNAME="$1"
shift

# Use the flock(1) utility to guard against long-running fetch or merge
# operations using the flock(2) system call. On Debian-based systems,
# this tool is found in the util-linux package.
(
    flock -n 9 

    cd "$WEBROOT/$MY_RAILS_APPNAME"
    git fetch origin master

    # Check if heads point to the same commit.
    if ! cmp --quiet <(git rev-parse master) <(git rev-parse origin/master)
    then
        git pull --force origin master
        touch "$WEBROOT/$MY_RAILS_APPNAME/tmp/restart.txt"
    fi
) 9> "/var/lock/polling4${MY_RAILS_APPNAME}"
* * * * * /usr/local/bin/git_poll.sh example_app