Github Actions:有没有办法对从特定分支打开的拉取请求运行操作?

Nje*_*igi 4 pull-request github-actions

我正在尝试创建一个操作,其工作流程在针对主服务器打开来自特定分支的拉取请求时运行。我目前所拥有的在所有分支针对 master 打开的所有 Pull 请求上运行工作流程。

name: Auto Approve Pull Request
on:
  pull_request:
    branches:
      - 'master'
      - 'skip-*'

jobs:
  build:
    name: Approve PR
    runs-on: ubuntu-latest
    steps:
      - name: Fetch out code
        uses: username/my-github-repo@master
        with:
          token: ******
        env:
          BRANCH_PREFIX: "skip-*"
          PULL_REQUEST_BRANCH: "master"

Run Code Online (Sandbox Code Playgroud)

我想让工作流程仅在名为 的分支上运行skip-*

Nje*_*igi 6

我找到的解决方案是使用 if 语句来检查 head-ref 分支名称是否匹配 (starts with) skip-

这是我的解决方案:

name: Auto Approve Pull Request
on:
  pull_request:
    branches:
      - 'master'

jobs:
  build:
    name: Approve PR
    runs-on: ubuntu-latest
    if: startsWith(github.head_ref, 'skip-') == true
    steps:
      - name: Fetch out code
        uses: username/my-github-repo@master
        with:
          token: ******
        env:
          BRANCH_PREFIX: "skip-*"
          PULL_REQUEST_BRANCH: "master"
Run Code Online (Sandbox Code Playgroud)

笔记: