在GitHub Action工作流中进行推送或合并之前,如何获取上一次提交?

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工作流,以便仅在推送或合并到master时部署受影响的应用。但是,我在弄清楚如何获取更改范围的“开始提交”时遇到了麻烦。换句话说,如何获取上次部署的提交哈希?

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 has pipeline.git.base_revision for this purpose. Do GitHub Actions have something similar?

评论
  • 凹凸曼
    凹凸曼 回复

    GitHub provides GITHUB_BASE_REF and the github.base_ref context that contain the base branch.

    If you're interested in the latest revision of that branch, you can run git rev-parse $GITHUB_BASE_REF to find it. If you're interested in the point at which the branches forked, you can run git merge-base $GITHUB_BASE_REF $GITHUB_SHA to find it.

    请注意,使用不兼容的API更改可能会破坏其他项目,而无需对它们进行任何代码更改,因此,虽然仅测试已更改的应用程序会更快,但您可能会发现这样做会导致意外损坏。