6
4

More than 3 years have passed since last update.

【初心者・最低限の設定】PostgresqlでCircleCiを使ってRspecをテストする方法

Last updated at Posted at 2020-05-11

初めてCircleCiを動かす人向けの記事になります。
・Postgresql9.6
・ruby 2.6.3
・githubのmasterブランチにpushすればrspecを回してくれる設定です。
・自動でデプロイする昨日は今回はつけていません。

※本番環境への自動デプロイはgithubのmasterブランチにpushしてrspecを動かせるようになってからの設定をおすすめします。

config.ymlの設定はCircleCiの公式設定をそのままパクっています。

まずは公式HPの設定をそのままconfig.ymlに貼り付けてから下記を参考にすると、エラーが少なく設定できると思います。

config.yml
version: 2
jobs:
   build:
      docker:
        #アプリのバージョンを指定
        - image: circleci/ruby:2.6.3-node
          environment:
            #自身の登録しているユーザに変える。
            POSTGRES_USER: postgres 
            PGHOST: 127.0.0.1
            RAILS_ENV: test
        #使ってるデータベースとバージョンを指定する。# 「host: localhost」でアクセスできるサービスコンテナイメージ。
        - image: circleci/postgres:9.6
          environment:
            #それぞれ設定
            POSTGRES_USER: postgres
            #下記を書けばパスワードなしでデータベースにログインしてくれる。 
            POSTGRES_HOST_AUTH_METHOD: trust
            POSTGRES_DB: portfolio_test
      #自身のアプリ名にする
      working_directory: ~/Portfolio
      steps:
        - checkout
        #bundle キャッシュをリストアする。公式の設定通り。
        - restore_cache:
            keys:
              - v1-dependencies-{{ checksum "Gemfile.lock" }}
              - v1-dependencies-
        # bundle install で依存関係をインストールする。公式の設定と多少異なってます。
        - run:
            name: install dependencies
            command: |
              gem install bundler -v 2.1.4
              bundle install --jobs=4 --retry=3 --path vendor/bundle

        # bundle キャッシュを保存する。公式の設定通り。
        - save_cache:
            key: v1-dependencies-{{ checksum "Gemfile.lock" }}
            paths:
              - ./vendor/bundle

        #↓追加する
        - run:
            name: DBの起動を待つ
            command: dockerize -wait tcp://127.0.0.1:5432 -timeout 120s

        - run: bundle exec rake db:create
        - run: bundle exec rake db:schema:load

        # rspecを実行する
        - run:
            name: rspecを実行
            command: bundle exec rspec

この状態でgithubに

$  git add -A

$  git commit -m"CI"

$  git push origin master

をすれば動きます。

遭遇したエラー

以下のエラー。

server started
psql: could not connect to server: Connection refused
    Is the server running on host "127.0.0.1" and accepting
    TCP/IP connections on port 5432?
LOG:  received SIGHUP, reloading configuration files
LOG:  parameter "listen_addresses" cannot be changed without restarting the server
LOG:  configuration file "/var/lib/postgresql/data/postgresql.conf" contains errors; unaffected changes were applied

原因はconfig.yml以下にPGHOST: 127.0.0.1を書いていなかった。

version: 2
jobs:
   build:
      docker:
        #アプリのバージョンを指定
        - image: circleci/ruby:2.6.3-node
          environment:
            POSTGRES_USER: postgres
            PGHOST: 127.0.0.1
            RAILS_ENV: test

環境変数が読み込めない

以下のエラー。

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       connections without a password. This is *not* recommended.

       See PostgreSQL documentation about "trust":
       https://www.postgresql.org/docs/current/auth-trust.html

postgresは.envの書き方で格納してしまうと読み込めません。
したがって私はユーザ名は直接記入しました。(後々調べて環境変数に格納します。)

ymlファイルの構文エラー

masterにpushした際に構文エラーが出るとだるいので一度config.ymlを書いたら下記コマンドで構文が正しいかチェックしましょう。

$  circleci config validate

bundle install で依存関係をインストールする場所のバージョンエラー

# bundle install で依存関係をインストールする。公式の設定と多少異なってます。
  - run:
      name: install dependencies
      command: |
      gem install bundler -v 2.1.4
      bundle install --jobs=4 --retry=3 --path vendor/bundle

CircleCiを動かすポイント

公式サイトのconfig.ymlをそのまま自身の設定にして、とりあえずmasterにpushして動かすことが一番エラーが出ないかと思われます。
エラーが出たら、circleciの画面に入りエラー文をみながら修正しましょう。
意外と親切に最低限の必要なコマンド、コメントアウトが残されているので参考にしないてはありません。

6
4
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
6
4