-
Notifications
You must be signed in to change notification settings - Fork 26
/
trigger-pipeline
executable file
·57 lines (47 loc) · 1.5 KB
/
trigger-pipeline
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
#!/usr/bin/env ruby
# frozen_string_literal: true
# Example usage:
# bin/trigger-pipeline [options] org pipeline [slug]
require "buildkit"
require "optparse"
def buildkite_token
ENV.fetch("BUILDKITE_TOKEN") {
raise "BUILDKITE_TOKEN undefined!\nMake sure your BUILDKITE_TOKEN has `write_builds` scope too!"
}
end
# Make sure your BUILDKITE_TOKEN has `write_builds` scope
def trigger_pipeline(org, pipeline, slug, branch: "main", commit: "HEAD", config_branch: nil, fork: nil, pr: nil)
env = {}
slug ||= pipeline
puts "Trigger build for #{pipeline}..."
configuration = File.read("pipelines/#{slug}/initial.yml")
client = Buildkit.new(token: buildkite_token)
if pr && fork
env["BUILDKITE_PULL_REQUEST"] = pr
env["BUILDKITE_PULL_REQUEST_BASE_BRANCH"] = "main"
env["BUILDKITE_PULL_REQUEST_REPO"] = "https://github.com/#{fork}/buildkite-config.git"
end
if config_branch && fork
env["CONFIG_REPO"] = "https://github.com/#{fork}/buildkite-config.git"
env["CONFIG_BRANCH"] = config_branch
end
resp = client.create_build(org, pipeline, {
branch: branch,
commit: commit,
override_steps_configuration: configuration,
env: env,
})
puts resp.web_url
end
options = {}
OptionParser.new do |opts|
opts.on("--branch=BRANCH")
opts.on("--commit=COMMIT")
opts.on("--config_branch=CONFIG_BRANCH")
opts.on("--fork=FORK")
opts.on("--pr=PR")
end.parse!(into: options)
org = ARGV.shift
pipeline = ARGV.shift
slug = ARGV.shift
trigger_pipeline(org, pipeline, slug, **options)