LoginSignup
1
1

More than 1 year has passed since last update.

Github ActionsでMySQLが文字化けした場合の対応

Last updated at Posted at 2021-10-14

概要

Github ActionsでMySQLを使ったテストを書いたが文字列のassertの際に文字化けでエラーになってしまった。
そのため、Github ActionsでMySQLを使う場合に文字コードをUTF-8にする方法を書きました。

default mysql と mysql-action について

Github Actionsがデフォルトで用意しているmysql(default mysql) は、
charasetの変更ができず、テスト時の文字列比較でエラーになってしまうので、mysql-action を使用しました。

例. default mysql の場合、charset =latin1 になってしまう

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        ports:
          - 3306:3306
        env:
          MYSQL_ROOT_PASSWORD: root
        options: --health-cmd "mysqladmin ping -h localhost" --health-interval 20s --health-timeout 10s --health-retries 10
    steps:
      - name: show charset
        run: |
          mysql -h 127.0.0.1 --port 3306 -uroot -proot -e "show variables like 'chara%'"

↓ 結果

Variable_name   Value
character_set_client    latin1
character_set_connection    latin1
character_set_database  latin1
character_set_filesystem    binary
character_set_results   latin1
character_set_server    latin1
character_set_system    utf8
character_sets_dir  /usr/share/mysql/charsets/

これだと charset が latin1 のため、文字列のassertでエラーになってしまう
※ docker のように command で設定できません

例. mysql-action の場合、charsetを指定可能

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Setup mysql-action
        uses: mirromutth/mysql-action@v1.1
        with:
          host port: 3306 # Optional, default value is 3306. The port of host
          container port: 3306 # Optional, default value is 3306. The port of container
          character set server: 'utf8' # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld
          collation server: 'utf8_general_ci' # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld
          mysql version: '5.7' # Optional, default value is "latest". The version of the MySQL
          mysql database: 'test_db' # Optional, default value is "test". The specified database which will be create
          mysql root password: 'root' # Required if "mysql user" is empty, default is empty. The root superuser password
      - name: show charset
        run: |
          sleep 30
          mysql -h 127.0.0.1 --port 3306 -uroot -proot -e "show variables like 'chara%'"

↓ 結果

Variable_name   Value
character_set_client    utf8
character_set_connection    utf8
character_set_database  utf8
character_set_filesystem    binary
character_set_results   utf8
character_set_server    utf8
character_set_system    utf8
character_sets_dir  /usr/share/mysql/charsets/

utf8になった

MySQLのrootユーザーのパスワードについて

docker の mysql の場合、MYSQL_ALLOW_EMPTY_PASSWORD で指定することで root のパスワードを空にできますが、
mysql-action では、root のパスワードを空に設定できませんでした。
参考:
https://github.com/mirromutth/mysql-action/issues/13
https://github.com/mirromutth/mysql-action/issues/17

ちなみに default mysql の場合の root のデフォルトパスワード は、root です

ローカルPCでGithub Actionsを試したい

act を使うことで可能
下記を参考にインストール
https://github.com/nektos/act

リポジトリにrepo/.github/workflows/test.ymlがある状態で下記を実行

cd repo
act pull_request

※ default mysqlやdefault で用意されている aws cli などは、動きません。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1