Njeri Ngigi
Njeri Ngigi

Reputation: 575

Github Actions: Is there a way to run actions on Pull Requests opened from specific branches?

I'm trying to create an action whose workflow runs when Pull Requests from specific branches are opened against master. What I currently have runs the workflow on all Pull requests opened against master from all branches.

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"

I wanted to have the workflow to run on just branches named skip-*.

Upvotes: 4

Views: 2166

Answers (1)

Njeri Ngigi
Njeri Ngigi

Reputation: 575

The solution I found was to use an if statement to check that the head-ref branch name matches (starts with) skip-.

Here's my solution:

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"

Notes:

Upvotes: 6

Related Questions