1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

#0115(2025/05/01)初心者向けGitHub Actions 入門ガイド

Posted at

GitHub Actions 入門ガイド

GitHub Actions は、GitHub リポジトリ上のさまざまな「イベント」をきっかけに、自動でジョブ(スクリプト)を実行できるCI/CDプラットフォームです。コードのビルド、テスト、デプロイから、定期バッチや通知まで、幅広い用途に使えます。


1. GitHub Actions の基本

  • ワークフロー(Workflow)

    • .github/workflows/ 配下に置く YAML ファイル
    • 「いつ」「何を」「どこで・どうやって」実行するかを定義
  • イベント(Triggers)

    • push(コードをプッシュしたとき)
    • pull_request(PRを作成・更新したとき)
    • schedule(cron形式で定期実行)
    • workflow_dispatch(手動ボタンで起動) など
  • ジョブ(Jobs)

    • ワークフロー内で並列/順序指定できる「作業単位」
    • 例:ビルドジョブ、テストジョブ、デプロイジョブ
  • ステップ(Steps)

    • ジョブ内で順に実行する処理
    • 例:リポジトリチェックアウト→依存インストール→テスト実行など

2. よく使う活用シーン

シーン 具体例
🚀 継続的インテグレーション(CI) プッシュやPR時に自動でビルド&ユニットテストを実行し、品質を担保
📦 継続的デリバリー(CD) mainブランチにマージされたらステージング/本番環境に自動デプロイ
🔍 静的解析・リント ESLint, flake8, MyPy, SonarCloudなどでコード品質チェック
📶 定期バッチ 毎朝9時に依存ライブラリのアップデートチェック、毎週日曜にバックアップ取得
🔔 通知・レポート SlackやTeamsにCI結果を通知、リリースノートの自動更新
📈 インフラ構成管理(IaC) TerraformやPulumiを使ってコードでクラウドリソースを構築・更新

3. ワークフローファイルの基本構成

# ファイル: .github/workflows/ci.yml
name: CI             # 任意の名前

# トリガー設定
on:
  push:
    branches: [ main ]         # mainブランチへのpushで実行
  pull_request:
    branches: [ main ]         # main向けPRでも実行
  schedule:
    - cron: '0 9 * * 1-5'      # 平日毎朝9時

jobs:
  build-and-test:
    name: Build & Test        # ジョブ名
    runs-on: ubuntu-latest    # 実行環境

    steps:
      - name: Checkout code
        uses: actions/checkout@v3    # コードを取得

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Install dependencies
        run: npm ci

      - name: Run linter
        run: npm run lint

      - name: Run tests
        run: npm test

ポイント:
1. on: で実行タイミングを指定
2. jobs: 配下に並列/順序設定
3. uses: は「公式/OSSのアクション」を呼び出す
4. run: はシェルコマンド実行


4. 実践例:PythonプロジェクトのCI

以下は、Python プロジェクトでユニットテストと静的解析を行い、Coverageをレポートする例です。

# .github/workflows/python-ci.yml
name: Python CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test-lint:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python 3.10
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install flake8 pytest coverage codecov

      - name: Lint with flake8
        run: flake8 src tests

      - name: Run tests with coverage
        run: |
          coverage run -m pytest
          coverage xml

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: coverage.xml
  • uses: actions/setup-python でPython環境を準備
  • run: でパイプライン処理をまとめて記述
  • codecov-action でカバレッジを可視化

5. 次のステップ

  1. マトリクス戦略
    • 例:複数のNode/Pythonバージョン、OSでテスト実行
  2. キャッシュ
    • ctions/cache で依存インストール高速化
  3. デプロイ
    • workflow_dispatch + appleboy/ssh-action で手動承認付きデプロイ
    • AWS/GCP/Azure Actions でクラウド自動化
  4. 通知・レポート
    • slackapi/slack-github-action でSlack通知
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?