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

Github Workflow未执行排查:PR触发Firebase部署流程故障

问题

我创建了一个.yml格式的GitHub Workflow,预期在创建Pull Request时自动执行,但目前未触发运行。我期望实现的流程如下:

  1. 获取分支名称
  2. 提取分支名称的最后部分(例如从feature/hosting/test中提取test),将其指定为Firebase的部署渠道
  3. 部署完成后安装jq工具
  4. 获取部署生成的Hosting URL
  5. 将该URL添加为Pull Request的评论

我的.yml文件内容如下:

name: Firebase Test Deploy

on:
  pull_request:
    branches:
      - bugfix/hosting/*
      - feature/hosting/*

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Install Firebase CLI
        run: npm install -g firebase-tools
      - name: Get branch name
        id: get_branch_name
        run: echo "::set-output name=branch::$(echo $GITHUB_REF | cut -d/ -f2-)"
      - name: Deploy to Firebase
        run: |
          cd functions && firebase use --token $FIREBASE_TOKEN
          firebase hosting:channel:deploy ${{ steps.get_branch_name.outputs.branch }} --only hosting
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - name: Install jq
        run: sudo apt-get update -y && sudo apt-get install -y jq
      - name: Get Hosting URL
        id: get_hosting_url
        run: |
          hosting_url=$(firebase hosting:channel:list --json | jq -r '.[0].url')
          echo "::set-output name=hosting_url::$hosting_url"
        shell: bash
      - name: Comment on Pull Request
        uses: actions/github-script@0.12.0
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const { context, github } = require('@actions/github');
            
            const comment = `
              **Firebase Hosting URL:**
              ${steps.get_hosting_url.outputs.hosting_url}
            `;
            
            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: comment,
            })

.yml文件路径为.github/workflows。请问问题出在哪里?


问题排查与修复方案

1. Workflow未触发:分支触发规则理解错误

on.pull_request.branches 指定的是PR的目标分支,而非发起PR的源分支。如果你的PR是合并到main/master等分支,而非bugfix/hosting/*或feature/hosting/*,Workflow不会触发。

若需源分支匹配规则时触发,修改触发配置:

on:
  pull_request:
    types: [opened, synchronize] # PR创建/推送新代码时都触发
    head_ref:
      - bugfix/hosting/*
      - feature/hosting/*

2. 分支名称获取错误

pull_request事件中,$GITHUB_REF指向目标分支,而非PR源分支。需用${{ github.head_ref }}获取源分支,同时调整提取逻辑以拿到分支最后一段:

- name: Get branch name suffix
  id: get_branch_suffix
  run: |
    BRANCH_NAME="${{ github.head_ref }}"
    SUFFIX=$(echo "$BRANCH_NAME" | awk -F/ '{print $NF}')
    echo "::set-output name=suffix::$SUFFIX"

3. Firebase部署命令缺陷

  • cd functions && firebase use路径错误:firebase use需在项目根目录执行,否则无法识别Firebase配置
  • 部署渠道应使用分支后缀,而非完整分支名

修正后的部署步骤(需替换项目别名/ID):

- name: Deploy to Firebase Hosting channel
  id: firebase_deploy
  run: |
    firebase use --token $FIREBASE_TOKEN <你的Firebase项目别名或ID>
    # 部署时直接提取URL,避免后续list命令取错渠道
    DEPLOY_JSON=$(firebase hosting:channel:deploy ${{ steps.get_branch_suffix.outputs.suffix }} --only hosting --json)
    HOSTING_URL=$(echo "$DEPLOY_JSON" | jq -r '.result.url')
    echo "::set-output name=hosting_url::$HOSTING_URL"
  env:
    FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}

4. PR评论变量引用错误

github-script的JS代码无法直接访问steps输出,需通过args传递变量:

- name: Add Hosting URL to PR comment
  uses: actions/github-script@0.12.0
  with:
    github-token: ${{ secrets.GITHUB_TOKEN }}
    args: ${{ steps.firebase_deploy.outputs.hosting_url }}
    script: |
      const { context, github } = require('@actions/github');
      const hostingUrl = args;

      const commentBody = `
      **Firebase Test Deployment URL:**
      ${hostingUrl}
      `;

      await github.issues.createComment({
        owner: context.repo.owner,
        repo: context.repo.repo,
        issue_number: context.issue.number,
        body: commentBody.trim()
      });

完整修复后的Workflow代码

name: Firebase Test Deploy

on:
  pull_request:
    types: [opened, synchronize]
    head_ref:
      - bugfix/hosting/*
      - feature/hosting/*

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

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

      - name: Install Firebase CLI
        run: npm install -g firebase-tools

      - name: Install jq
        run: sudo apt-get update -y && sudo apt-get install -y jq

      - name: Get branch name suffix
        id: get_branch_suffix
        run: |
          BRANCH_NAME="${{ github.head_ref }}"
          SUFFIX=$(echo "$BRANCH_NAME" | awk -F/ '{print $NF}')
          echo "::set-output name=suffix::$SUFFIX"

      - name: Deploy to Firebase Hosting channel
        id: firebase_deploy
        run: |
          firebase use --token $FIREBASE_TOKEN <你的Firebase项目别名或ID>
          DEPLOY_JSON=$(firebase hosting:channel:deploy ${{ steps.get_branch_suffix.outputs.suffix }} --only hosting --json)
          HOSTING_URL=$(echo "$DEPLOY_JSON" | jq -r '.result.url')
          echo "::set-output name=hosting_url::$HOSTING_URL"
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}

      - name: Add Hosting URL to PR comment
        uses: actions/github-script@0.12.0
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          args: ${{ steps.firebase_deploy.outputs.hosting_url }}
          script: |
            const { context, github } = require('@actions/github');
            const hostingUrl = args;

            const commentBody = `
            **Firebase Test Deployment URL:**
            ${hostingUrl}
            `;

            await github.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
              body: commentBody.trim()
            });

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:31:08