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

GitHub Actions提取合并PR内容至Release失败问题排查

解决GitHub Actions中PR正文同步到Release描述的错误问题

问题场景

配置了GitHub Actions工作流,当合并到master分支的PR关闭时触发,自动生成APK并附加到带指定标签的新GitHub Release中,同时希望将PR正文同步到Release描述里记录版本变更。但执行时出现以下错误:

Run PR_ID=$(echo $GITHUB_REF | sed -E 's/refs\/pull\/([0-9]+)\/merge/\1/')
no pull requests found for branch "refs/heads/master"
Error: Process completed with exit code 1.

错误原因

在PR合并触发的pull_request closed事件中,GITHUB_REF指向的是目标分支(即master),而非PR的临时合并分支refs/pull/xxx/merge。原代码通过解析GITHUB_REF提取PR ID的方式完全失效,导致gh pr view无法找到对应PR。

修复方案

1. 直接使用GitHub上下文变量获取PR ID

GitHub Actions在pull_request事件中提供了github.event.pull_request.number变量,可直接获取当前PR的ID,无需解析分支引用。

2. 优化PR正文转义处理

原代码将换行符替换为空格的处理会丢失格式,建议保留换行符,通过将PR正文写入临时文件,再用gh release create的--notes-file参数读取,确保Release描述格式正确。

3. 简化认证步骤

gh命令会自动读取GH_TOKEN环境变量,无需执行gh auth login步骤。

修改后的完整工作流代码

name: Build and Release

on:
  pull_request:
    branches: 
      - master
    types: 
      - closed
    # 仅在PR被合并时触发,避免关闭未合并的PR时执行
    if: github.event.pull_request.merged == true

jobs:
  build-and-release:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2
      with:
        fetch-depth: 0 # Fetch all tags

    - name: Setup Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '20'

    - name: Determine New Version and Tag
      id: version
      run: |
        CURRENT_VERSION=$(git describe --tags --abbrev=0)
        MAJOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f1)
        MINOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f2)
        PATCH_VERSION=$(echo $CURRENT_VERSION | cut -d. -f3)
        NEW_PATCH_VERSION=$((PATCH_VERSION+1))
        NEW_VERSION="$MAJOR_VERSION.$MINOR_VERSION.$NEW_PATCH_VERSION"
        echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
        git config --global user.email "github-actions[bot]@users.noreply.github.com"
        git config --global user.name "GitHub Actions"
        git tag "$NEW_VERSION"
        git push origin "$NEW_VERSION"

    - name: Extract PR Body
      id: extract-pr-body
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        # 直接从上下文获取PR ID
        PR_ID=${{ github.event.pull_request.number }}
        # 获取PR正文并写入临时文件
        gh pr view $PR_ID --json body -q .body > pr_body.txt
        # 将文件路径存入环境变量,后续步骤使用
        echo "PR_BODY_FILE=pr_body.txt" >> $GITHUB_ENV

    - name: Install Dependencies
      run: npm install

    - name: Build Android Release
      run: |
        mkdir -p android/app/src/main/assets/
        npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
        cd android
        rm -r app/src/main/res/drawable-*
        ./gradlew assembleRelease

    - name: Rename APK
      run: |
        mv android/app/build/outputs/apk/release/app-release.apk MyAPP-${{ env.NEW_VERSION }}.apk

    - name: Create GitHub Release and Upload APK
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        gh release create ${{ env.NEW_VERSION }} \
          MyAPP-${{ env.NEW_VERSION }}.apk \
          --title "${{ env.NEW_VERSION }}" \
          --notes-file ${{ env.PR_BODY_FILE }} \
          --notes-start "Release notes for ${{ env.NEW_VERSION }}:\n\n"

额外优化说明

  • 添加了if: github.event.pull_request.merged == true条件,确保仅在PR被合并时触发工作流,避免关闭未合并PR时执行无效操作。
  • 使用--notes-file和--notes-start组合,既保留PR正文的原始格式,又添加了版本标题前缀。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 20:05:00