LoginSignup
8
3

More than 5 years have passed since last update.

Bitbucket Pipelines + Rails + MySQL でRspec を実行

Last updated at Posted at 2019-04-14

Bitbucket Pipelines とは

公式の通り、Atlassian製のCIツール。
他にまとめられている方がいるのでこの辺りの詳細はぶん投げ:relaxed:

やりたいこと

開発環境をRuby -> Ruby on Docker へ移したため、Pipelines の設定もそれに追従させたい。

対応したこと

今回は環境変数を .env で管理しているため、テスト環境で使用する .env.test ファイルを用意。

.env.test
RDS_USERNAME=root
RDS_PASSWORD=password
RDS_HOSTNAME=127.0.0.1

ひとまず以下成功した設定 :tada:

bitbucket-pipelines.yml
pipelines:
  branches:
    development:
      - step:
          image: ruby:2.4.4
          caches:
            - bundler
          script:
            - apt-get update && apt-get install -y nodejs
            - bundle install
            - cp .env.test.env
            - bundle exec rails db:setup RAILS_ENV=test
            - bundle exec rake db:migrate RAILS_ENV=test
            - bundle exec rspec
          services:
            - db
definitions:
  caches:
    bundler: ./vendor
  services:
    db:
      image: mysql:5.7
      environment:
        MYSQL_ROOT_PASSWORD: password

Ruby のupgrade は明日から本気出す。

上の設定で大事なこと①

definitions: services: db: environment: 以下に MYSQL_DATABASE: を設定しない。
これを設定して実行すると、MySQLがdb を作成し rails db:setup で文字コードが合わず?エラーが発生する :sob:

:writing_hand: 以下発生したエラー
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect string value: '\xE3\x81\xBF\xE3\x81\x9A...' for column 'hoge_hoge' at row 1: INSERT INTO `hoges` (`hoge_id`, `hoge_name`, `hoge_name_kana`) VALUES ('1', '???', '????')

上の設定で大事なこと②

Bitbucket Pipelines でデータベースを使用する場合、ポートマッピングやホストの設定は不要。
公式曰く、 localhost で動くお:relaxed:とのこと。

パイプラインの実行時、bitbucket-pipeline.yml のステップで参照されているサービスが、パイプライン ステップ内で実行されるようにスケジューリングされます。これらのサービスは、ビルド コンテナとネットワーク アダプタを共有するほか、localhost でオープンなポートを共有します。ポート マッピングやホスト名は不要です。たとえば、Postgres を使用している場合、localhost のポート 5432 に接続するだけでテストを行えます。

知らずにホストをdocker-compose.yml のホスト名と合わせて db 等にすると、以下のエラーが発生し延々としらばっくれられて心の傷を負うことになるので注意。
Mysql2::Error: Unknown MySQL server host 'db' (-2)

上の設定で大事なこと③

.env.testRDS_HOSTNAME は 127.0.0.1 に設定する。
公式の説明をみると、Unixソケットを介して接続することがあるため、localhostの使用は避けてとのこと。

Connecting to MySQL

If you use the example above, MySQL (version 5.7) will be available on:

Host name: 127.0.0.1 (avoid using localhost, as some clients will attempt to connect via a local "Unix socket", which will not work in Pipelines)
Port: 3306
Default database: pipelines
User: test_user, password: test_user_password. (The root user of MySQL will not be accessible.)
You will need to populate the pipelines database with your tables and schema. If you need to configure the underlying database engine further, refer to the official Docker Hub image for details.

いやlocalhost で動くって言ってたじゃん!と思うが、localhost のまま実行をすると Mysql2::Error: Unknown MySQL server host 'localhost' (-2) とキレられて辛酸を嘗めることになるので注意。

最後に

意外と Bitbucket Pipelines + Rspec の記事少なかった(ような気がした)ので、誰かの救いになれば良いな...... :pray:

8
3
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
8
3