LoginSignup
0
0

master(main)へのpushを禁止するテンプレートを作ろう

Last updated at Posted at 2023-12-03

はじめに

gitを通ぶってジットと呼びます。嘘です。にわのわです。

masterへ直pushし、もろもろのgithub actionsが走り、
runfailedメールを受け取ることがまれによくあります。
githubにはmasterへの直pushを禁止する機能がありますが、
毎度設定するのはしんどいです。
ですのでテンプレート化しましよう。

テンプレートリポジトリを作成する

githubには便利なことにテンプレートリポジトリという機能があります。
https://docs.github.com/ja/repositories/creating-and-managing-repositories/creating-a-template-repository

ということでトップページからぽちぽち作成。
image.png

ついでなのでMITライセンスをデフォルトにするよ。
image.png

settings > General
Template repositoryにチェックを入れる。
githubsettings.png

直push禁止設定を入れる

上の方でリンクで紹介したこれはテンプレートに反映されないよ

ということで、.githooks/pre-pushに設定を入れる。

#!/bin/bash

# pushを禁止するブランチ
readonly MASTER='main'
readonly RELEASE='^.*release.*$'  # 「release」文字列を含む全てのブランチ

while read local_ref local_sha1 remote_ref remote_sha1
do
  if [[ "${remote_ref##refs/heads/}" = $MASTER ]]; then
    echo -e "\033[0;32mDo not push to\033[m\033[1;34m master\033[m \033[0;32mbranch\033[m"
    exit 1
  fi
  if [[ "${remote_ref##refs/heads/}" =~ $RELEASE ]]; then
    echo -e "\033[0;32mDo not push to\033[m\033[1;34m release\033[m \033[0;32mbranch\033[m"
    exit 1
  fi
done

これを実行して完成 本当はcloneするだけ、templateを選択するだけで直pushを禁止したかった

git config --local core.hooksPath .githooks

ok
image.png

参考まるパクリ

おわりに

.github/hooks/pre-push でよしなに
or
.git/hooksだけバージョン管理できるようにとかできないんですかね?

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