滅入るんるん

何か書きます

バージョン管理に便利なGitHub Actionを作りました

タイトルの通りに便利なGitHub Actionを作りました。個人的用途ではGitHub Actionを作るために作ったのですがそれ以外の用途でも通用するはずです

要件としては

  • 最新のReleaseからバージョンを推測し、CommitやPullRequestから次のバージョンを決定
  • CommitやPullRequestからRelease noteを作成し、それを載せたReleaseを公開
  • メジャーバージョンやマイナーバージョンのブランチを自動作成し該当のブランチにPush
  • ファイルに書かれたバージョンの書き換え

という感じにバージョン管理で必要になる処理を一通りするActionを作りました*1

実際に作ったものはこれ↓

github.com

細かい設定方法などはリポジトリーを見てもらった方がわかりやすいので割愛して、自分のユースケースを紹介します

個人的ユースケース(GitHub Action作成の場合)

GitHub Actionを作成し、Marketplaceに公開するには必ずReleaseを1度作成する必要があります。Actionのバージョン管理はいろいろな方法がありますが、最近の自分の取り組みとしてはvX.X.Xのようにsemverでバージョン付けしたものをReleaseに公開しvXブランチもついでに作成しています。これによって利用者はuses: MeilCli/bump-release-action@v1.0.5のように個別のバージョンを指定して利用することやuses: MeilCli/bump-release-action@v1のようにそのメジャーバージョンの最新のバージョンを指定して利用することができるようになります

このワークフローを実現するために自分は以下のようにGitHub Actionを用意しています

# .github/workflows/release.yml
name: Release

on:
  workflow_dispatch:
    inputs:
      bump:
        description: 'bump type, major or minor or patch or empty string'
        default: ''
      dry_run:
        description: 'dry run, true or false'
        default: 'false'
      draft:
        description: 'draft, true or false'
        default: 'false'
      pre_release:
        description: 'pre release, true or false'
        default: 'false'

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: npm install
      - run: npm run build
      - run: npm run test
      - uses: MeilCli/bump-release-action@master # ここはv1とかに置き換えたほうがいいです
        with:
          config_path: '.github/bump.yml'
          bump: ${{ github.event.inputs.bump }}
          dry_run: ${{ github.event.inputs.dry_run }}
          draft: ${{ github.event.inputs.draft }}
          pre_release: ${{ github.event.inputs.pre_release }}
# .github/bump.yml
release:
  title-prefix: 'v'
  initial-version: '0.0.1'
  tag-prefix: 'v'
  commit-note-replacers:
    - replace-prefix: 'breaking: '
      new-prefix: ''
    - replace-prefix: 'feature: '
      new-prefix: ''
    - replace-prefix: 'change: '
      new-prefix: ''
    - replace-prefix: 'fix: '
      new-prefix: ''
    - replace-prefix: 'document: '
      new-prefix: ''
    - replace-prefix: 'dependency: '
      new-prefix: ''
branch:
  version-branch-prefix: 'v'
categories:
  - title: 'Breaking Changes!'
    labels:
      - 'BreakingChange'
    commits:
      - 'breaking:'
    changes-prefix: ':warning: '
  - title: 'Changes'
    labels:
      - 'Feature'
    commits:
      - 'feature:'
    changes-prefix: ':gift: '
  - title: 'Changes'
    labels:
      - 'Maintenance'
    commits:
      - 'change:'
    changes-prefix: ':hammer: '
  - title: 'Bug Fixes'
    labels:
      - 'Bug'
    commits:
      - 'fix:'
    changes-prefix: ':ambulance: '
  - title: 'Changes'
    labels:
      - 'Documentation'
    commits:
      - 'document:'
    changes-prefix: ':blue_book: '
  - title: 'Dependency Updates'
    labels:
      - 'Dependencies'
    skip-label: 'Development'
    commits:
      - 'dependency:'
    changes-prefix: ':green_book: '
bump:
  default: 'patch'
  major:
    labels:
      - 'BreakingChange'
    commits:
      - 'breaking:'
  minor:
    labels:
      - 'Feature'
    commits:
      - 'feature:'

少し長いですが解説するとbump-release-actionではCommit MessageとPullRequest Labelからリリースノートの作成や次のバージョンを決定します

たとえばbreaking:から始まるcommitを作るとリリースノートではBreaking Changes!というカテゴリーに載せられ次のバージョンアップはメジャーバージョンアップになり、feature:から始まるcommitを作るとリリースノートではChangesというカテゴリーに載せられ次のバージョンアップはマイナーバージョンアップになります。自分が使っている設定が長いのは破壊的変更から微小な変更やドキュメント・依存関係の変更までの場合を網羅したものにしているためです

PullRequestをマージしたりCommitをpushしたりなどしてバージョンアップリリースをしたくなったらGitHub ActionのところからRelease CIをマニュアル実行すればいいようにしています。運用によってはこれを毎週定期的に実行するということも考えれますね

このbump-release-action自体もbump-release-actionでリリースしているため、GitHubのReleaseを見るとリリースノートによってどういった変更がされたかがわかるようになっています

github.com

*1:これらの個々のことをするActionやライブラリーはいろいろな人が作っていますが自分のこの要件すべてを満たすものは探しても見当たらなかったです