🗂

GitHub Workflowとは

に公開

GitHub Workflowとは

GitHub Workflow(GitHub Actionsのワークフロー)とは、GitHub上でテストやデプロイなどの処理を自動的に実行する仕組みです。CI/CD(継続的インテグレーション/継続的デリバリー)を行う際によく使われます。


概要

  • .github/workflows/ フォルダに YAML ファイルを置いて定義する
  • pushpull_request などのイベントをトリガーに実行される
  • テスト、ビルド、静的解析、デプロイなどに利用される

構成要素(Workflowファイルの中身)

  • name
    Workflowの名前。GitHub上での表示名になります。

  • on
    実行をトリガーとするイベント。例:push, pull_request, schedule など。

  • jobs
    実行する処理単位。複数のジョブを並行または直列に定義できる。

  • runs-on
    使用する仮想環境。例:ubuntu-latest, windows-latest, macos-latest など。

  • steps
    ジョブ内で実行する個々の処理。コマンドや外部アクションを定義。


例:RailsでRSpecを実行するWorkflow

name: Rails Test

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:13
        ports: ['5432:5432']
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: リポジトリをチェックアウト
        uses: actions/checkout@v3

      - name: Rubyをセットアップ
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.2

      - name: Gemをインストール
        run: bundle install --jobs 4 --retry 3

      - name: DBをセットアップ
        run: |
          cp config/database.yml.github_actions config/database.yml
          bundle exec rails db:create db:schema:load

      - name: RSpecを実行
        run: bundle exec rspec

Discussion