Pau*_* CV 5 python testing github github-actions
我创建了工作流程来在提交之前测试我的 Python 应用程序。问题是,如果测试失败,提交无论如何都会被推送。如果测试不成功,我如何添加一个条件来避免推送?
工作流程文件 .yml 的结构如下。
name: Python application
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Lint with flake8
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pip install pytest
pytest
Run Code Online (Sandbox Code Playgroud)
由于几个原因,您实际上无法阻止 CI 系统的推送。
首先,您的 CI 系统需要能够访问要推送的数据,这意味着它必须位于某个存储库中,以便可以获取。其次,CI 系统可能需要很长时间才能运行,没有人愿意在 CI 系统运行时等待推送成功或失败。如果您在工作日结束前加班怎么办?
通常执行此操作的方法是推送到分支,让 CI 系统运行,然后合并它。如果您与多人合作,那么使用拉取请求并将 CI 设置为在打开或更新时运行是正确的做法。否则,您可以将工作流程设置为在所有分支上运行(如下所示),然后在分支通过时合并分支:
on: push
Run Code Online (Sandbox Code Playgroud)