forked from travis-ci/dpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger
executable file
·102 lines (87 loc) · 2.64 KB
/
trigger
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
#!/usr/bin/env ruby
# Triggers a build on travis-ci.org/travis-ci/dpl.
#
# Requires either ENV['TRAVIS_API_TOKEN'] to be set, or -t/--token to
# be given.
#
# If no arguments are given then jobs for all providers are included,
# otherwise arguments are interpreted as provider names and matched
# against YAML config files in .travis/providers.
require 'json'
require 'net/http'
require 'securerandom'
require 'optparse'
require 'yaml'
def parse_opts(opts)
ARGV.options do |o|
o.on('-u', '--url URL') { |obj| opts[:url] = obj }
o.on('-t', '--token TOKEN') { |obj| opts[:token] = obj }
o.on('-b', '--branch BRANCH') { |obj| opts[:branch] = obj }
o.on('--pretend') { opts[:pretend] = true }
o.on('--error') { opts[:error] = true }
o.parse!
end
opts
end
def post(uri, body, opts)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri.path)
req['Authorization'] = "token #{opts[:token]}"
req['Content-Type'] = 'application/json'
req['Travis-API-Version'] = '3'
req.body = JSON.dump(body)
http.request(req)
end
SKIP = /(azure_web_apps|cloud66|datica|npm-github|snap)/
def paths
glob = ARGV.any? ? ARGV.join(',') : '*'
paths = Dir[".travis/providers/{#{glob}}/*.yml"].sort
paths = paths.reject { |path| path =~ SKIP }
abort "No configs found for #{glob}" if paths.empty?
paths
end
def error(config)
keys = config.keys - %w(provider cleanup keep_history)
keys = keys.select { |key| config[key].is_a?(String) || config[key].is_a?(Hash) && config[key]['secure'] }
keys.each { |key| config[key] = 'kaputt' }
config
end
def jobs(opts)
paths.map do |path|
job = YAML.load(File.read(path))
job['name'] = path =~ %r(providers/(.+)/(.+)\.yml) && [$1, $2 == 'travis' ? nil : $2].compact.join(':')
job['deploy'][0] = error(job['deploy'][0]) if opts[:error]
job['deploy'][0].update(edge: only(opts, :branch), on: only(opts, :branch))
job
end
end
def only(hash, *keys)
hash.select { |key, _| keys.include?(key) }.to_h
end
opts = parse_opts(token: ENV['TRAVIS_API_TOKEN'], branch: 'master')
uri = URI.parse(opts[:url] || 'https://api.travis-ci.com/repo/travis-ci%2Fdpl/requests')
body = {
request: {
branch: opts[:branch],
message: "test dpl (#{opts[:branch]}) #{ARGV.any? ? ARGV.join(', ') : '(all providers)'} #{Time.now}",
config: {
import: './.travis/blank.yml', # skips .travis.yml
env: {
global: {
ID: SecureRandom.hex
}
},
matrix: {
include: jobs(opts)
}
}
}
}
if opts[:pretend]
puts JSON.pretty_generate(body)
else
res = post(uri, body, opts)
p res.code.to_i
puts res.body
end