ruby-on-rails - 使用 Github 操作测试生产 Rails 服务器启动
问题描述
我使用 Github Action 来运行 RSpec 测试。我确信 Rails 服务器可以在生产模式下成功运行。成功,我的意思是这个命令运行了几秒钟而不引发异常。
bundle exec rails s -e production
如果我们将此步骤添加到操作工作流中,如果成功,我们将无法停止服务器,因为它会永远运行。
- name: RSpec
run: |
bundle exec rspec ./spec
- name: Boot Rails server
run: bundle exec rails s -e production
解决方案
我建议编写一个脚本并将其放入bin/test_boot
:
#!/usr/bin/env ruby
pid = Process.spawn("bundle exec rails s -e production") # run the server and get its PID
sleep 5 # time in seconds you need to ensure the server boots without errors
Process.kill("INT", pid) # send ^C, gracefully terminate the server
_, status = Process.wait2(pid) # wait for the server termination and get its exit code
exit status.exitstatus # exit from the script with the same code as the server
并将 CI 配置文件更新为:
- name: Boot Rails server
run: bin/test_boot
CI 管道通过每个步骤的退出代码来决定是否失败,并且该Boot Rails server
步骤以与服务器相同的代码退出。