如何在每次提交时运行GitHub工作流

编程入门 行业动态 更新时间:2024-10-27 14:33:32
本文介绍了如何在每次提交时运行GitHub工作流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些要在存储库的每次提交中运行的测试.我的仓库中有以下脚本:

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 属性( docs )可用于创建提交范围.但是,这些属性不适用于其他事件,例如 push (在这种情况下, github.ref 可以与固定的分支名称一起使用,例如 origin/main).

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工作流

本文发布于:2023-11-24 09:24:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时运   工作流   如何在   GitHub

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!