Skip to content

Instantly share code, notes, and snippets.

@gamecreature
Last active September 28, 2021 12:24
Show Gist options
  • Save gamecreature/f91adf16e7d5af43f03cf5bde9135e24 to your computer and use it in GitHub Desktop.
Save gamecreature/f91adf16e7d5af43f03cf5bde9135e24 to your computer and use it in GitHub Desktop.
A snippet for deploying to multiple servers at once
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# -8<-------8<-------8<-------8<-------8<-------8<------
# include from here, after the git plugin
stage = ARGV[0]
multi_deployment = ['production', 'staging'].include?(stage)
if multi_deployment
args = ARGV[1..-1]
puts "MULTI DEPLOYMENT: #{stage}"
puts "=" * 60
puts ""
files = Dir.glob("config/deploy/#{stage}*.rb").sort
files.each do |file|
name = file.sub('config/deploy/', '').sub('.rb', '')
next if name == stage
cmd = ['bundle', 'exec', 'cap', name] + args
puts cmd.join(' ')
puts "-" * 60
system(*cmd)
puts ""
end
exit
end
# ->8------->8------->8------->8------->8------->8------->8------
## rest of normal capile ...
@gamecreature
Copy link
Author

With the snippet above it is possible to have multiple production/staging servers...
And makes it easy to deploy to all of them or per-server.

For example

  • config/deploy/production1.rb
  • config/deploy/production2.rb
  • config/deploy/staging1.rb
  • config/deploy/staging2.rb

You can deploy ALL servers of a given environemtn
Or a specific server individually

# to deploy to all production servers:  (it wil invoke several separate deployment!!)
bundle exec cap production deploy   

# for deploying to the first production server:
bundle exec cap production1 deploy   

# or deploying to the second staging server
bundle exec cap staging2 deploy   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment