如何在每次提交时运行GitHub工作流 [英] How to run GitHub workflow on every commit of a push
问题描述
我有一些要在存储库的每次提交中运行的测试.我的仓库中有以下脚本:
I have some tests that I would like to run on every commit of my repository. I have the following script in my repo:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: echo "my tests"
不幸的是,如果我将一些新的提交推送到我的存储库中,则仅针对最新的提交运行测试.有没有一种方法可以测试所有提交?
Unfortunately, if I push some new commits to my repository, the tests are only run against the latest commit. Is there a way to test all commits?
推荐答案
为此,可以通过检查单个提交并在单个 run:
步骤中构建每个提交来实现.
It is possible to to this by checking out individual commits and building each one in a single run:
step.
为此, checkout
操作的 fetch-depth
选项需要为 0
才能检出完整的git树.
In order to do this, the fetch-depth
option for the checkout
action needs to be 0
to checkout the full git tree.
我使用 GitPython 进行了类似的操作,以迭代并签出每次提交.
I did something like this using GitPython to iterate and checkout each commit.
仅使用 git
命令行工具, rev-list 命令可用于创建提交列表.
Using just the git
command line tool, the rev-list command could be used to create a list of commits.
棘手的部分是确定提交范围.对于请求请求,GitHub动作提供了 github.head_ref
和 github.base_ref
属性(
The tricky part is figuring out the commit range. For pull requests, GitHub actions provides github.head_ref
and github.base_ref
properties (docs) that could be used to create the commit range. However, these properties are not available for other events, like push
(in that case, github.ref
could be used with a fixed branch name like origin/main
).
这是一个简单的例子.可能需要对 rev-list
进行更高级的查询,以处理 base_ref
不是 head_ref
的祖先的情况,但我将其保留给其他要回答的问题.
Here is a simple example. It may need more a advanced query for rev-list
to handle cases where base_ref
is not an ancestor of head_ref
, but I will leave that for other SO questions to answer.
name: CI
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- run: |
for commit in $(git rev-list ${{ github.base_ref }}..${{ github.head_ref }}); do
git checkout $commit
echo "run test"
done
这篇关于如何在每次提交时运行GitHub工作流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!