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

More than 1 year has passed since last update.

【GitHubAction】mainへのPRをdevelopからしか通さないように設定してみた

Last updated at Posted at 2024-03-21

はじめに

本記事ではbranchの保護ルールと、github actionを使用し、
mainブランチへのPRをdevelopブランチからのみ許可する方法を記載します。

前提

リポジトリに以下3つのブランチがあることを想定しています。

  • main
  • develop(mainから切る)
  • feature(developから切る)

branchの保護ルールの設定

◆Branch name patternについて

マージ先のブランチ(main)を設定します。

image.png

◆Protect matching branchesについて

Require status checks to pass before mergingをチェックすると、
下に検索バーが表示されるのでここにjob名を入力し選択します。

image.png

job名とは?
GitHubActionで実行するyamlファイルに記載している下記画像部分の名称の事です。
image.png

Do not allow bypassing the above settingsにチェックを入れることでmainブランチへの直pushを禁止しておきます。

image.png

・参考:https://zenn.dev/json_hardcoder/articles/f9b534377103a4

YAMLファイルの設定

以下の設定では、developブランチ以外からmainブランチへのPRが作成された際に、
ステータスのチェックを通さないように設定しています。

main.yaml
name: CI

on:
  pull_request:
    types: [opened, reopened]
    branches:
      - main

jobs:
  cicd_test:
    name: cicd_to_main
    runs-on: ubuntu-latest
    steps:
      - name: Check:if PR is from develop to main branch
        run: |
          if [ "${{ github.event.pull_request.base.ref }}" == "main" ] && [ "${{ github.event.pull_request.head.ref }}" != "develop" ]; then
            echo "Pull request not from develop to main branch"
            exit 1
          else
            echo "Pull request from develop to main branch"
          fi

実行

◆ feature⇒mainへのPRを作成してみる

featureブランチからmainブランチへのPRを作成してみると、下記画像のようにPRのステータスチェックが通らない事を確認できました。

image.png

◆ develop⇒mainへのPRを作成してみる

developブランチからmainブランチへのPRを作成してみると、下記画像のようにPRのステータスチェックが通る事を確認できました。

image.png

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