forked from bebo/gst-wasapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.rb
executable file
·137 lines (115 loc) · 4.02 KB
/
deploy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env ruby
# vim: set sw=4 ts=4 et :
require 'optparse'
require 'open3'
require 'rbconfig'
include Open3
JENKINS_HOST = 'usw1-jenkins-002.blab.im'
JENKINS_PROJECT = 'gst-wasapi'
JENKINS_URL = "https://#{JENKINS_HOST}/job/#{JENKINS_PROJECT}/"
JENKINS_TOKEN = 'u0d6jw9xMC5cGrEpCrniKfa5u96SgIp9'
IS_WINDOWS = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
curl = IS_WINDOWS ? "./third_party/curl/bin/curl.exe -L --write-out %{http_code}" : "curl -s --write-out %{http_code}"
curl_test = IS_WINDOWS ? "./third_party/curl/bin/curl.exe -L --write-out %{http_code} -so nul" : "curl --write-out %{http_code} -so /dev/null"
def bump_version(t)
elems = t.split(".").map{|x| x.to_i}
elems[-1] += 1
elems.join(".")
end
def require_clean_work_tree
# do a git pull to make sure we are current
system("git pull") or raise "Something went wrong with git pull"
# update the index
system("git update-index -q --ignore-submodules --refresh") or raise "Something went wrong with git update-index"
#no unstaged files allowed
changed_files, _stderr_str, status = capture3('git status --short')
unless status.success?
puts "cannot check git status, exiting"
exit(1)
end
unless changed_files.empty?
puts "no unstaged files allowed. exiting"
puts changed_files
exit(1)
end
end
#ARGV << '-h' if ARGV.empty?
# env, tag, deploy
# get options
#
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: deploy.rb [options]"
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on("-a", "--allow-dirty", "allow dirty git repo") do |d|
options[:dirty] = d
end
opts.on("-n", "--[no-]dry-run", "Dry run") do |n|
options[:dryrun] = n
end
options[:environment] = "dev"
opts.on("-e", "--env ENV", "Environment to deploy to: dev, prod, ptr") do |e|
options[:environment] = e
end
opts.on("-t", "--tag TAG", "tag to deploy") do |t|
options[:tag] = t
end
options[:live] = false
opts.on("-l", "--live", "live or not") do |l|
options[:live] = l
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
# remove this when we have automatic cert signing
if options[:live] && options[:environment] == "prod"
puts "we don't have automatic certificate signing, so you'll need to make a non-live build and manually sign it then make the signed version live manually"
exit(1)
end
# make sure there are no uncommited changes before we tag
unless options[:dirty]
require_clean_work_tree
end
puts "#{curl_test} #{JENKINS_URL}"
test_response=%x(#{curl_test} #{JENKINS_URL})
# check for 200, if not, then exit
unless test_response == '200'
#if test_response != '200' || test_response != '201'
puts "cannot contact jenkins, did you forget to connect to the VPN? #{test_response}"
exit(1)
end
# generate tag
new_tag = ''
unless options[:tag]
time = Time.new
current_branch=%x(git rev-parse --abbrev-ref HEAD).chomp
new_tag = current_branch + "-" + time.strftime("%Y%m%d%H%M%S")
# if tag_result != '0' || tag_push_result != '0'
# puts "tagging failed, try again later: #{tag_result}"
# end
else
new_tag = options[:tag]
end
puts "current branch: #{current_branch}" if options[:verbose]
puts "new tag: #{new_tag}" if options[:verbose]
unless options[:dryrun]
system("git tag #{new_tag}") or raise "Cannot set tag: #{new_tag}"
system("git push --tags") or raise "Cannot push tags, something went wrong"
end
# trigger new build
jenkins_build_url="#{JENKINS_URL}buildWithParameters?token=#{JENKINS_TOKEN}&ENV=#{options[:environment]}&TAG=#{new_tag}&LIVE=#{options[:live]}"
puts "#{jenkins_build_url}"
puts "jenkins url: #{jenkins_build_url}" if options[:verbose]
unless options[:dryrun]
build_response=%x(#{curl} "#{jenkins_build_url}")
if build_response == '200' || build_response == '201'
puts "building #{new_tag}"
else
puts "contacting jenkins failed with status code #{build_response}"
exit(1)
end
end