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

如何启动新测试时取消Firebase Test Lab的旧运行任务?

解决方案:取消同一分支的旧Firebase Test Lab测试任务

方案1:用GitHub Actions原生并发控制终止旧工作流

这是最省心的方式——直接通过GitHub Actions的concurrency配置,让新工作流启动时自动终止同一分支/PR的旧工作流,从根源上避免并行测试。

在你的工作流YAML文件(比如.github/workflows/firebase-test.yml)顶部添加:

concurrency:
  group: ${{ github.head_ref || github.ref_name }}
  cancel-in-progress: true
  • group用github.head_ref标识PR分支,确保同一PR的所有工作流归为一组;如果是直接推送的分支,github.ref_name也能精准匹配。
  • cancel-in-progress: true开启自动取消:新工作流启动时,同一组内正在运行的旧工作流会被立即终止。

注意:如果旧工作流已经把测试任务提交到Firebase,工作流终止后Firebase侧的测试可能会短暂继续运行,但后续不会占用更多资源,适合大多数场景。

方案2:保存Test Matrix ID,新工作流主动取消Firebase测试

如果需要彻底终止Firebase侧的测试任务,可以在旧工作流中存储生成的Test Matrix ID,新工作流启动时调用Firebase的cancel接口。

步骤1:捕获并存储Test Matrix ID

在运行Firebase测试的步骤中,用gcloud命令的输出提取ID,并存到GitHub分支变量(需要gh CLI授权):

# 运行测试并提取testMatrixId
TEST_MATRIX_ID=$(gcloud firebase test android run --your-test-config --format=json | jq -r '.testMatrixId')

# 将ID存入当前分支的临时变量
gh variable set -b ${{ github.head_ref }} FIREBASE_OLD_TEST_MATRIX --body "$TEST_MATRIX_ID"

步骤2:新工作流启动时取消旧测试

在工作流开头添加步骤,查询并取消旧的测试矩阵:

# 获取旧的testMatrixId
OLD_TEST_ID=$(gh variable get -b ${{ github.head_ref }} FIREBASE_OLD_TEST_MATRIX 2>/dev/null)

# 如果ID存在,调用cancel接口
if [ ! -z "$OLD_TEST_ID" ]; then
  gcloud firebase test matrices cancel $OLD_TEST_ID --project=your-firebase-project-id
  # 删除旧变量,避免重复取消
  gh variable remove -b ${{ github.head_ref }} FIREBASE_OLD_TEST_MATRIX
fi

方案3:通过Firebase API筛选并取消分支对应的测试

如果不想依赖GitHub变量,直接调用Firebase的测试矩阵列表API,筛选出当前分支的未完成测试并取消。

首先,运行测试时把分支名传入测试矩阵的环境变量:

gcloud firebase test android run --your-test-config --environment-variables=branch=${{ github.head_ref }}

然后在新工作流开头添加取消步骤:

# 筛选当前分支的正在运行的测试矩阵ID,逐个取消
gcloud firebase test matrices list --project=your-firebase-project-id --filter="state:IN_PROGRESS" --format=json | \
jq -r '.[] | select(.environmentVariables.branch == "${{ github.head_ref }}") | .testMatrixId' | \
while read -r TEST_ID; do
  gcloud firebase test matrices cancel $TEST_ID --project=your-firebase-project-id
done

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 06:52:46