Skip to content

Instantly share code, notes, and snippets.

@desmondmonster desmondmonster/deploy.rb
Last active Oct 20, 2017

Embed
What would you like to do?
Elixir Deploy Script with Versioning Niceties
#!/usr/bin/ruby
require 'optparse'
FILE_NAME = 'VERSION.txt'
options = {}
def red(str); "\e[31m#{str}\e[0m"; end
def green(str); "\e[32m#{str}\e[0m"; end
def parse_version(version)
version.split(".").map(&:to_i)
end
def combine_version(major, minor, patch)
[major, minor, patch].map(&:to_s).join(".")
end
def increment_major(version)
major, minor, patch = parse_version(version)
combine_version(major + 1, 0, 0)
end
def increment_minor(version)
major, minor, patch = parse_version(version)
combine_version(major, minor + 1, 0)
end
def increment_patch(version)
major, minor, patch = parse_version(version)
combine_version(major, minor, patch + 1)
end
OptionParser.new do |opts|
opts.banner = "Usage: deploy.rb [options]"
opts.on("-s", "--set [VERSION]", "Manually set version") do |v|
options[:version] = v
end
opts.on("-M", "--major", "Increment major version") do |o|
options[:major] = o
end
opts.on("-m", "--minor", "Increment minor version") do |o|
options[:minor] = o
end
opts.on("-p", "--patch", "Increment patch version (default)") do |o|
options[:patch] = o
end
opts.on("-d", "--dry-run", "Dry run (do not bump version, commit changes, or build release") do |o|
options[:dry_run] = o
end
end.parse!
# read version
old_version = File.read(FILE_NAME).strip
new_version =
if options[:version]
puts "#{red("manually")} setting version"
options[:version]
elsif options[:major]
puts "incrementing #{red("MAJOR")} version"
increment_major(old_version)
elsif options[:minor]
puts "incrementing #{red("MINOR")} version"
increment_minor(old_version)
else
puts "incrementing #{red("PATCH")} version"
increment_patch(old_version)
end
puts "#{green("#{old_version} -> #{new_version}")}\n\n"
exit if options[:dry_run]
# write new version
open(FILE_NAME, 'w') { |f| f.puts new_version }
# commit, add tag
system %(git add #{FILE_NAME})
system %(git commit -m "bump version")
system %(git tag -a v#{new_version} -m "release version #{new_version}")
system %(git push --tags)
# build release
exec "mix edeliver build release && mix edeliver deploy release to production --start-deploy --version=#{new_version}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.