This repository was archived by the owner on Jul 11, 2026. It is now read-only.
更新 python-package.yml #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 工作流名称 | |
| name: Python Application CI | |
| # 触发工作流的事件 | |
| on: | |
| push: | |
| branches: [ "main", "master" ] # 当主分支有推送时触发 | |
| pull_request: | |
| branches: [ "main", "master" ] # 当向主分支发起 Pull Request 时触发 | |
| # 定义工作流中的任务 | |
| jobs: | |
| build-and-test: | |
| # 任务名称 | |
| name: Test on ${{ matrix.os }} with Python ${{ matrix.python-version }} | |
| # 定义运行环境和 Python 版本的矩阵 | |
| strategy: | |
| fail-fast: false # 确保即使一个任务失败,其他任务也会继续运行 | |
| matrix: | |
| os: [ubuntu-latest, macos-latest, windows-latest] | |
| python-version: ["3.10", "3.11", "3.12"] | |
| # 指定任务运行的虚拟机环境 | |
| runs-on: ${{ matrix.os }} | |
| # 定义任务的步骤 | |
| steps: | |
| # 第一步:检出代码 | |
| - name: Check out repository code | |
| uses: actions/checkout@v4 | |
| # 第二步:设置 Python 环境 | |
| - name: Set up Python ${{ matrix.python-version }} | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| cache: 'pip' # 启用 pip 缓存以加快后续构建速度 | |
| # 第三步:安装依赖 | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| # 第四步:运行启动测试 | |
| # 这个步骤会尝试启动 main.py。如果启动时有任何错误,工作流会失败。 | |
| # 我们使用 timeout 命令来确保它只运行很短的时间,然后被终止。 | |
| - name: Run Startup Test | |
| run: | | |
| # 使用 timeout 命令。它会运行 python main.py, | |
| # 如果 5 秒后程序仍在运行,timeout 会自动终止它并成功退出。 | |
| # 如果程序在 5 秒内因错误退出,timeout 会以失败状态退出,从而使工作流失败。 | |
| timeout 5s python main.py || true | |
| # 仅在 Linux 和 macOS 上运行此测试 | |
| if: runner.os != 'Windows' | |
| # 为 Windows 单独设置运行测试步骤 | |
| - name: Run Startup Test on Windows | |
| run: | | |
| # 在 Windows 上,我们可以使用 Start-Process 和 Stop-Process | |
| Start-Process python -ArgumentList "main.py" -NoNewWindow | |
| Start-Sleep -Seconds 5 | |
| # 检查进程是否存在,如果存在则停止它 | |
| $process = Get-Process -Name "python" -ErrorAction SilentlyContinue | |
| if ($process) { | |
| echo "Success: main.py started without crashing." | |
| Stop-Process -Name "python" -Force | |
| } else { | |
| echo "Error: main.py exited prematurely." | |
| exit 1 | |
| } | |
| # 仅在 Windows 上运行此测试 | |
| if: runner.os == 'Windows' |