4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

駆け出しエンジニアのCI改善(その2 GithubActionsでテスト実行)

Last updated at Posted at 2022-04-08

自己紹介: 駆け出しエンジニア

はじめまして。
私はRailsで自社サービスのWeb開発を行っている駆け出しエンジニアです。
この度はGithubActionsによる自動テストの作成を担当しました。

2020年 11月 エンジニアとしての学習を開始
2021年 5月 エンジニアへ転職
2022年 2月 テスト自動化 :tada:
2022年 4月 現在(11ヶ月)

現時点で動くものを作ることができましたので、これからさらに磨きをかけて行きます。
より良い方法、至らない点については優しく指摘頂ければ幸いです。

実行環境: Ruby3.0&Rail6.0

Ruby 3.0.3
Rails 6.0.4.6

目標:GithubActionsでテストを並列実行する

最終的な目標はRspecのテストをGithubActionsで並列で実行することです。
4つのステップに分けて進めて行きます。

  1. テスト用DB作成の為にseed.rbを作成する
  2. GithubActionsで自動テストを作成する ← 今回の記事
  3. 自動テスト実行時にテストカバレッジの測定をする
  4. GithubActionsのテストを並列で実行したい (目標)

今回は2の GithubActionsで自動テストを作成する について記載します。

今回やること: GithubActionsでRSpecを実行する

  • 前回作成した seed.rb を使用して、GithubActionsでテスト用のDBを作成する(前回の記事)
  • GithubActionsでRSpecのテストを実行する

完成したもの: GithubActionsの設定ファイル

.github/Dockerfile
# バージョンは省略しています
FROM postgres
RUN localedef -i ja_JP -c -f UTF-8 -A /usr/share/locale/locale.alias ja_JP.UTF-8
ENV LANG ja_JP.utf8
.github/workflows/rspec.yml
name: RSpec

env:
  DB_HOST: localhost
  DB_PASSWORD: db_password
  TZ: Asia/Tokyo

on:
  pull_request:
    types: [opened, reopened, synchronize]

jobs:
  test:
    name: Run RSpec
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: DB Build and Run
        run: |
          docker build -t db_image .github/workflows
          docker run -d --name db_name -p 5432:5432 -e POSTGRES_PASSWORD=db_password db_image

      - name: Bundle Install
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true

      - name: Create DB User
        run: createuser -h localhost -U postgres -d user_name

      - name: Setup Database
        run: bundle exec rails db:setup
        env:
          RAILS_MASTER_KEY: ${{ secrets.CI_MASTER_KEY }}

      # 自社独自のrakeタスク(詳細については割愛します)
      # development環境のDBからtest環境のDBを作成する
      - name: Test Prepare
        run: bin/rake test_prepare

      - name: Run RSpec
        run: bundle exec rspec
        env:
          RAILS_MASTER_KEY: ${{ secrets.CI_MASTER_KEY }}

実行タイミング: PullRequestで動作する

自社では、タスク毎にブランチを切って開発を行い、PullRequestを作成してレビューを受けてからマージしています。
その為、動作タイミングをPullRequest作成時、またはPullRequestへのコミット追加時に実行されるように設定しています。
参考: https://docs.github.com/ja/actions/using-workflows/events-that-trigger-workflows#pull_request

ルール: テストをパスしないとマージできない

上述の通り、マージする為にはPurllRequestを作成しているので、
テストがパスしないとマージできないように設定しました。
設定方法は Require status checks to pass before merging にチェックを追加し、今回作成したGithubActionsを選択し追加します。

参考: https://docs.github.com/ja/repositories/configuring-branches-and-merges-in-your-repository/defining-the-mergeability-of-pull-requests/managing-a-branch-protection-rule

取り組み: DBのDockerイメージを用意した

GithubActionsにはサービスコンテナが用意されていましたが、localeがja_JP.UTF-8に対応していませんでした。
以下を参考にして、用意したDockerイメージを使用するようにしました。

参考: https://github.community/t/add-locale-to-the-service-container/167530

細かいチェックポイント: 環境変数の指定など

  1. TZの設定
    自社のテスト結果がJSTとなっている箇所があり、
    GithubActionsでのデフォルトではUTCの時間が使われていた為、
    TZ: Asia/Tokyo と全体の環境変数を設定することでJSTとなるようにしました。

    .github/workflows/rspec.yml
    env:
      TZ: Asia/Tokyo
    
  2. Railsが起動しない
    Railsにはcredetialsという秘匿情報を管理する仕組みがあり、参照する為には master.key が必要となっています。
    この master.key はバージョン管理に含めないようにしている為、Actions secretsで用意しました。
    参考: https://docs.github.com/ja/github-ae@latest/actions/security-guides/encrypted-secrets

    .github/workflows/rspec.yml
    env:
      RAILS_MASTER_KEY: ${{ secrets.CI_MASTER_KEY }}
    
  3. Githubでデバッグを有効にする
    Actions secretsでACTIONS_STEP_DEBUGACTIONS_RUNNER_DEBUGを設定し値をtrueにすることでデバッグが有効になります。
    たくさん出力されますが、何がダメかわからないより良いと思うので、私はGithubでの作業中は設定するようにしています。
    参考: https://docs.github.com/ja/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging

次回: 自動テスト実行時にテストカバレッジの測定をする

GithubActionsでテストが実行できるようになりました。
次回は引き続きCI改善の為にテストカバレッジの測定を行いたいと思います。

最後まで読んで頂きありがとうございました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?