Wrong workflow being triggered?

I have two workflows defined in my .github/actions/workflows folder: self_hosted.yml and github_hosted.yml

The trigger for self_hosted.yml is defined as follows:

name: Self-hosted Build and Test
on:
  pull_request:
    branches:
      # Run on PR's on selected branches matching the filter
      # So 'aakash/feature_x' would match
      # As would 'self-hosted/better-dimensions'
      # See details at https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet
      - self-hosted/**
      - aakash/**

The trigger for github_hosted.yml is defined as follows:

name: Build and Test
on:
  push:
    # Run on pushes to master
    branches:
      - master
  pull_request:
    # Run on pull requests to any branch except a few chosen ones we want to send to the self-hosted runner workflow
    branches-ignore:
      - self-hosted/**
      - aakash/**

The problem I’m facing is this: when I open a new pull request (to land my working branch on master), it always runs the github_hosted.yml workflow and not my self_hosted.yml workflow, even though I name my branches like so:
aakash/new_feature_1
aakash/test_feature
etc.

Doubly confusing because these branch names should match the branches-ignore filter on github_hosted.yml and not run there.

Please help.

@pacificsky,

when I open a new pull request (to land my working branch on master)

Do you mean the PR is used to merge commits from your working branch into the master branch?
If so, according to the defined triggers in your workflows, it is correct that the self_hosted.yml workflow is not triggered.

Please note that the branches filter (branches or branches-ignore) on the pull_request event is used to filter the target branch of the PR, not the source branch.

For the self_hosted.yml workflow, it is triggered only when the target branch of the PR matches the patterns “self-hosted/**” OR “aakash/**”.

For the github_hosted.yml workflow, it is triggered as long as the target branch of the PR does not match the patterns “self-hosted/**” AND “aakash/**”.

To view more details, you can see the related docs below:
https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestbranchestags

Yes, the PR is used to merge commits from my working branch into the master branch. Given that for most people, the target branch for their PR is likely master, it seems like filtering by branch may not be that useful for pull requests. It wasn’t clear to me from the documentation, so thanks for clearing that up.

Is there any way to trigger a specific workflow based on the source branch of the pull request?

@pacificsky,

Currently, we have no build-in branches filter for the source branch of the PR.

When a workflow run is triggered by the pull_request event, generally we can use the expression github.head_ref to get the name of the source branch from the github context.

So, as the workaround, you can use the job-level if conditional (jobs.<job_id>.if) to skip all the jobs in the workflow according to the name of the source branch.
a simple demo:

on: 
  pull_request:
    branches: 
      ---- branches filter patterns----

jobs:
  job1:
    if: ${{ github.head_ref == 'branch_name' }} 
    . . .
  job2:
    if: ${{ github.head_ref == 'branch_name' }}
    . . .
  jobN:
    if: ${{ github.head_ref == 'branch_name' }}
    . . .

Or use the Not equal (!=) operator in the if conditional.

if: ${{ github.head_ref != 'branch_name' }}