0
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.

GitHub Actionsで実行しているテストが通らないとマージできないようにするには、ブランチ保護の設定でステータスに`job_id`を入力する

Last updated at Posted at 2024-07-06

環境

  • Python 3.11

はじめに

TravisCIでPythonコードのテストとリントを実行していましたが、諸々の理由でGitHub Actionsへ移行することになりました。

TravisCIの設定ファイル

.travis.yml
dist: xenial
language: python
python:
  - "3.11"
  - "3.12"
install:
  - pip install poetry
  - travis_retry poetry install
script:
   - make lint
   - make test
branches:
  only:
  - main

GitHub Actionsの設定ファイル

設定ファイルは"Python application"というテンプレートを利用しました。
https://github.com/actions/starter-workflows/blob/main/ci/python-app.yml

https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python を参考にしました。

.github/workflows/lint-test.yml
name: Python application

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

permissions:
  contents: read

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: ["3.11", "3.12"]

    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip "poetry<1.9"
          poetry install
      - name: Lint
        run: |
          make lint
      - name: Test
        run: |
          make test

Makefile

Makefile
ifndef SOURCE_FILES
	export SOURCE_FILES:=src
endif
ifndef TEST_FILES
	export TEST_FILES:=tests
endif

.PHONY: lint format test

format:
	poetry run ruff format ${SOURCE_FILES} ${TEST_FILES}
	poetry run ruff check ${SOURCE_FILES} ${TEST_FILES} --fix-only --exit-zero
	
lint:
	poetry run ruff check ${SOURCE_FILES} ${TEST_FILES}
	poetry run mypy ${SOURCE_FILES} ${TEST_FILES}

test:
	poetry run pytest tests

Pull Requestのマージをブロック

GitHub Actionsでのリントとテストが成功しないと、マージできないようにしたいです。
以下の手順で対応しました。

  1. "Sttings"タブから"Branch protection rule"を設定する画面に移動する
  2. "Require status checks to pass before merging"チェックボックスをONにする
  3. ステータスに"build"を入力する。検索候補である"build (3.11)"と"build (3.12)"を選択する
    • "build"は、GitHub Actionsの設定ファイルに記載されているjobs配下のbuildというjob_idに対応する

image.png

ちなみに、TravisCIの場合は、ステータスに"travis"と入力して"Travis CI - Branch"と"Travis CI - Pull Request"を選択していました。
Github Actionsに移行した際、どのようなスタータスが候補になるのか分からなくて、少しハマりました。

image.png

所感

  • ステータスは、最初から選択できる候補をすべて表示して欲しかった。そうすれば、今回ハマることはなかったと思う。
  • GitHub Actionsの場合、matrixに指定したPythonのバージョンごとにステータスを選択できる。便利。

参考にしたサイト

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?