Overview
This post report a way to set configuration only for the CI Envirionment. Rails application flequently use RSpec in CI/CD pipeline. Like many others I also follow this approach in Rails application development.
One day, I found numerous logs displayed in the GitHub Actions output during the RSpec process.Following error code is diplayed in real.
log writing failed. "\xE4" from ASCII-8BIT to UTF-8
log writing failed. "\xE4" from ASCII-8BIT to UTF-8
・
・
・
I researched reason why this error occurred. As a result, I found that the process of handling CSV in RSpec caused this error. However, I couldn't stop using the existing RSpec. So I tried resolve this error.
What is the reason?
I investigated the output when running RSpec in my local development environment and found that the above error occurred when Rails attempted to write execution logs to the local storage of the Docker container. Both my local and CI environments used UTF-8 encoding. However, RSpec attempted to write logs in ASCII-8BIT while handling CSV files.
Resolving each error one by one would require significant effort. Therefore, I decided to disable log writing to log/development.log in local storage. At first glance, this decision may seem like an act of laziness or an attempt to avoid solving the problem. However, it not only resolves the previously mentioned issue but also helps reduce CI execution time.
RSpec runs inside a Docker container as defined in the CI.yml file. Logs written inside the container during CI execution cannot be viewed unless they are transferred to another environment. However, this is hardly a problem.
How to make Rails identify CI environment
Simply add the following code to the appropriate configuration file:
if ENV['CI']
config.logger = ActiveSupport::TaggedLogging.new(Logger.new(nil))
end
ENV['CI'] is set by CI/CD tools by default.