allow remotes other than "origin" in the git deploy method

This commit is contained in:
Tom Vaughan 2012-10-05 00:44:02 -03:00
parent 54ebdd153e
commit 01eb463045
2 changed files with 34 additions and 19 deletions

View file

@ -1,8 +1,12 @@
require "middleman-core/cli" require "middleman-core/cli"
require "middleman-deploy/extension" require "middleman-deploy/extension"
require "middleman-deploy/pkg-info"
require 'git' require "git"
PACKAGE = "#{Middleman::Deploy::PACKAGE}"
VERSION = "#{Middleman::Deploy::VERSION}"
module Middleman module Middleman
module Cli module Cli
@ -20,7 +24,7 @@ module Middleman
true true
end end
desc "deploy", "Copy build directory to a remote host" desc "deploy", "Deploy build directory to a remote host via rsync or git"
method_option "clean", method_option "clean",
:type => :boolean, :type => :boolean,
:aliases => "-c", :aliases => "-c",
@ -37,7 +41,7 @@ module Middleman
You should follow one of the two examples below to setup the deploy You should follow one of the two examples below to setup the deploy
extension in config.rb. extension in config.rb.
# To copy the build directory to a remote host: # To deploy the build directory to a remote host via rsync:
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.method = :rsync deploy.method = :rsync
# host, user, and path *must* be set # host, user, and path *must* be set
@ -48,11 +52,15 @@ activate :deploy do |deploy|
deploy.clean = true deploy.clean = true
end end
# To push to a remote gh-pages branch on GitHub: # To deploy to a remote branch via git (e.g. gh-pages on github):
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.method = :git deploy.method = :git
# git branch to push (default is gh-pages) # remote is optional (default is "origin")
deploy.branch = "master" # run `git remote -v` to see a list of possible remotes
deploy.remote = "some-other-remote-name"
# branch is optional (default is "gh-pages")
# run `git branch -a` to see a list of possible branches
deploy.branch = "some-other-branch-name"
end end
EOF EOF
end end
@ -85,6 +93,8 @@ EOF
user = self.deploy_options.user user = self.deploy_options.user
path = self.deploy_options.path path = self.deploy_options.path
puts "## Deploying via rsync to #{user}@#{host}:#{path} port=#{port}"
command = "rsync -avze '" + "ssh -p #{port}" + "' build/ #{user}@#{host}:#{path}" command = "rsync -avze '" + "ssh -p #{port}" + "' build/ #{user}@#{host}:#{path}"
if options.has_key? "clean" if options.has_key? "clean"
@ -101,29 +111,33 @@ EOF
end end
def deploy_git def deploy_git
remote = self.deploy_options.remote
branch = self.deploy_options.branch branch = self.deploy_options.branch
puts "## Deploying to GitHub Pages" puts "## Deploying via git to remote=\"#{remote}\" and branch=\"#{branch}\""
# ensure that the remote branch exists in ENV["MM_ROOT"]
orig = Git.open(ENV["MM_ROOT"])
# TODO: orig.branch(branch, "#{remote}/#{branch}")
Dir.mktmpdir do |tmp| Dir.mktmpdir do |tmp|
# clone ./ with branch gh-pages to tmp # clone ENV["MM_ROOT"] to tmp (ENV["MM_ROOT"] is now "origin")
repo = Git.clone(ENV['MM_ROOT'], tmp) repo = Git.clone(ENV["MM_ROOT"], tmp)
repo.checkout("origin/#{branch}", :new_branch => branch) repo.checkout("origin/#{branch}", :new_branch => branch)
# copy ./build/* to tmp # copy ./build/* to tmp
FileUtils.cp_r(Dir.glob(File.join(ENV['MM_ROOT'], 'build', '*')), tmp) FileUtils.cp_r(Dir.glob(File.join(ENV["MM_ROOT"], "build", "*")), tmp)
# git add and commit in tmp # git add and commit in tmp
repo.add repo.add
repo.commit("Automated commit at #{Time.now.utc}") repo.commit("Automated commit at #{Time.now.utc} by #{PACKAGE} #{VERSION}")
# push from tmp to self # push back into ENV["MM_ROOT"]
repo.push('origin', branch) repo.push("origin", branch)
# push to github
github_url = Git.open(ENV['MM_ROOT']).remote.url
repo.add_remote('github', github_url)
repo.push('github', branch)
end end
orig.push(remote, branch)
orig.remote(remote).fetch
end end
end end

View file

@ -5,7 +5,7 @@ require "middleman-core"
module Middleman module Middleman
module Deploy module Deploy
class Options < Struct.new(:whatisthis, :method, :host, :port, :user, :path, :clean, :branch); end class Options < Struct.new(:whatisthis, :method, :host, :port, :user, :path, :clean, :remote, :branch); end
class << self class << self
@ -19,6 +19,7 @@ module Middleman
options.port ||= 22 options.port ||= 22
options.clean ||= false options.clean ||= false
options.remote ||= "origin"
options.branch ||= "gh-pages" options.branch ||= "gh-pages"
@@options = options @@options = options