GIT后收到多个分支部署

时间:2017-05-02 20:53:12

标签: ruby git

我在同一台服务器上有一个开发和生产文件夹,后面有一个repo,根据推送的分支推送到两个文件夹。我想将开发文件夹部署到当推送到master时按下repo和生产文件夹。我有一个编辑的ruby post-receive文件,我在另一个网站上找到但我不熟悉ruby,似乎无法弄清楚为什么它没有推送到任何一个文件夹。

#!/usr/bin/env ruby
# post-receive

from, to, branch = ARGF.read.split " "

if (branch =~ /^master/)

    puts "Received branch #{branch}, deploying to production."

    deploy_to_dir = File.expand_path('/var/www/html/production')
    `GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master`
    puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"

    exit
    ∂
elsif (branch =~ /^develop/)

    puts "Received branch #{branch}, deploying to development."

    deploy_to_dir = File.expand_path('/var/www/html/development')
    `GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f develop`
    puts "DEPLOY: develop(#{to}) copied to '#{deploy_to_dir}'"

    exit

end

对此次收到或替换的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

如果你不介意shell脚本而不是Ruby,那么这些人已经解决了同样的问题。

Git post-receive hook to checkout each branch to different folders?

答案 1 :(得分:0)

或许在这方面有点晚了,但是也许它会帮助那些试图在Ruby中找到解决方案的人。这是一个工作示例(从this source修改,让我排序):

#!/usr/bin/env ruby
# post-receive

# 1. Read STDIN (Format: "from_commit to_commit branch_name")
from, to, branch = ARGF.read.split " "

# 2. Only deploy if staging or master branch was pushed
if (branch =~ /staging$/) == nil && (branch =~ /master$/) == nil
    puts "Received branch #{branch}, not deploying."
    exit
end

# 3. Copy files to deploy directory(Path to deploy is relative to the git bare repo: e.g. website-root/repos)
if (branch =~ /staging$/)
    deploy_to_dir = File.expand_path('../path-to-staging-deploy.com')
    `GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f staging`
    puts "DEPLOY: staging(#{to}) copied to '#{deploy_to_dir}'"

elsif (branch =~ /master$/)
    deploy_to_dir = File.expand_path('../path-to-master-deploy.com')
        `GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master`
        puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"
else
    puts "Received branch #{branch}, not deploying."
    exit
end

我是Ruby的新手,所以可能有更好的方法来编写它,但它正如预期的那样工作 - 据我所见。