Execute only on certain branch names during PR

I’d like for an action to run on any PR that’s opened and that has a head name like feature/**, fix/** or hotfix/**

name: something
on:
  pull_request:
    types:
    - opened
    - reopened
    - synchronize
    branches:
    - develop
jobs:
  something:
    name: Unit tests
    runs-on: ubuntu-18.04
    if: contains(github.head_ref, 'feature/**') || contains(github.head_ref, 'fix/**') || contains(github.head_ref, 'hotfix/**')
    steps:
...

However this doesn’t work, the action just sits in “Starting your workflow runs…”

There’s nothing in the documentation to suggest that contains() performs regular expression or pattern matching, so that probably why this isn’t working.

Given that all the patterns you’ve listed are prefix matches, have you considered using startsWith() instead?

Hi @constantinoschillebe ,

I add some comments to your code, please kindly check:

name: something
on:
  pull_request:
    types:
    - opened
    - reopened
    - synchronize
    branches:
    - develop #the pull requst will be triggered only when target branch is 'develop'.
jobs:
  something:
    name: Unit tests
    runs-on: ubuntu-18.04
    if: contains(github.head_ref, 'feature') || contains(github.head_ref, 'fix') || contains(github.head_ref, 'hotfix') #remove **, it doesn't accept pattern, or you can use startswith() instead
    steps:
    ...

The workflow will be triggered when PR is created from ‘feature**,fix**, hotfix**’ branch to ‘develop’ branch.

However i doubt whether you want to check the source branch, if you’d like to limit the workflow to ‘target’ branch,  on ‘pull_request.branches’ is enough.

Thanks for the suggestions @weide-zhou; I’ve updated my code

My use case is as follows: we use actions to run all our unit tests including functional tests, we use gitflow for branch naming and therefore I do not want to run unit tests on branches like “docs/**” or “chore/**”. We use these actions as required status checks (base_ref develop) before merging on all head_ref. Therefore, even if skipping the action, I need it to return some sort of success status. I’m doing that now like this:

name: something
on:
  pull_request:
    types:
    - opened
    - reopened
    - synchronize
    branches:
    - develop #the pull requst will be triggered only when target branch is 'develop'.
jobs:
  something:
    name: Unit tests
    runs-on: ubuntu-18.04
    steps:
- name: step1
if: "! contains(toJSON(github.head\_ref), 'chore/') && ! contains(toJSON(github.head\_ref), 'docs/')" ...
- name: step2
 if: "! contains(toJSON(github.head\_ref), 'chore/') && ! contains(toJSON(github.head\_ref), 'docs/')"
...
- name: step3
 if: "! contains(toJSON(github.head\_ref), 'chore/') && ! contains(toJSON(github.head\_ref), 'docs/')"
...

I tried using 

if:success()

instead of copy&pasting that if line on each step, but that doesn’t work either.

Ideally I’d like to do an early success termination on the condition below

if: contains(toJSON(github.head_ref), 'chore/') && contains(toJSON(github.head_ref), 'docs/')

Hi @constantinoschillebe ,

Glad to support! Since you’d like to return success even if skipping the action, as your code shown, probably you have to set ‘if’ expression on step level instead of job level to achieve this.

BTW, if you’d like to skip the whole workflow for PR source branch ‘docs/**’ or ‘chore/**’, you can simply remove the yaml file in these branches.

Since you’d like to return success even if skipping the action, as your code shown, probably you have to set ‘if’ expression on step level instead of job level to achieve this.

Unfortunately that doesn’t actually work; if the whole action is skipped, success is never returned to the status.

BTW, if you’d like to skip the whole workflow for PR source branch ‘docs/**’ or ‘chore/**’, you can simply remove the yaml file in these branches.

This also doesn’t work as the PR will get merged into develop.

Hi @constantinoschillebe ,

I create a repository for checking:

  1. Pull request from ‘docs/B2’ to ‘develop’ branch: https://github.com/weide-zhou/ticket22/pull/1

  2. Pull request from ‘feature/B1’ to ‘develop’ branch: https://github.com/weide-zhou/ticket22/pull/2

  3. Pull request form ‘succeedBranch’ to develop, no step is skiped: https://github.com/weide-zhou/ticket22/pull/3

Status check is passed and steps are skiped for source branch docs/** and feature/**.

Please let me know if my understanding is wrong.