Pull Request触发GitHub Action失败问题排查求助
问题排查与解决方案
一、Pull Request触发工作流失败的排查点
1. 分支配置匹配问题
你的工作流中pull_request事件的branches: [ghactions]定义的是目标分支为ghactions的PR才会触发。如果你的PR是从其他分支合并到ghactions,这个配置是对的;但如果是ghactions分支向其他分支发起PR,这个配置不会触发工作流。请确认你的PR目标分支是否为ghactions。
2. Draft PR不触发事件
如果你的PR是草稿(Draft)状态,默认不会触发pull_request的opened/synchronize事件。可以:
- 将PR改为“Ready for review”状态;
- 或者在工作流的
types中添加ready_for_review,修改为:types: [opened, synchronize, reopened, closed, ready_for_review]
3. 仓库Action权限限制
进入仓库设置 → Actions → General,确认:
- “Allow all actions and reusable workflows”已勾选;
- “Workflow permissions”设置为“Read and write permissions”(确保工作流能读写PR评论)。
二、手动触发(workflow_dispatch)缺少PR ID的修复
当前手动触发时,脚本中使用context.issue.number无法获取PR编号,因为context.issue仅在PR/Issue事件中存在。需要修改两处:
1. 给DO部署Action传入PR编号
在Deploy the admin app步骤中,添加pr_number参数,兼容PR事件和手动触发:
- name: Deploy the admin app id: deploy-admin uses: digitalocean/app_action/deploy@v2 with: deploy_pr_preview: "true" token: ${{ secrets.DO_DEPLOY_EXPIRES_2025_12_17 }} app_spec_location: .do/admin/app.yaml pr_number: ${{ github.event.inputs.pr_number || github.event.pull_request.number }}
2. 修复评论脚本中的PR编号获取
将两个github-script步骤中的issue_number改为兼容手动输入和PR事件的取值:
issue_number: ${{ github.event.inputs.pr_number || context.issue.number }}
三、完整修复后的工作流配置
name: App Platform Preview on: pull_request: branches: [ghactions] types: [opened, synchronize, reopened, closed, ready_for_review] workflow_dispatch: inputs: pr_number: description: 'Pull Request Number' required: true type: number permissions: contents: read pull-requests: write jobs: deploy-admin: name: Deploy Admin runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Deploy the admin app id: deploy-admin uses: digitalocean/app_action/deploy@v2 with: deploy_pr_preview: "true" token: ${{ secrets.DO_DEPLOY_EXPIRES_2025_12_17 }} app_spec_location: .do/admin/app.yaml pr_number: ${{ github.event.inputs.pr_number || github.event.pull_request.number }} - uses: actions/github-script@v7 env: BUILD_LOGS: ${{ steps.deploy-admin.outputs.build_logs }} DEPLOY_LOGS: ${{ steps.deploy-admin.outputs.deploy_logs }} with: script: | const { BUILD_LOGS, DEPLOY_LOGS } = process.env; github.rest.issues.createComment({ issue_number: ${{ github.event.inputs.pr_number || context.issue.number }}, owner: context.repo.owner, repo: context.repo.repo, body: `🚀 The **admin app** was successfully deployed at ${{ fromJson(steps.deploy-admin.outputs.app).live_url }}.` }); - uses: actions/github-script@v7 if: failure() with: script: | const { BUILD_LOGS, DEPLOY_LOGS } = process.env; github.rest.issues.createComment({ issue_number: ${{ github.event.inputs.pr_number || context.issue.number }}, owner: context.repo.owner, repo: context.repo.repo, body: `The **admin app** failed to be deployed. Logs can be found [here](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}). ## Logs <details> <summary>Build logs</summary> ``` ${BUILD_LOGS} ``` </details> <details> <summary>Deploy logs</summary> ``` ${DEPLOY_LOGS} ``` </details>` });
内容的提问来源于stack exchange,提问作者KoalaKid
相关产品推荐
相关产品推荐

