この記事は1191文字3で読めます

最近GitHub Actionを触って便利さに気がついてしまったのでご紹介します。

#Table of Contents

#GitHub Actionとは?

GitHub Actionとは、 built by you, run by us です。(公式より)

詰まるところGithub製のCIです。

結構簡単に使えたのでご紹介します。

#GitHub Actionのプレビューに応募する

GitHub Action自体はまだプレビュー版ですので、こちらのサイトから 利用申請をする必要があります。

私は申し込みから一週間くらいで使えるようになりました。

無事利用できるようになりますと、レポジトリにActionボタンが 出てきます。

GitHubレポジトリにActionボタンが表示されている画面

#Workflowを設定する

GitHub ActionはほかのCIと同じくYAMLファイルで定義します。

今回はPythonのPytestでテストを回します。

下記のようにPython環境の設定、のパッケージBuild、Pytestまでを設定します。

name: Python package

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.6, 3.7]

    steps:
    - uses: actions/checkout@v1
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Setup ebook-homebrew
      run: |
        python setup.py install
    - name: Test with pytest
      run: |
        pip install pytest
        pip install -r requirements-test.txt
        pytest --it
    - name: Lint check
      run: |
        black ebook_homebrew setup.py --check

#Trigger設定

今回はpush時に動くようにしますので、

name: Python package

on: [push]

jobs:
  build:

のように設定します。

Triggerについてはこちらをご確認ください。

#Matrixでテスト

Matrixでテストする場合は下記のようにPythonのバージョンを複数設定します。

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.6, 3.7]

    steps:
    - uses: actions/checkout@v1
    - name: Set up Python ${{ matrix.python-version }}

#Stepの設定

StepはほかのCIと同じような感じで設定します。

    steps:
    - uses: actions/checkout@v1
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Setup ebook-homebrew
      run: |
        python setup.py install
    - name: Test with pytest
      run: |
        pip install pytest
        pip install -r requirements-test.txt
        pytest --it
    - name: Lint check
      run: |
        black ebook_homebrew setup.py --check

#CI起動時の動き

無事PushでCIが動くと各push、PRごとにActionに結果がでます。

GitHub ActionのCI実行結果一覧:各PushやPRごとにActionの結果が表示されている

さらに、実行ログも確認できます。

もう普通のCIですね。

GitHub Actionの実行ログ詳細画面:各Stepの実行状況が確認できる

#結論

GitHub Actionを使えば、めんどっちいCIサーバー連携も不要で楽々です。GitHub Actionの活用例として、GitHub JavaScript Actionを使ったRelease自動化や、depcheckをGitHub Actionで使ってPRコメントに結果を出力する方法も紹介していますので参考にどうぞ。

tubone24にラーメンを食べさせよう!

ぽちっとな↓

Buy me a ramen