17
10

More than 3 years have passed since last update.

【GitHub Actions】 RailsアプリをRuboCop/ESLintチェック+Slack通知+キャッシュする

Last updated at Posted at 2019-11-20

本記事は、先日投稿したNuxtアプリをGitHub ActionsでGAE環境にデプロイするRailsアプリ版です

やりたいこと

  • GitHub Actions上で「RuboCop/ESLint」のチェックを実行する
  • 上記の実行結果をSlackに通知する
  • ワークフローの高速化のためにbundle installyarn installの結果をキャッシュする

対応内容

以下の設定で実現できました

.github/workflows/feature.yml
name: Rubocop & ESLint checks

on:
  push:
    branches:
      - feature/*
      - hotfix/*

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        ruby: [ '2.6.x' ]

    steps:
      - uses: actions/checkout@v1

      - name: Set up Ruby version
        uses: actions/setup-ruby@master
        with:
          ruby-version : ${{ matrix.ruby }}

      - name: apt-get
        run: |
          sudo apt-get update
          sudo apt-get install libmysqlclient-dev

      - name: Set up bundler
        run: gem install bundler

      - name: Cache gems
        uses: actions/cache@preview
        with:
          path: vendor/bundle
          key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-gem-

      - name: Cache node modules
        uses: actions/cache@preview
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: bundle install
        run: bundle install --path vendor/bundle

      - name: yarn install
        run: yarn install

      - name: Rubocop checks
        run: bundle exec rubocop

      - name: ESLint checks
        run: yarn run eslint:check

      - name: Slack Notification(Failed)
        if: failure()
        uses: rtCamp/action-slack-notify@master
        env:
          SLACK_CHANNEL: github-actions
          SLACK_ICON: https://github.co.jp/assets/images/features/actions/actions-icon-actions.svg
          SLACK_MESSAGE: 'Actions Failed :-1:'
          SLACK_TITLE: 'feature/hotfix'
          SLACK_USERNAME: GitHub Actions
          SLACK_WEBHOOK: {Incoming Webhookで設定したURLを指定}
          SLACK_COLOR: '#FF0000'

この設定はfeature/*hotfix/*ブランチのpush時にワークフローが走りますが、これらのブランチは頻繁にpushが起きると思うのでワークフローの成功時のSlack通知は設定してません
(developにマージする前にエラーは検知したいので、失敗時は通知するようにしています)

ハマりどころ

mysql2のgemを利用している場合、事前にsudo apt-get install libmysqlclient-devでMySQLクライアントを入れておかないとbundle installを実行時に以下のエラーで怒られます

mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or
'yum install mysql-devel', and try again.

公式のマーケットプレイスを利用する選択肢

GitHubの公式マーケットプレイスには、お手軽にRuboCop/ESLintでエラーと警告のチェックが実現できる仕組みが用意されています。
本当はこれを利用したかったのですが、以下のエラーが発生し解決することができませんでした...
スクリーンショット 2019-11-20 22.04.20.png
※ ESLintも同じようなDockerのエラーが発生

そのため、今まで手動で実行していたRubocop/eslintコマンドをGitHub Actionsにそのまま移植して実行する方法を採用しました

参考

17
10
1

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
17
10