【github action】lintのflake8を使ってみた【CICD】

 

 

github actionの勉強中

ついに、CIをやってみる時がきた

github actionのCIというのは、プルリクエストを出した時に自動でソースをチェックしてくれる機能の事

CIの中のリントというのをやってみる

lintには言語ごとにやり方があるが、今回はpythonでテストしてみる

まずは、わざとエラーありのpythonファイルを作成

 

lint_test.py

print 'lint test'

printがエラーがあります。

これをymlファイルで、github actionでチェックさせます。

 

hoge.yml

name: lintのテスト


on: [pull_request]

jobs:

lint_python:

runs-on: ubuntu-latest

steps:

- name: コードをチェックアウト

uses: actions/checkout@v4


- name: Python をセットアップ

uses: actions/setup-python@v5

with:

python-version: "3.12"


- name: lint ツールをインストール

run: pip install flake8


- name: flake8 で構文チェック

run: flake8

 

説明はあとで、これでpushしてプルリクを出してみると

 

 

All checks have failed


1 failing check

failing checks


lintのテスト / lint_python (pull_request)

lintのテスト / lint_python (pull_request)Failing after 8s

 

と、プルリクが自動でエラーとなりました。

pythonにエラーがあるので、プルリクが自動で否認されています。

 

github action側のエラーを見てみると

./python/lint_test.py:1:2: E999 SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

 

とこなエラーが出ています。

printに()が必要じゃないの???

とエラー吐かれています。

 

こういう風に、github actionのCIは自動でソースをチェックさせられるので、つまらないミスがあるファイルのマージを防ぐ事が出来ます!!

めちゃくちゃ便利

●ソースの説明

 

もう一度ソースを見てみます。

 

name: lintのテスト

on: [pull_request]

jobs:

lint_python:

runs-on: ubuntu-latest

steps:

- name: コードをチェックアウト

uses: actions/checkout@v4

- name: Python をセットアップ

uses: actions/setup-python@v5

with:

python-version: "3.12"

- name: lint ツールをインストール

run: pip install flake8

- name: flake8 で構文チェック

run: flake8

 

 

これの

– name: lint ツールをインストール

run: pip install flake8

ここで、pythonチェックツールのlintのflake8をインストールしています。

 

そして次に

– name: flake8 で構文チェック

run: flake8

ここでfalkeにチェックさせています。

 

チェックするのは、変更のあったファイルだけではなくて、リポジトリ内の全pythonファイルになります。

これは便利すぎる。。。

絶対に全プロジェクトに入れたほうがいいでしょ。

 

 

コメント