Github ActionsでPyPIにパッケージを公開する
2019-09-13

Github Actionsは2019/09/13時点でbetaです。
将来的にこの内容で動作しなくなるかもしれません。

Github Actions?

  • Githubが出したCIサービス
  • yamlで記述
  • ジョブを最大20並列で動かせたりいい感じっぽい
  • 日本語ドキュメント嬉しい

PyPIにpublishしてみる

  • python3.6と3.7でテスト
  • テストが通ったらpublish
    • needs: test で依存関係を作成
  • PyPIへのログイン情報はsecretsから取得
    • settings > Secrets から設定

name: Publish python package

on:
  push:
    branches:
      - master

jobs:
  test:
    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: Lint with flake8
        run: |
          pip install flake8
          flake8 gql_query_builder/ --count --select=E9,F63,F7,F82 --show-source --statistics
          flake8 gql_query_builder/ --count --exit-zero --max-complexity=10 --max-line-length=256 --statistics
      - name: Test
        run: |
          python -m unittest discover tests -v

  publish:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Init .pypirc
        env:
          PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }}
          PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
        run: |
          echo -e '[pypi]' >> ~/.pypirc
          echo -e "username = ${PYPI_USERNAME}" >> ~/.pypirc
          echo -e "password = ${PYPI_PASSWORD}" >> ~/.pypirc
      - name: Publish
        run: |
          pip install twine
          python setup.py publish

ref. https://github.com/youyo/gql-query-builder/blob/master/.github/workflows/publish.yaml