在GitHub Action工作流中进行推送或合并之前,如何获取上一次提交? [英] How can I get the previous commit before a push or merge in GitHub Action workflow?

查看:345
本文介绍了在GitHub Action工作流中进行推送或合并之前,如何获取上一次提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Nx 用于新项目的monorepo支持. Nx的好处之一是,它可以确定monorepo中的哪些应用受到一系列更改(开始提交,结束提交)的影响.因此,如果您有一堆应用程序,则只需构建,测试和部署实际上受更改影响的应用程序,而不是整个monorepo.

I'm using Nx for monorepo support on a new project. One of the benefits of Nx is that it can determine which apps in the monorepo are affected by a range of changes (start commit, end commit). So if you have a bunch of apps, you only have to build, test, and deploy the apps that are actually affected by the changes instead of the entire monorepo.

我想设置一个GitHub Action工作流,以便仅在推送或合并到主服务器上部署受影响的应用程序.但是,我在弄清楚如何获取更改范围的开始提交"时遇到了麻烦.换句话说,如何获取上次部署的提交哈希?

I'd like to setup a GitHub Action workflow to deploy only the affected apps on push or merge to master. However, I'm having trouble figuring out how to get the "start commit" for the range of changes. In other words, how do I get the commit hash of the last deploy?

GitHub提供了一个环境变量GITHUB_BASE_REF,但仅适用于从分叉存储库运行到头部存储库的工作流.

GitHub provides an env variable GITHUB_SHA but that's the commit that triggered the workflow (i.e. the "end commit"). It also provides GITHUB_BASE_REF but that only works on workflows running from a forked repo comparing to the head repo.

CircleCI为此具有 pipeline.git.base_revision . GitHub Actions有类似的东西吗?

CircleCI has pipeline.git.base_revision for this purpose. Do GitHub Actions have something similar?

推荐答案

对于请求请求事件,可以在github上下文中找到基址的ref和sha,如下所示.

For pull request events the ref and sha for the base can be found in the github context as follows.

${{ github.event.pull_request.base.ref }}
${{ github.event.pull_request.base.sha }}

对于推送事件,有base_refbefore参数.

For push events there are base_ref and before parameters.

${{ github.event.base_ref }}
${{ github.event.before }}

before是推送到分支base_ref上原点的最后一个git sha.请注意,如果这是在新分支上的第一次提交,则base_refbefore将具有null/默认值,如下所示.

before is the last git sha pushed to origin on branch base_ref. Note that if this is the first commit on a new branch, base_ref and before will have null/default values as shown below.

##[debug]  "event": {
##[debug]    "after": "727f7aec97c394083d769029e5f619e9b094a235",
##[debug]    "base_ref": null,
##[debug]    "before": "0000000000000000000000000000000000000000",
...

顺便说一句,您可以转储github上下文并通过将以下步骤添加到工作流程中来检查可用参数:

By the way, you can dump the github context and check the available parameters by adding this step to your workflow:

      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"

这篇关于在GitHub Action工作流中进行推送或合并之前,如何获取上一次提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆