★ 初級

12. コミット規約(Conventional Commits)

「型 (スコープ): 概要」 という 1 行ルール。これだけで、CHANGELOG 自動生成、リリースバージョン自動決定、PR の意図共有が一気に整います。

Conventional Commits とは

conventionalcommits.org で定義されている、コミットメッセージの軽量フォーマット仕様。

<type>(<scope>): <subject>

[body]

[footer]

例:

feat(vpc): add IPv6 support to public subnets
fix(rds): correct backup retention period for prd
chore(deps): bump aws provider to 5.84.0
docs(readme): update apply instructions for windows

主な type

type意味semver 影響
feat新機能追加MINOR
fixバグ修正PATCH
chore雑務(依存更新、CI 設定等)
docsドキュメントのみ
refactor動作不変のリファクタ
testテスト追加・修正
style整形のみ(fmt 等)
perf性能改善PATCH
ciCI 設定の変更
buildビルド設定の変更

スコープ

変更が及ぶ範囲を カッコ内 に書く。任意だが付けると圧倒的に読みやすい。

feat(vpc): add IPv6 support
fix(iam): tighten S3 read policy
chore(deps): bump aws provider
ci(actions): cache .terraform between runs
refactor(modules/network): split subnet logic

IaC リポジトリの典型的なスコープ: vpc / iam / rds / ecs / lambda / cdn / monitoring / 各 envs / 各 module 名。

BREAKING CHANGE

後方互換のない変更を示す方法は 2 つ:

① type の後ろに !

feat(vpc)!: rename cidr_block to cidr

② footer に BREAKING CHANGE

feat(vpc): rename cidr_block to cidr

cidr_block was confusing because it referred to VPC range only.

BREAKING CHANGE: variable "cidr_block" has been renamed to "cidr".
Migration: update calling code from cidr_block = ... to cidr = ...

これらを認識して semantic-release / release-please は MAJOR バンプを判定します。

IaC での例

# 新リソース追加
feat(s3): add logs bucket for CloudFront access logs
feat(rds): provision read replica in stg

# 変更
fix(sg): close 0.0.0.0/0 on port 22 for prd web
fix(asg): correct health check grace period

# 設定変更
chore(provider): pin aws to ~> 5.84
chore(deps): bump terraform to 1.14
ci: add tflint to PR workflow

# モジュール
feat(modules/network)!: drop nat_gateway_count, derive from subnet count

BREAKING CHANGE: nat_gateway_count is removed. Now derived automatically.

強制する仕組み

commitlint(自動チェック)

# package.json
npm i --save-dev @commitlint/cli @commitlint/config-conventional

# commitlint.config.js
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

GitHub Actions で PR タイトルチェック

name: Lint PR
on:
  pull_request:
    types: [opened, edited, synchronize]

permissions:
  pull-requests: read

jobs:
  pr-title:
    runs-on: ubuntu-latest
    steps:
      - uses: amannn/action-semantic-pull-request@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          types: |
            feat
            fix
            chore
            docs
            refactor
            test
            ci
            build
            perf
            style

GitHub の merge を「Squash and merge」に統一し、PR タイトルがそのまま 1 コミットメッセージになる 設定にすると、PR タイトルだけ守れば履歴が綺麗。

CHANGELOG / 自動リリース

Conventional Commits を採用すると、tag・CHANGELOG・GitHub Releases の生成を自動化できます。

release-please

# .github/workflows/release.yml
name: Release
on:
  push:
    branches: [main]

permissions:
  contents: write
  pull-requests: write

jobs:
  release-please:
    runs-on: ubuntu-latest
    steps:
      - uses: googleapis/release-please-action@v4
        with:
          release-type: terraform-module

これだけで、main へのコミットを集計し、「次のリリース PR」を自動生成・更新。マージすると tag + Release が打たれ、CHANGELOG.md も自動更新されます。

最低限のスタートライン type 7 種(feat / fix / chore / docs / refactor / test / ci)だけ覚えれば 95% カバー。「PR タイトルを Conventional Commits にする」 ところから始めて、慣れてきたら commitlint や release-please へ。