You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GitHub Action依赖安装步骤前切换至子目录app?

解决PyTest在GitHub Action中因pyproject.toml位于子目录的运行问题

问题描述

代码推送到仓库时希望运行Pytest,但pyproject.toml文件位于子目录app而非根目录,需要在GitHub Action的“Install dependencies”步骤前切换到app目录以解决问题,原GitHub Action workflow文件如下:

# .github/workflows/app.yaml
name: PyTest
on: push

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Switch to Current Branch
        run: git checkout ${{ env.BRANCH }}

      # Setup Python (faster than using Python container)
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.x"

      # install & configure poetry
      - name: Install Poetry
        uses: snok/install-poetry@v1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
          installer-parallel: true

      # load cached venv if cache exists
      - 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') }}

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

      # install root project
      - name: Install project
        run: poetry install --no-interaction

      # run test suite
      - name: Run tests
        run: |
          export PYTHONPATH=$PWD
          poetry run pytest -v -cov=pp_from_python

解决方案

有两种常用方式可以实现切换到子目录运行相关步骤,同时适配Poetry的依赖管理和缓存逻辑:


方式一:使用working-directory指定工作目录

给所有需要在app目录下执行的步骤添加working-directory: app参数,同时调整缓存路径和依赖文件的哈希匹配规则:

修改后的完整workflow:

# .github/workflows/app.yaml
name: PyTest
on: push

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Switch to Current Branch
        run: git checkout ${{ env.BRANCH }}

      # Setup Python (faster than using Python container)
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.x"

      # install & configure poetry
      - name: Install Poetry
        uses: snok/install-poetry@v1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
          installer-parallel: true

      # load cached venv if cache exists
      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v3
        with:
          path: app/.venv
          key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('app/poetry.lock') }}

      # install dependencies if cache does not exist
      - name: Install dependencies
        if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
        working-directory: app
        run: poetry install --no-interaction --no-root

      # install root project
      - name: Install project
        working-directory: app
        run: poetry install --no-interaction

      # run test suite
      - name: Run tests
        working-directory: app
        run: |
          export PYTHONPATH=$PWD
          poetry run pytest -v -cov=pp_from_python

方式二:添加切换目录的步骤

在Setup Python之后添加切换到app目录的步骤,后续步骤会自动继承该工作目录,同时调整缓存路径:

修改后的完整workflow:

# .github/workflows/app.yaml
name: PyTest
on: push

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Switch to Current Branch
        run: git checkout ${{ env.BRANCH }}

      # Setup Python (faster than using Python container)
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.x"

      # 切换到app子目录
      - name: Switch to app directory
        run: cd app && echo "PWD=$(pwd)" >> $GITHUB_ENV

      # install & configure poetry
      - name: Install Poetry
        uses: snok/install-poetry@v1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
          installer-parallel: true

      # load cached venv if cache exists
      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v3
        with:
          path: app/.venv
          key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('app/poetry.lock') }}

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

      # install root project
      - name: Install project
        run: poetry install --no-interaction

      # run test suite
      - name: Run tests
        run: |
          export PYTHONPATH=$PWD
          poetry run pytest -v -cov=pp_from_python

关键修改说明

  1. 缓存路径调整:将缓存路径从.venv改为app/.venv,确保缓存的是app目录下的虚拟环境
  2. 依赖锁文件匹配:将hashFiles('**/poetry.lock')改为hashFiles('app/poetry.lock'),精准匹配app目录下的依赖锁文件,避免误匹配其他目录的文件
  3. 工作目录切换:通过working-directory参数或cd命令切换到app目录,让Poetry能正确找到pyproject.toml文件执行依赖安装和测试命令

内容的提问来源于stack exchange,提问作者Christoph H.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:05:58