You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将GitHub Actions工作流提交数据传入JS文件

GitHub Actions版本更新问题解决

问题描述

需求目标:读取PR相关的所有提交记录,检查是否包含feat、fix、BREAKING CHANGE等关键词,若包含则递增应用版本号(如2.11.7)。

遇到的异常:在使用pull_request_target事件的工作流中,尝试将github.event.commits和github.event传入处理版本更新的JS文件时,二者均为null,但github.event.pull_request可正常传递。

附相关工作流代码:

name: update-version
on:
  pull_request_target:
    types:
    - closed

jobs: 
  if_merged:
    if: github.event.pull_request.merged == true && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/test')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Run script to update the version stored in package.json
        # How do I pass a github.event.commits to the below file?
        run: node ./shell-scripts/update-version.js github.event github.event.commits github.event.pull_request

      - run: |
          echo The version was updated
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"
          git add -A
          git commit -m "updated version"
          git push
          echo The version update has been committed

原因分析

pull_request_target事件的Payload中没有github.event.commits字段,该事件的核心数据都封装在github.event.pull_request对象里。另外你直接在shell命令里写github.event没有被正确解析为GitHub上下文变量,需要用${{ github.event }}语法,但即使解析成功,github.event里也不存在commits属性。

解决方案

方法1:通过Git命令在JS脚本中获取提交记录

先修改checkout步骤拉取完整提交历史,再在JS脚本中通过Git命令提取PR相关的提交信息:

  1. 更新checkout步骤:
- name: Checkout code
  uses: actions/checkout@v4
  with:
    fetch-depth: 0  # 拉取所有提交历史,确保能获取PR的所有提交
  1. 在update-version.js中,用child_process执行Git命令获取PR合并前的提交范围:
const { execSync } = require('child_process');
const prBaseRef = process.argv[2];

// 获取base分支到当前分支的所有提交消息
const commitsOutput = execSync(`git log ${prBaseRef}..HEAD --pretty=format:"%B"`).toString();
const commits = commitsOutput.split('\n\n').filter(Boolean);

// 检查提交信息中是否包含目标关键词
const hasFeat = commits.some(commit => commit.includes('feat'));
const hasFix = commits.some(commit => commit.includes('fix'));
const hasBreaking = commits.some(commit => commit.includes('BREAKING CHANGE'));

// 后续版本号递增逻辑...
  1. 修改工作流中的脚本调用步骤,传入PR的base分支:
- name: Run script to update the version stored in package.json
  run: node ./shell-scripts/update-version.js "${{ github.event.pull_request.base.ref }}"

方法2:通过GitHub CLI获取PR提交记录

利用GitHub CLI直接拉取PR的所有提交信息,再传递给JS脚本:

  1. 添加步骤获取PR提交消息:
- name: Get PR commits
  id: get-commits
  run: |
    # 获取PR的所有提交消息,用|分隔以便传递
    COMMITS=$(gh pr view ${{ github.event.pull_request.number }} --json commits --jq '.commits | .[] | .message' | tr '\n' '|')
    echo "commits=$COMMITS" >> $GITHUB_OUTPUT
  1. 修改脚本调用步骤,传递提交信息和PR对象:
- name: Run script to update the version stored in package.json
  run: node ./shell-scripts/update-version.js "${{ steps.get-commits.outputs.commits }}" "${{ toJSON(github.event.pull_request) }}"
  1. 在JS脚本中解析提交信息:
const commitsStr = process.argv[1];
const pr = JSON.parse(process.argv[2]);

// 还原提交消息列表
const commits = commitsStr.split('|').filter(Boolean);

// 关键词检查逻辑同方法1...

额外注意事项

  • 所有GitHub上下文变量必须用${{ ... }}的语法包裹,才能在shell命令中被正确解析。
  • 使用方法1时,pull_request_target事件在PR合并后默认会checkout目标分支,确保Git命令能获取到正确的提交范围。

内容的提问来源于stack exchange,提问作者ezg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 07:52:54