繁体   English   中英

如何在每次提交推送时在 GitHub 工作流中运行 commitlint

[英]How to run commitlint in GitHub workflow on every commit of a push

我有一个 Github 存储库,在本地安装了commitlint 和 husky,并希望在验证拉取请求时设置一个在每次推送提交时运行 commitlint 的工作流。 在主分支上,较旧的提交不遵循传统的提交规则。

根据此评论,我创建了一个单独的分支

https://github.com/conventional-changelog/commitlint/issues/586#issuecomment-657226800

我从这个工作流程开始

name: Run commitlint on pull request

on: pull_request

jobs:
  run-commitlint-on-pull-request:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 14.x

      - name: Install dependencies
        run: npm install

      - name: Validate all commits from PR
        run: npx commitlint --from HEAD~${{ github.event.pull_request.commits }} --to HEAD --verbose

我按照传统的提交规则又做了两次提交并开始了一个拉取请求

  • 我预计工作流不会运行,因为我在主分支上还不存在。
    • 实际上它运行
  • 我希望工作流只检查 PR 提交
    • 工作流失败,因为它开始验证主分支中的每个提交。 而且由于我知道旧的提交不遵守规则,所以这永远不会通过。

我想到的第一个解决方案是重新设置所有内容并重命名每个提交以遵循规则,但这需要付出巨大的努力。

我不确定我是否必须在这里改进这条线

npx commitlint --from HEAD~${{ github.event.pull_request.commits }} --to HEAD --verbose

仅检查 PR 的提交(不幸的是,我不知道那里需要修复什么)。

您有什么想法或者正在重新定位和重命名唯一的解决方案?

解决方案

直接的解决方案是使用 commitlint 的--to--from argumentsSHA-1 值而不是分支名称或相对引用。 一方面,这可靠地解决了工作树中未知修订或路径的问题。 另一方面,只会检查 PR 的 scope 中的提交。 作为旁注:GitHub 对正在签出的临时合并使用相同的引用 (SHA-1)。

我们需要基础-SHA 和头部-SHA。 在 GitHub 操作中,这些值在 github 上下文中的事件的拉取请求github中可用。

因此,您可以使用经过测试并按预期工作的以下行:

npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose

演示

这是GitHub 上的 POC 存储库,带有 3 个测试用例(拉取请求)。

完整的工作流程

name: Run Commitlint on PR

on:
  pull_request:

jobs:

  run-commitlint-on-pr:
    runs-on: ubuntu-latest

    steps:

      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 14.x

      - name: Install dependencies
        run: npm install

      - name: Validate all commits from PR
        run: npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose

git rev-list被认为是因为拉取请求 (PR) 的提交似乎无效。 不需要循环。

这个问题提示检查 PR 分支,这似乎比获取 PR 提交更简单。 从问题来看,似乎不需要对默认分支进行测试。

- uses: actions/checkout@v2
        with:
          fetch-depth: 0
          ref: ${{ github.event.pull_request.base.sha }}

lint 指令是:

npx commitlint --from ${{ github.event.pull_request.base.sha }} --verbose

拉取请求有效负载的文档不会立即提供提交列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM