提供了一个 DSL,用于声明一个可以在本地或云端运行的持续集成工作流。每个步骤都会被计时,报告成功/失败,并汇总成一个整体报告,该报告会报告总运行时长,以及整个运行是否成功。
示例
ActiveSupport::ContinuousIntegration.run do step "Setup", "bin/setup --skip-server" step "Style: Ruby", "bin/rubocop" step "Security: Gem audit", "bin/bundler-audit" step "Tests: Rails", "bin/rails test test:system" if success? step "Signoff: Ready for merge and deploy", "gh signoff" else failure "Skipping signoff; CI failed.", "Fix the issues and try again." end end
从 Rails 8.1 开始,会创建一个默认的 `bin/ci` 和 `config/ci.rb` 文件,以提供开箱即用的 CI。
方法
常量
| COLORS | = | { banner: "\033[1;32m", # 绿色 title: "\033[1;35m", # 紫色 subtitle: "\033[1;90m", # 中灰色 error: "\033[1;31m", # 红色 success: "\033[1;32m" # 绿色 } |
Attributes
| [R] | results |
类公共方法
new() 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/continuous_integration.rb, line 64 def initialize @results = [] end
run(title = "Continuous Integration", subtitle = "Running tests, style checks, and security audits", &block) 链接
执行一次 CI 运行。执行每个步骤,显示其结果和运行时长,如果出现任何失败,则以非零状态退出。
传入一个可选的标题、副标题和一个声明要执行步骤的代码块。
将 CI 环境变量设置为 “true”,以允许应用中的条件行为,例如启用预加载和禁用日志记录。
示例
ActiveSupport::ContinuousIntegration.run do step "Setup", "bin/setup --skip-server" step "Style: Ruby", "bin/rubocop" step "Security: Gem audit", "bin/bundler-audit" step "Tests: Rails", "bin/rails test test:system" if success? step "Signoff: Ready for merge and deploy", "gh signoff" else failure "Skipping signoff; CI failed.", "Fix the issues and try again." end end
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/continuous_integration.rb, line 55 def self.run(title = "Continuous Integration", subtitle = "Running tests, style checks, and security audits", &block) new.tap do |ci| ENV["CI"] = "true" ci.heading title, subtitle, padding: false ci.report(title, &block) abort unless ci.success? end end
实例公共方法
echo(text, type:) 链接
以与文本类型相对应的颜色将文本回显到终端。
示例
echo "This is going to be green!", type: :success echo "This is going to be red!", type: :error
有关选项的完整列表,请参阅 ActiveSupport::ContinuousIntegration::COLORS。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/continuous_integration.rb, line 111 def echo(text, type:) puts colorize(text, type) end
failure(title, subtitle = nil) 链接
显示一个带有标题和可选副标题的错误标题,以反映运行失败。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/continuous_integration.rb, line 86 def failure(title, subtitle = nil) heading title, subtitle, type: :error end
heading(heading, subtitle = nil, type: :banner, padding: true) 链接
显示一个彩色标题,后面跟着一个可选的副标题。
示例
heading "Smoke Testing", "End-to-end tests verifying key functionality", padding: false heading "Skipping video encoding tests", "Install FFmpeg to run these tests", type: :error
有关选项的完整列表,请参阅 ActiveSupport::ContinuousIntegration::COLORS。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/continuous_integration.rb, line 98 def heading(heading, subtitle = nil, type: :banner, padding: true) echo "#{padding ? "\n\n" : ""}#{heading}", type: type echo "#{subtitle}#{padding ? "\n" : ""}", type: :subtitle if subtitle end
step(title, *command) 链接
声明一个带有标题和命令的步骤。命令可以是一个单独的字符串,也可以是多个字符串,这些字符串将被作为单独的参数传递给 ‘system`(因此可以正确地转义路径等)。
示例
step "Setup", "bin/setup" step "Single test", "bin/rails", "test", "--name", "test_that_is_one"
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/continuous_integration.rb, line 75 def step(title, *command) heading title, command.join(" "), type: :title report(title) { results << system(*command) } end
success?() 链接
如果所有步骤都成功,则返回 true。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/continuous_integration.rb, line 81 def success? results.all? end