0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

github actionを使って自動でリリースタグをつけたい

Posted at

gitのリリースブランチにpushした時に、自動でversion付きのTagをつけれると便利なのでやってみた

バージョン管理の番号の手入力をやめたい

バージョン番号を手入力していると、少し変更するだけでも1回1回バージョン付け直さないといけないし、複数人だとさらにごちゃるので、
基本的にはリリースブランチにpushされたら自動で生成されるようにするルールでやってみることにした

作成したgithub actionのworkflow

VERSIONを読み込んで、v0.0.1だったら、v0.0.2にするgithub action

name: Create Release Tag

on:
  push:
    branches:
      - release

jobs:
  create-release-tag:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Read current version
        id: current_version
        run: echo "CURRENT_VERSION=$(cat VERSION)" >> "$GITHUB_OUTPUT"
      
      - name: Increment version
        id: increment_version
        run: |
          current_version=${{ steps.current_version.outputs.CURRENT_VERSION }}
          IFS='.' read -r -a version_parts <<< "${current_version#v}"
          major=${version_parts[0]}
          minor=${version_parts[1]}
          patch=${version_parts[2]}
          new_patch=$((patch + 1))
          new_version="v$major.$minor.$new_patch"
          echo "NEW_VERSION=${new_version}" >> "$GITHUB_OUTPUT"
          echo $new_version > VERSION

      - name: Commit and push updated VERSION
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add VERSION
          git commit -m "Increment version to ${{ steps.increment_version.outputs.NEW_VERSION }}"
          git push origin HEAD:release

      - name: Create and push the tag
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          new_version=${{ steps.increment_version.outputs.NEW_VERSION }}
          git tag $new_version
          git push origin $new_version

解説

バージョン番号は v0.0.1のような形式になっていて、releaseブランチにpushされると、
最後の桁(マイナーバージョン)に+1してTAG付けをします。
メジャーバージョンを上げたい場合はレポジトリのVERSIONのテキストを手で編集していきます。

TAGをつけることによってうれしいこと

コードを取得する際にTAG指定でfetchすればいいので、後々の管理が楽なのと、デプロイ先のDockerでもインスタンスでも
tagを指定して開発、検証、本番環境のような切り分けができるようになってよかった

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?