-
Notifications
You must be signed in to change notification settings - Fork 26
/
build_context.rb
196 lines (156 loc) · 4.5 KB
/
build_context.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# frozen_string_literal: true
require "buildkite-builder"
module Buildkite::Config
class BuildContext < Buildkite::Builder::Extension
attr_accessor :ruby
attr_reader :rubies
attr_writer :default_ruby
def initialize(*)
@rubies = []
super
end
dsl do
def build_context
context.extensions.find(BuildContext)
end
end
def nightly?
ENV.has_key?("RAILS_CI_NIGHTLY")
end
def skip?
# [ci skip], [skip ci], [ci-skip], or [skip-ci]
[ENV["BUILDKITE_MESSAGE"], FetchPr.title].grep(/(ci skip|skip ci|ci-skip|skip-ci)/i).any?
end
def rails_root
Pathname.pwd
end
def rails_version
@rails_version ||= Gem::Version.new(rails_version_file)
end
def default_ruby
@default_ruby ||= rubies.select { |r| !r.soft_fail? }.first
end
def setup_rubies(ruby_minors)
@rubies = ruby_minors.sort.select { |v| Gem::Version.new(v) >= min_ruby }.map do |m|
v = Gem::Version.new(m)
rc = RubyConfig.new(version: m, prefix: "ruby:")
if max_ruby && v > max_ruby && !(max_ruby.approximate_recommendation === v)
rc.soft_fail = true
end
rc
end.tap do |rubies|
rubies.reverse!
rubies.sort_by { |r| [r.version.to_s, r.soft_fail] }
end
end
def bundler
case rails_version
when Gem::Requirement.new("< 6.1")
"< 2.2.10"
end
end
def rubygems
case rails_version
when Gem::Requirement.new("< 6.1")
"3.2.9"
end
end
def max_ruby
case rails_version
when Gem::Requirement.new("< 6.1")
Gem::Version.new("2.7")
when Gem::Requirement.new("< 8")
Gem::Version.new("3.3")
end
end
# IMAGE_BASE = "buildkite-config-base"
def image_base
ENV["DOCKER_IMAGE"] || remote_image_base
end
def image_name_for(source, prefix: "base:")
"#{prefix}#{image_base}:#{ruby.image_name_for(source)}"
end
def build_id
ENV["BUILDKITE_BUILD_ID"] || ENV["BUILD_ID"] || "build_id"
end
def rebuild_id
([ENV["BUILDKITE_REBUILT_FROM_BUILD_ID"]] - [""]).first
end
def base_branch
([ENV["BUILDKITE_PULL_REQUEST_BASE_BRANCH"], ENV["BUILDKITE_BRANCH"], "main"] - [""]).first
end
def local_branch
([ENV["BUILDKITE_BRANCH"], "main"] - [""]).first
end
def mainline
local_branch == "main" || local_branch =~ /\A[0-9-]+(?:-stable)?\z/
end
def ci?
ENV.has_key?("BUILDKITE") || ENV.has_key?("CI")
end
def pull_request
([ENV["BUILDKITE_PULL_REQUEST"]] - ["false"]).first
end
def standard_queues
[nil, "default", "builder"]
end
# If the pipeline is running in a non-standard queue, default to
# running everything in that queue.
def queue
unless standard_queues.include?(ENV["BUILDKITE_AGENT_META_DATA_QUEUE"])
ENV["BUILDKITE_AGENT_META_DATA_QUEUE"]
end
end
def supports_trilogy?
rails_version >= Gem::Version.new("7.1.0.alpha")
end
def has_railspect?
File.exist?(rails_root.join("tools/railspect"))
end
def support_guides_lint?
rails_version >= Gem::Version.new("7.1.0.beta1")
end
def test_with_multiple_versions_of_rack?(ruby)
ruby == default_ruby && rails_version >= Gem::Version.new("7.1.x")
end
def build_queue
ENV["BUILD_QUEUE"] || queue || "builder"
end
# RUN_QUEUE = ENV["RUN_QUEUE"] || ENV["QUEUE"] || "default"
def run_queue
ENV["RUN_QUEUE"] || queue || "default"
end
def artifact_paths
["test-reports/*/*.xml"]
end
def automatic_retry_on
[
{ exit_status: -1, limit: 2 },
{ exit_status: 255, limit: 2 },
]
end
def timeout_in_minutes
30
end
private
def rails_version_file
rails_root.join("RAILS_VERSION").read
end
def rails_gemspec
rails_root.join("rails.gemspec").read
end
def min_ruby
# Sets $1 below for MIN_RUBY
# e.g.:
# >> rails_root.join("rails.gemspec").read =~ /required_ruby_version[^0-9]+([0-9]+\.[0-9]+)/
# #=> 486
# >> Gem::Version.new($1)
# #=> Gem::Version.new("2.7")
rails_gemspec =~ /required_ruby_version[^0-9]+([0-9]+\.[0-9]+)/
Gem::Version.new($1 || "2.0")
end
def remote_image_base
"973266071021.dkr.ecr.us-east-1.amazonaws.com/#{"#{build_queue}-" unless standard_queues.include?(build_queue)}builds"
end
end
end