🐺

GitHub Actions による npm パッケージのリリース自動化

に公開

はじめに

複数人でnpmパッケージのバージョン管理を行う際、手動でのリリース作業は運用上のリスクが高く、バージョンの不整合やリリース手順の漏れといった問題が発生しやすい状況となります。

今回は、GitHub
Actionsを活用してnpmパッケージの公開プロセスを自動化し、リリース作業の安定化を図りました。

全体像

workflow

リリースフローは以下の通りです。

  1. Workflow Dispatch イベントで GitHub Actions がトリガー
  2. npm version コマンドでバージョン番号を更新
  3. npm publish コマンドでパッケージを公開
  4. gh release create コマンドでリリースノートの自動生成

実装の背景と課題

解決したい課題

複数人でのnpmパッケージ開発において、以下の課題が発生していました。

  • 手動でのnpm publishによるヒューマンエラー
  • バージョン番号の管理ミス
  • リリース手順の属人化
  • テスト実行忘れによる不具合のあるパッケージの公開

これらの課題を解決するために、GitHub
Actionsを活用した自動化の実装に取り組みました。

開発プロセス

GitHub Actions設定ファイルの作成

以下のようなGitHub Actionsの設定ファイルを作成しました。

.github/workflows/publish.yml

name: Publish to npm

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version type"
        required: true
        default: "patch"
        type: choice
        options:
          - patch
          - minor
          - major

jobs:
  create-tag:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      version: ${{ steps.version.outputs.version }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: "main"

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "latest"

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: latest

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Configure git
        run: |
          git config user.name "${{ github.actor }}"
          git config user.email "${{ github.actor }}@users.noreply.github.com"

      - name: Update version and create tag
        run: |
          pnpm version ${{ github.event.inputs.version }}
          git push origin main --tags
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Get new version and set output
        id: version
        run: |
          NEW_VERSION=$(node -p "require('./package.json').version")
          echo "version=v$NEW_VERSION" >> $GITHUB_OUTPUT
          echo "✅ Version updated to v$NEW_VERSION and tag created successfully by ${{ github.actor }}"
          echo "This will trigger the publish workflow automatically"

  publish:
    runs-on: ubuntu-latest
    needs: create-tag
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: "main"

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "latest"
          registry-url: "https://registry.npmjs.org"

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: latest

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run tests
        run: pnpm test

      - name: Build
        run: pnpm build

      - name: Publish to npm
        run: pnpm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  create-release:
    runs-on: ubuntu-latest
    needs: [create-tag, publish]
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - name: Create GitHub Release
        run: gh release create ${{ needs.create-tag.outputs.version }} --generate-notes
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

NPM_TOKENの設定

npmへの公開権限を持つトークンをGitHubのSecretsに設定します。

※automation用のトークンを作成し設定しています。

検討ポイント

自動化の範囲

今回の実装では、以下の範囲で自動化を行いました。

  • npmバージョン管理とタグ管理
  • npmパッケージの公開
  • リリースノートの自動生成

基本的にはnpmで提供されているコマンドで自動化できる部分を中心に実装しています。
gh release createコマンドはリリースノートを生成するコマンドなのでありがたく利用しています。

参考リンク

Discussion