How to Fix Encoding::CompatibilityError When Running git-pr-release on GitHub Actions ubuntu-slim
Introduction
Recently, GitHub Actions introduced a new lightweight ubuntu-slim runner with just 1 vCPU.
I immediately began migrating lighter workflows to use this runner. However, when trying to generate release Pull Requests using git-pr-release, I encountered the following error:
(erb):34:in 'block in Git::Pr::Release::Util#build_pr_title_and_body': incompatible character encodings: BINARY (ASCII-8BIT) and UTF-8 (Encoding::CompatibilityError)
This post outlines how I resolved the issue.
Solution
The root cause appears to be that Ruby on the ubuntu-slim runner defaults to ASCII-8BIT encoding. When the Pull Request title contains UTF-8 characters (e.g., Japanese), the script throws an error.
When I changed the PR title to English, the error disappeared. So, the problem is definitely related to encoding.
The fix is to set the Ruby environment variable RUBYOPT to include the encoding option -EUTF-8:
runs-on: ubuntu-slim
env:
RUBYOPT: -EUTF-8
Initially, I tried to fix it by adding a magic encoding comment in the ERB file. However, git-pr-release uses the first line of the template as the title, and adding the encoding comment caused a code: missing_field error.
<%# encoding: UTF-8 %>
Conclusion
The new ubuntu-slim runner is great for lightweight workflows. Besides generating release Pull Requests, I’ve also migrated tasks like linting and release note generation. So far, it’s been working well and has helped reduce costs effectively.