CI\CD/GitHub

[GitHub] Branch Protection Rule 적용해 브랜치 보호하기

Branch를 보호하는 것이 중요한 이유

협업을 할 때 Rule이 없으면 각자 자신의 방식으로 일을 하게 되기 때문에 뒤죽박죽이 된다. 만약 인원이 적다면 별 문제가 되지 않지만, 실무에서는 적게는 3명 많게는 수십 수백명이 하나의 저장소를 관리하기 때문에 저장소를 관리하기 위한 Rule이 중요하다. 특히 Git의 Branch는 협업을 위한 기본 토대이기 때문에 최소한의 규칙을 정해야 협업 시의 혼란을 방지할 수 있다. 

 

 

GitHub의 Branch Protection Rules

GitHub에서는 GitHub에 올라간 Branch들에 대한 Rule을 지정할 수 있게 해준다. 이 Rule을 이용하면 특정 브랜치가 실수로 지워지는 것을 방지하거나 PR(Pull Request)가 아닌 다른 방식으로 커밋을 추가하는 것을 막아 코드 리뷰를 강제하는 등의 작업을 할 수 있다. 주의할 점은 GitHub에 올라간 브랜치들만 이 Rule의 적용을 받는다. 로컬에서는 아무렇게나 작업해도 제약이 없다.

 

그림1. GitHub Branch Protection Rules

 

Git Branch Protection Rule은 [그림2]의 사항들로 설정할 수 있다. 먼저 Protection Rule이 적용될 Branch의 패턴을 만든 후 해당 패턴의 브랜치에 대해 8가지 Protection Rule을 설정할 수 있다.

 

그림2. Protection Rule 구성

 

자 이제 어떻게 Branch에 Protection Rule을 추가할 수 있는지 알아보자.

 

Branch name pattern

특정한 브랜치나 특정한 패턴을 가진 브랜치(접두어가 feature인 브랜치 등)에 대해 아래서 다룰 Branch Protection Rule이 적용될 수 있도록 한다. 

 

브랜치 하나에 적용 : [branch name] 
예) main

특정 경로의 모든 브랜치에 적용 원하는 단어 후 *을 붙이면 됨
예) feature* : feature 접두어를 가진 모든 브랜치에 적용 , feature/* :  feature/ 접두어를 가진 모든 브랜치에 적용

 

 

Require a pull request before merging

When enabled, all commits must be made to a non-protected branch and submitted via a pull request before they can be merged into a branch that matches this rule.

해당 브랜치에 커밋을 추가하기 위해 로컬에서 Direct Push가 안되고, 별도 브랜치를 만들어 Pull Request를 해야되는 것을 설정하는 값이다. 협업 시 브랜치를 Local에서의 Direct Push로부터 보호하고 코드리뷰를 강제하기 위해 사용한다.

 

 

Require status checks to pass before merging

Choose which status checks must pass before branches can be merged into a branch that matches this rule. When enabled, commits must first be pushed to another branch, then merged or pushed directly to a branch that matches this rule after status checks have passed.

테스트 결과 이상이 없을 시에만 Merge 가능 하도록 한다. 

 

 

Require conversation resolution before merging

When enabled, all conversations on code must be resolved before a pull request can be merged into a branch that matches this rule. Learn more.

Pull Request를 날리게 되면 코드리뷰를 통해 Conversation이 생기게 되는데, 이 Conversation이 모두 solved 되었을 때만 머지할 수 있도록 한다.

 

 

Require signed commits

Commits pushed to matching branches must have verified signatures.

커밋들이 sign이 되어있어야 한다. 보통 GitHub에 GPG Key를 등록하고 해당 Key를 가진 사람이 커밋한 경우 Verified라고 뜨게 된다.

 

 

 

Require linear history

Prevent merge commits from being pushed to matching branches.

해당 브랜치에서 분기를 불가능하게 만든다. 히스토리 추적을 쉽게하기 위함이며, 하나의 브랜치로 유지해야 할 필요가 있을 때 사용한다.

 

 

Include administrators

Enforce all configured restrictions above for administrators.

관리자에게도 위의 5가지 Require a pull request before merging, Require status checks to pass before merging, Require conversation resolution before merging, Require signed commits, Include administrators Rule 중 선택한 Rule이 적용되도록 한다.

 

 

Allow force pushes

Permit force pushes for all users with push access.

Force Push (강제 푸시) 를 허용할 것인지 여부를 결정한다. Force Push란 Git의 커밋들은 현재 브랜치의 커밋들로 모두 대체하는 푸시로 절대로 사용하면 안된다!

 

 

Allow deletions

Allow users with push access to delete matching branches.

push access를 가진 유저들이 브랜치를 지울 수 있도록 한다. 푸시는 Write를 위한 명령어인데 이 명령어에 대한 유저에게 삭제 명령을 가능하도록 만드는 옵션이다. 왠만하면 사용하지 말자

반응형

 

이 글의 저작권은 '조세영의 Kotlin World' 에 있습니다. 글, 이미지 무단 재배포 및 변경을 금지합니다.

 

 

Kotlin, Android, Spring 사용자 오픈 카톡

오셔서 궁금한 점을 질문해보세요!
비밀번호 : kotlin22

open.kakao.com