Poetry & Github Action

Python
Github Action
Poetry
Author

曳影

Published

December 30, 2022

使用Poetry做Python的项目管理工具,集成到Github Actions,包含以下内容: - Check out 代码仓库 - 启动Python环境,可以限制版本,也可以使用多个版本 - 安装Poetry - 设置虚拟环境缓存 - 安装依赖 - 代码格式化(yapf) - 代码静态类型检查(pytype) - 代码测试,以及覆盖率测试报告

Github Action workflow 配置如下:

name: CI
on: push

jobs:
  ci:
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.8.5"]
        poetry-version: ["1.2.2"]
        os: [ubuntu-18.04]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Check out repository
        uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install Poetry
        uses: snok/install-poetry@v1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
          installer-parallel: true
      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v3
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

      - name: Install Denpendencies
        if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
        run: poetry install --no-interaction --no-root

      - name: Code Format
        run: poetry run yapf

      - name: Type Check
        run: poetry run pytype --config=pytype.cfg

      - name: Testing and coverage
        run: poetry run pytest --cov