Help us understand the problem. What is going on with this article?

CircleCI2.0でDatabaseを使ったテストをする

More than 1 year has passed since last update.

Circle2.0ではDatabaseのDocker imageを使ってテストをすることが可能です。

MySQLを使ったテストをやってみます。

config.yml

.circleci/config.yaml
version: 2
jobs:
  build:
    working_directory: ~/repo
    docker:
      - image: circleci/php:7.2-apache-node-browsers
        environment:
        - DB_HOST=127.0.0.1
        - DB_PORT=3306
        - DB_DATABASE=circleci
        - DB_USERNAME=circleci
        - DB_PASSWORD=circleci
      - image: circleci/mysql:5.6.39-ram
        environment:
        - MYSQL_ROOT_PASSWORD=circleci
        - MYSQL_DATABASE=circleci
        - MYSQL_USER=circleci
        - MYSQL_PASSWORD=circleci
    steps:
      - checkout
      - run:
          name: install dependecies
          command: composer install 
      - run:
          name: install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.3.0
      - run:
          name: Wait for db
          command: dockerize -wait tcp://localhost:3306 -timeout 1m
      - run:
          name: Test
          command: phpunit

docker imageの指定と環境変数の設定

ひとつめのイメージはアプリケーション、2つめはMySQLのイメージを指定しています。

MySQLはcircleciのしかも-ramがついたイメージを使うと高速にテストできます。

また、DB_HOST127.0.0.1です。

僕はlocalhostとやってハマりました。

以下はそのときのエラーです。

In Connection.php line 664:

  SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = amex and table_name = migrations)


In Connector.php line 67:

  SQLSTATE[HY000] [2002] No such file or directory

dockerize

MySQLが立ち上がる前にテストが開始されてもテストはうまくいきません。
そこで、dockerizeを使って、使用可能になるまで処理を止めておきます。

.circleci/config.yaml
      - run:
          name: install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.3.0

https://circleci.com/docs/2.0/postgres-config/#using-dockerize

まとめ

  • hostは127.0.0.1
  • dockerizeを使う
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away