My Note

自己理解のためのブログ

tagprでPRベースでタグを管理する

はじめに

githubでタグでバージョン管理するときにtag打ちとリリースノートの作成を手動で行うのは面倒だと思っていました。 tagprというOSSがリリースされてtag打ちとリリースノートをworkflowで自動で行なってくれるので今回使ってみました。

github.com

設定

  • .github/workflows/tagpr.yml
name: tagpr
on:
  push:
    branches:
      - main
jobs:
  tagpr:
    runs-on: ubuntu-latest
    timeout-minutes: 3
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      
      - name: Tagpr
        uses: Songmu/tagpr@main
        id: tagpr
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  • .github/release.yml
    • リリースノートの設定について設定します。
    • PRに tagpr をタグを付与したものは除外する設定を追加
changelog:
  exclude:
    labels:
      - tagpr
# config file for the tagpr in git config format
# The tagpr generates the initial configuration, which you can rewrite to suit your environment.
# CONFIGURATIONS:
#   tagpr.releaseBranch
#       Generally, it is "main." It is the branch for releases. The tagpr tracks this branch,
#       creates or updates a pull request as a release candidate, or tags when they are merged.
#
#   tagpr.versionFile
#       A versioning file containing the semantic version needed to be updated at release.
#       It will be synchronized with the "git tag".
#       Often this is a meta-information file such as gemspec, setup.cfg, package.json, etc.
#       Sometimes the source code file, such as version.go or Bar.pm, is used.
#       If you do not want to use versioning files but only git tags, specify the "-" string here.
#
#   tagpr.vPrefix
#       Flag whether or not v-prefix is added to semver when git tagging. (e.g. v1.2.3 if true)
[tagpr]
    vPrefix = true
    releaseBranch = main
    versionFile = -

実際に利用してみる

  • tag: v0.0.3 の状態で#3 のPRをmainブランチに対してマージするとworkflowが走る
  • PR Release for v0.0.4 #4 が作成される(下記画像)

  • この状態で #5 のPRをmainブランチに対してマージするとworkflowが走り Release for v0.0.4 #4Whats Changed に追加される

このように、tagprのworkflowで指定しているブランチに対してPRを作成していき任意のタイミングでtagをきるさいにこの Release for v0.0.4 #4 のPRをマージする

  • この状態で Release for v0.0.4 #4 のPRをマージします。そうるすと、workflowが走りtagとreleaseノートが発行される

特にタグを指定しないとパッチバージョンがインクリメントされます。 ここで、マイナーバージョンを上げるためには tagpr:minor をラベルに付与します。メジャーバージョンを上げるには同様に tagpr:major をラベルに付与します。

ref) GitHub - Songmu/tagpr: automatically creates and updates a pull request for unreleased items, tag them when they are merged, and create releases. 今回は試しに、マイナーバージョンを上げるために tagpr:minor のラベルを Release for v0.0.5 に付与してPRをマージしました。↓

  • PRに tagpr:minor ラベルを付与

  • マイナーバージョンがアップデートされた (v0.0.4 --> v0.1.0)

tag発行をトリガーとして利用する

tagprで作成されたPRをマージするとworkflowが実行されtagが打たれてリリースされる。 このタイミングで何かしらアクション、(例えばデプロイ)などする場合に下記のようにして 任意の処理を実行することも可能です。

  • .github/workflows/tagpr.yml
name: tagpr
on:
  push:
    branches:
      - main

jobs:
  tagpr:
    runs-on: ubuntu-latest
    timeout-minutes: 3
    outputs:
      tagpr-tag: ${{ steps.exec-tagpr.outputs.tag }}
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Check out source code
        uses: actions/checkout@v3

      - name: Exec-tagpr
        id: exec-tagpr
        uses: Songmu/tagpr@v1

  deploy_something:
    runs-on: ubuntu-latest
    timeout-minutes: 3
    needs: tagpr
    env:
      TAG_VERSION: ${{ needs.tagpr.outputs.tagpr-tag }}

    steps:
      - name: Show tag
        if: needs.tagpr.outputs.tagpr-tag != ''
        run: |
          echo ${{ env.TAG_VERSION }}

さいごに

今回、githubのタグを管理する方法としてtagprを検証しました。PRベースでタグとリリースノートが自動で作成でき、バージョンアップのメジャー、マイナー、パッチも PRにタグを付与することで設定できたのでとても便利でした。個人開発のリポジトリに導入して利用していこうと思います。