在拉取请求触发的 GH 操作中检索提交消息

问题描述 投票:0回答:1

我想创建一个在 pull_request 上触发的 GitHub Actions 工作流程。工作流程应获取拉取请求中的最后提交消息,并根据给定的正则表达式对其进行检查。

我正在努力的部分实际上是获取最后的提交消息。

我的工作流程如下所示:

---
name: CI-PR-Check

on:
  pull_request:

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3
 
      - name: Check commit message
        shell: bash
        run: |
          echo "commit message validation"
          message=$(git log --pretty=%B -n 1)
          python ${{ github.action_path }}/commitcheck.py "$message"

我希望

message=$(git log --pretty=%B -n 1)
行能够获取上次提交的消息,但我从拉取请求中获取消息,所以类似于
Merge <some sha> into <other sha>

使用 GitHub webhook events,我能够获取我感兴趣的提交的 sha

sha="${{ github.event.pull_request.head.sha }}"
。但是,仍然无法通过
git log --format=%B -n 1 $sha
获取提交消息。我收到错误
fatal: bad object <my sha>

有什么方法可以获取我的提交消息吗?

git github-actions pull-request git-commit github-webhook
1个回答
0
投票

你必须使用正确的sha

name: CI-PR-Check

on:
  pull_request:

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
 
      - name: Check commit message
        shell: bash
        run: |
          echo "commit message validation"
          message=$(git log --pretty=%B -n 1)
          python ${{ github.action_path }}/commitcheck.py "$message"

      - name: Last Commit Message
        id: last-commit
        run: echo "::set-output name=message::$(git log -1 --pretty=%B)"

      - name: Check commit message
        shell: bash
        run: |
          echo "commit message validation"
          message=$(git log --pretty=%B -n 1)
          python ${{ github.action_path }}/commitcheck.py "${{ steps.last-commit.outputs.message }}"

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.