[GitHub Actions] Pull Request時に自動テスト/prettier/ESLintを実行する

github-actions-prettier-eslint
INDEX

はじめに

GitHub ActionsでCI/CDで自動テスト/prettier/ESLintを実行する方法を学習しました!
その結果を執筆します。

結論

.github/workflows/code_check.yamlを作成し、ブランチに取り込む

使い方

1. github/workflows配下にcode_check.yamlを作成する

mkdir -p .github/workflows && touch .github/workflows/code_check.yaml

2. code_check.yamlを記述する

  • 自動テスト
  • prettier
  • ESLint
# Actionの名前
name: Code Check

on:
  # Pull Request時
  pull_request:
    # 対象のブランチを指定
    branches: ['test_CI/CD']

jobs:
  Code-Check:
    runs-on: ubuntu-latest
    steps:
      # GitHubリポジトリのコードをチェックアウト
      - uses: actions/checkout@v3
      # Node.js v18をセットアップ
      - name: Use Node.js v18
        # npmのキャッシュを有効
        # 以前にインストールしたnpmパッケージを再利用でき、ビルド時間を短縮
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'npm'
      # プロジェクトの依存関係をインストール
      - run: npm ci
      # prettierでコードのフォーマットをチェック
      - run: npm run prettier-format
      # ESLintでコードをチェック
      - run: npm run eslint
      # 自動テストの結果/カバレッジをチェック
      - run: npm run test:cov test/
自動テスト/prettier/ESLintの各コマンドは、所属プロジェクトのpackage.jsonscriptsを確認してください

3. 開発ブランチに取り込み完了!

今後のPR(Pull Request)を出されたときにGitHub Actionsが実行されます!
よかったらシェアしてね!
  • URLをコピーしました!

この記事を書いた人

Born in 1994
Engineer's career is from 2020.10
Skill: Next.js, TypeScript, Django, Python, HTML, CSS

INDEX