首页 > 解决方案 > 如何使用“deployment_status”小猫并且仅在 QAS 分支上运行 Github Actions?

问题描述

我需要 github 操作仅在 QAS 分支上运行并部署事件。它应该在“pull_request”和“pull”上运行,并且只能在 QAS 分支上运行。

name: Cypress

on: [deployment_status]

jobs:
  e2e:
    if: github.event.deployment_status.state == 'success'
    runs-on: ubuntu-latest

    steps:
      - name: Print URL
        run: echo Testing URL ${{ github.event.deployment_status.target_url }}

      - name: Checkout
        uses: actions/checkout@v2

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

      - name: Install modules
        run: yarn

      - name: Run Cypress
        uses: cypress-io/github-action@v2

但我想要这样的东西:

name: Cypress
    
    on:
      deployment_status:
        pull_request:
          branches:
            - qas
        push:
          branches:
            - qas
    
    jobs:...

标签: testingcontinuous-integrationcypressgithub-actionse2e

解决方案


目前无法单独使用触发器条件来实现您想要的。这是因为这些条件被配置为作为OR而不是作为AND

在这种情况下,一种解决方法是使用一个触发条件 -例如on: [deployment_status]您当前正在使用的 - 然后在作业级别添加一个过滤器,以根据github.ref来自Github Context的 来检查分支名称。

在你的情况下,我想它看起来像这样:

name: Cypress

on: [deployment_status]

jobs:
  e2e:
    if: ${{ github.event.deployment_status.state == 'success' && github.ref == 'refs/heads/qas' }}
    runs-on: ubuntu-latest

    steps:
      [...]

注意:让我知道是否job if condition按预期工作(我没有配置 webhook 的 poc 存储库也可以尝试使用github.event.deployment_status.state)。

注意 2:可能不需要使用${{ }}around 条件:

if: github.event.deployment_status.state == 'success' && github.ref == 'refs/heads/qas'

推荐阅读