LoginSignup
7
2

More than 5 years have passed since last update.

Bitbucket Pipelines を使ってリポジトリのマージ済みブランチを定期削除

Last updated at Posted at 2018-03-27

前提

Bitbucket Pipelines とは

Bitbucket 向けの CI/CD のための機能。

リポジトリへのコードの push などをトリガとして、docker コンテナ上でスクリプトを実行できる。

マージ済みリモートブランチを一括削除するコマンド

git branch -r --merged origin/master | grep -v master | grep -v release | sed -e "s% *origin/%%" | xargs --no-run-if-empty -Ibranch git push origin :branch

grep -v のところは各プロジェクトに合わせて、削除したくないブランチを指定すること

参考:Gitでリモートのマージ済みのブランチを一括削除する@fukayatsu

設定手順

Pipelines 用の SSH 鍵を設定

リポジトリのページの 設定 > PIPELINES > SSH Keys から秘密鍵・公開鍵のペアを登録。

add-ssh-keys.png

「Generate keys」で作成した鍵ペアの公開鍵をチームに登録できればベスト。

それができなければ自分のアカウントに登録する(自分のアカウントでの操作になるので注意)。

Pipeline を登録

アプリケーションルートに bitbucket-piepelines.yml というファイルを以下の内容で作成(すでに存在する場合は編集して設定を追加)。

bitbucket-pipelines.yml
pipelines:
  custom:
    prune-branches: # 自由に設定可(表示用の名前)
      - step:
          image: alpine/git
          script:
            - git fetch
            - git branch -r --merged origin/master | grep -v master | grep -v release | sed -e "s% *origin/%%" | xargs --no-run-if-empty -Ibranch git push origin :branch

ここまでで、手動実行ができるようになる。

ブランチの「Run pipeline for a branch」またはコミットの「Run pipeline」からをクリックすると以下のようなモーダルウィンドウが開くので、「Run」をクリックすると Pipeline が実行開始される。

run-pipeline.png

pipeline-result.png

Pipeline 実行スケジュール設定

下記ページ(公式ドキュメント)の「2. Create a schedule」に従って設定。

リポジトリのページの「Pipelines」よりSchedules > New scheduleを選択。

image.png

ブランチ(※)と、先程登録した Pipeline、実行時間を選択して「Create」をクリックすれば登録完了。

※今回の Pipeline は実行内容がブランチに依存しないので、どれでもよい。

image.png

登録済みのものは画面上で確認することができる。

image.png

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