2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

bitbucket pipeline でリリース時にgit tagを自動で打つ

2
Last updated at Posted at 2025-12-22

はじめに

本記事はLocation Tech Company's Advent Calendar 2025
の22日目の記事として作成されたものです。


株式会社unerry でデータエンジニアをしている小野です。

突然ですが、みなさんBitbucket Pipelinesを使っていますか?
GitHub Actions と比べて、実現できることが少ないイメージはないでしょうか?

弊社では、多くのリポジトリがbitbucket にあります。
また、私が関わっているプロジェクトでは、セマンティックバージョニングのgit tag で、バージョンの管理をしています。

今回は、bitbucket でtagpr のように、リリース時の自動タグ付けを実現する方法について書きます。

必要なもの

  • bitbucket pipelines
  • bitbucket-pipelines.yml
  • version.txt

開発フロー

  • mainブランチへと各チケットのブランチをマージしていく手法をとっています
  • リリースは週次で行います

要件定義

次のように整理しました

  • 新しく打つtag の名前は人間が決める
  • version.txt の変更をキャッチする
  • リリース時は必ずveersion.txt を更新するようにする

実際のコード

bitbucket-pipelines.ymlに記載します

  1. 現在のリポジトリのgit tagを取得する
  2. version.txtの中身が現在のgit tagと差分がないか確認する
  3. 差分があった場合はgit tagをうつ
    - step: &add-git-tag
        name: Check and add git tag versions for CI
        script:
          # fatal: detected dubious ownership in repository のエラーを回避
          - git config --global --add safe.directory /opt/atlassian/pipelines/agent/build
          # 現在のgit tagを取得
          - git for-each-ref --sort=-authordate --format='%(refname:short)' refs/tags | head -n 1 > /tmp/version.txt
          # 新しいversionと比較
          - diff version.txt /tmp/version.txt && status=0 || status=$?
          - >-
            if [[ "${status}" -eq 0 ]]; then
              echo -e "\033[31mError: Same Version\033[m"
            elif [[ "${status}" -eq 1 ]]; then
              git tag $(cat version.txt)
              git push --tags
            fi

この条件をmain ブランチへのマージ時の条件 に書いておくことで、version.txtの更新pr がマージされると、マージコミットに対してgit tagが付与されます。

pipelines:
  branches:
    "main":
    - stage:
        name: CD
        steps:
          - step: *add-git-tag

※補足
git tag のversion 比較について
本来はgit tag -l --sort -refname | head -n 1 > /tmp/version.txt のようにgit のオプションを使って書きたいのですが、git tag --sort がgit のv2.x系 でしか使えず断念しました。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?