コンテンツにスキップ

GitHub Actions基礎

この章では、GitHub Actionsの基本概念、ワークフローの構造、トリガー、ランナーについて学びます。

GitHub Actionsは、GitHubに組み込まれたCI/CD(継続的インテグレーション/継続的デリバリー)プラットフォームです。

  • 自動テスト
  • 自動ビルド
  • 自動デプロイ
  • コードの自動チェック(lint, format)
  • 定期実行タスク
  • Issue/PR の自動化
.github/
└── workflows/
├── ci.yml
├── deploy.yml
└── release.yml
.github/workflows/ci.yml
name: CI # ワークフロー名
on: # トリガー
push:
branches: [main]
pull_request:
branches: [main]
jobs: # ジョブ定義
test: # ジョブ名
runs-on: ubuntu-latest # ランナー
steps: # ステップ
- uses: actions/checkout@v4 # アクション
- name: Run tests # ステップ名
run: npm test # コマンド
要素説明
Workflow自動化プロセス全体(YAMLファイル)
Eventワークフローを起動するトリガー
Job同じランナーで実行されるステップの集合
Step個々のタスク(コマンドまたはアクション)
Action再利用可能なステップ
Runnerジョブを実行するサーバー
on:
push:
branches:
- main
- 'release/**'
paths:
- 'src/**'
- '!src/**/*.md' # 除外
pull_request:
branches: [main]
types: [opened, synchronize, reopened]
# スケジュール実行
on:
schedule:
- cron: '0 0 * * *' # 毎日0時(UTC)
# 手動実行
on:
workflow_dispatch:
inputs:
environment:
description: 'Deploy environment'
required: true
default: 'staging'
# リリース
on:
release:
types: [published]
# Issue / PR イベント
on:
issues:
types: [opened, labeled]
pull_request_review:
types: [submitted]
on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: '0 0 * * 0' # 毎週日曜
workflow_dispatch:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm run lint
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm test
build:
runs-on: ubuntu-latest
needs: [lint, test] # 依存関係
steps:
- uses: actions/checkout@v4
- run: npm run build
steps:
# アクションを使用
- uses: actions/checkout@v4
# アクション + 設定
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
# コマンドを実行
- run: npm install
# 名前付きステップ
- name: Run tests
run: npm test
# 複数行コマンド
- name: Build and deploy
run: |
npm run build
npm run deploy
# 環境変数
- name: With env
env:
NODE_ENV: production
run: npm run build

GitHubが提供する仮想マシン:

ラベルOS
ubuntu-latestUbuntu 22.04
ubuntu-22.04Ubuntu 22.04
ubuntu-20.04Ubuntu 20.04
windows-latestWindows Server 2022
macos-latestmacOS 14 (Sonoma)
macos-13macOS 13 (Ventura)

自分のサーバーでジョブを実行:

jobs:
build:
runs-on: self-hosted
# または
runs-on: [self-hosted, linux, x64]

マーケットプレイスのアクション

Section titled “マーケットプレイスのアクション”
# コードのチェックアウト
- uses: actions/checkout@v4
# Node.js のセットアップ
- uses: actions/setup-node@v4
with:
node-version: '20'
# Python のセットアップ
- uses: actions/setup-python@v5
with:
python-version: '3.12'
# キャッシュ
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
# アーティファクトのアップロード
- uses: actions/upload-artifact@v4
with:
name: build
path: dist/
# バージョン指定(推奨)
- uses: actions/checkout@v4
# コミットハッシュ(最も安全)
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# ブランチ(非推奨)
- uses: actions/checkout@main
# ローカルアクション
- uses: ./.github/actions/my-action
env:
# ワークフローレベル
NODE_ENV: production
jobs:
build:
env:
# ジョブレベル
CI: true
steps:
- name: Build
env:
# ステップレベル
API_URL: https://api.example.com
run: npm run build

Settings → Secrets and variables → Actions:

steps:
- name: Deploy
env:
API_KEY: ${{ secrets.API_KEY }}
run: ./deploy.sh
- run: |
echo "Repository: ${{ github.repository }}"
echo "Branch: ${{ github.ref_name }}"
echo "SHA: ${{ github.sha }}"
echo "Actor: ${{ github.actor }}"
echo "Event: ${{ github.event_name }}"

steps:
- name: Only on main
if: github.ref == 'refs/heads/main'
run: echo "Main branch"
- name: Only on PR
if: github.event_name == 'pull_request'
run: echo "Pull request"
- name: Skip on failure
if: success()
run: echo "Previous step succeeded"
- name: Always run
if: always()
run: echo "This always runs"
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node: [18, 20, 22]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm test
Terminal window
# 実行一覧
gh run list
# 実行詳細
gh run view 123456
# ログを見る
gh run view 123456 --log
# 再実行
gh run rerun 123456

要素説明
WorkflowYAMLで定義する自動化プロセス
Eventワークフローを起動するトリガー
Jobステップの集合
Step個々のタスク
Action再利用可能な処理
Runner実行環境
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm test
- run: npm run build

次の章では、GitHub Actions応用について学びます。

Q1. GitHub Actionsのワークフローファイルを配置するディレクトリはどれですか?

Q2. pushイベントでワークフローをトリガーする設定として正しいものはどれですか?

Q3. GitHub-hostedランナーとself-hostedランナーの違いは何ですか?