运行 使用 Cloud Build Trigger 对 Github 拉取请求中提到的文件进行 Pytest
Running Pytest on files mentioned in a Github Pull Request using Cloud Build Trigger
我有一个 Cloud Build 触发器,它 运行 仅当在 Github 存储库的主分支上创建新的合并请求时才触发。
但是,现在,pytest 在整个存储库上 运行,而我要求 pytest 仅在合并请求中 created/edited 的文件上 运行。
我的cloudbuild文件如下:
#Testing
steps:
- name: 'python:3.7'
id: Testing
entrypoint: 'bash'
args:
- '-c'
- |
pip install --upgrade pip \
&& pip install -r requirements.txt \
&& pytest **/test_*.py
知道如何实现吗?
我认为 Cloud Build 不会在拉取请求中公开已修改文件的列表,但作为解决方法,您可以使用 Github 的 webhooks。
根据 documentation 使用替换过程在云构建中获取“_PULL_REQUEST_ID”
_PULL_REQUEST_ID $(pull_request.pull_request.id)
使用在github rest api替换过程中收到的上述pull request id并获取modified/created个文件的列表。
a) 要了解此 api 的工作原理,请参阅 api link.
b) rest的使用方法api参考rest link.
c) 请参阅 github github link.
处的示例代码
根据 doc 将 GITHUB_REPOSITORY 更新为 $REPO_NAME 并将 github.event.pull_request.number 更新为在步骤 1
中收到的号码
URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${{ github.event.pull_request.number }}/files"
FILES=$(curl -s -X GET -G $URL | jq -r '.[] | .filename')
echo “$FILES” > modified.txt
按照上面的示例代码,文件列表将保存到 modified.txt。当您获得 modified/created 文件列表时,您可以通过使用 bash while 循环或 python 循环从 modified.txt 获取文件名来对这些文件执行测试。
我有一个 Cloud Build 触发器,它 运行 仅当在 Github 存储库的主分支上创建新的合并请求时才触发。
但是,现在,pytest 在整个存储库上 运行,而我要求 pytest 仅在合并请求中 created/edited 的文件上 运行。
我的cloudbuild文件如下:
#Testing
steps:
- name: 'python:3.7'
id: Testing
entrypoint: 'bash'
args:
- '-c'
- |
pip install --upgrade pip \
&& pip install -r requirements.txt \
&& pytest **/test_*.py
知道如何实现吗?
我认为 Cloud Build 不会在拉取请求中公开已修改文件的列表,但作为解决方法,您可以使用 Github 的 webhooks。
根据 documentation 使用替换过程在云构建中获取“_PULL_REQUEST_ID”
_PULL_REQUEST_ID $(pull_request.pull_request.id)
使用在github rest api替换过程中收到的上述pull request id并获取modified/created个文件的列表。
a) 要了解此 api 的工作原理,请参阅 api link.
b) rest的使用方法api参考rest link.
c) 请参阅 github github link.
处的示例代码根据 doc 将 GITHUB_REPOSITORY 更新为 $REPO_NAME 并将 github.event.pull_request.number 更新为在步骤 1
中收到的号码URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${{ github.event.pull_request.number }}/files" FILES=$(curl -s -X GET -G $URL | jq -r '.[] | .filename') echo “$FILES” > modified.txt
按照上面的示例代码,文件列表将保存到 modified.txt。当您获得 modified/created 文件列表时,您可以通过使用 bash while 循环或 python 循环从 modified.txt 获取文件名来对这些文件执行测试。