【问题标题】:How to run github action tests when a python file (.py) anywhere in the project changed?当项目中任何地方的 python 文件 (.py) 发生更改时,如何运行 github 操作测试?
【发布时间】:2020-11-25 17:42:24
【问题描述】:

当项目中任何地方的任何 python 文件(.py 文件)发生更改时,我怎样才能有一个运行 pytest 的 github 操作?这个项目包含不同语言的混合,如果 python 文件在项目中的某个位置(在项目中任何级别的任何目录中)发生更改,我只想运行 pytest。

name: Test Python Tests

on:
  push:
    paths:
      - what to put here???? 

jobs:
  build-and-run:
    steps:
    - uses: actions/checkout@v1
    - name: Update Conda environment with "requirements.yml"
      uses: matthewrmshin/conda-action@v1
      with:
        args: conda env update -f ./requirements.yml
    - name: Run "pytest" with the Conda environment
      uses: matthewrmshin/conda-action@v1
      with:
        args: pytest

【问题讨论】:

    标签: python github continuous-integration pytest github-actions


    【解决方案1】:
    on:
     push:
      paths:
      - '**.py'
    

    这应该可以解决问题,请参阅Filter pattern cheat sheet

    【讨论】:

    • 好吧 - 我猜我以错误的方式解释了我在回答中引用的文档。这要简单得多。
    【解决方案2】:

    基本上,您需要的是 git diff 信息并从那里读取所有更改的文件。

    GitHub Actions 的推送事件不包括已修改文件的列表。这意味着,您必须始终触发推送时运行的工作流,然后检查通过普通 REST API 更改的文件。 https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push

    注意:GitHub Actions 可用的 webhook 有效负载不包括提交对象中添加、删除和修改的属性。您可以使用 REST API 检索完整的提交对象。有关详细信息,请参阅“获取单个提交”。

    您可以将 JavaScript 操作与 OctoKit 客户端 (https://github.com/actions/toolkit) 结合使用。如果您使用工具包中的那个,它已经通过了身份验证。

    OctoKit 可用于使 REST 调用相当容易。在https://docs.github.com/en/rest/reference/repos#get-a-commit查看默认响应200

    ...
      "files": [
        {
          "filename": "file1.txt",
          "additions": 10,
          "deletions": 2,
          "changes": 12,
          "status": "modified",
          "raw_url": "https://github.com/octocat/Hello-World/raw/7ca483543807a51b6079e54ac4cc392bc29ae284/file1.txt",
          "blob_url": "https://github.com/octocat/Hello-World/blob/7ca483543807a51b6079e54ac4cc392bc29ae284/file1.txt",
          "patch": "@@ -29,7 +29,7 @@\n....."
        }
      ]
    ...
    

    如果文件字段包含 .py 文件,请取消工作流程。您可以直接从 JS 本身取消工作流:

      core.setFailed(error.message);
    

    核心是您的 OctoKit 客户端。

    【讨论】:

      猜你喜欢
      • 2013-02-16
      • 2020-10-03
      • 1970-01-01
      • 2021-07-12
      • 2022-10-17
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      相关资源
      最近更新 更多