Trigger Job on branch created

Hello, community.

I didn’t find documentation on how to trigger an action when I’m creating a branch.

What I want, on creating branch releases/** to trigger publish. Is it possible right now?

When you push newly created branches to Github. You can watch on push events on the branch. https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onpushpull_requestbranchestags

Hi @dmfilipenko ,

You can use the Create event to do this, mode details please reference here:
https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#create-event-create
Note: When you create a new branch, in the branch which the new branch is based on, the YAML file for the Create event workflow must have been existing, otherwise the new branch will not trigger the workflow run.

I tried

on:
  create:
    branches: 
      - releases/*

and


on:
  push:
    types: [created]
    branches:
      - 'releases/**'

But I am still seeing jobs getting triggered by non-release branches.

@shkhaliq,

The create event does not support branch filter and tag filter. Anytime you create a new branch or tag, the workflow will be triggered as long as the workflow runs on create event has been existing on the base ref of the new branch or tag.

The push event does not have any Activity types. The activity type “created” you set on the push event does not exist.

If your projects really need the feature that the create event supports branch filter and tag filter, I recommend that you can directly report a feature request here. That will allow you to directly interact with the appropriate engineering team, and make it more convenient for the engineering team to collect and categorize your suggestions.

In addition, as the workaround, you can use the expression github.ref to get the name of the created branch from the github context, then add an if conditional on each job to skip all the jobs in the workflow according to the branch name.
For example:

on: create
jobs:
  job1:
    if: ${{ contains(github.ref, 'refs/heads/releases/') }}
    . . .
  job2:
    if: ${{ contains(github.ref, 'refs/heads/releases/') }}
    . . .
  jobN:
    if: ${{ contains(github.ref, 'refs/heads/releases/') }}
    . . .
1 Like

Thanks for this @brightran this is a really helpful workaround. I’ve submitted feedback from your link as there are still a few issues with this method, which I’ll just call out for anyone else who uses this method instead:

  1. The workflow still runs but just skips, instead of not running at all. This pollutes the workflow history with lots of meaningless workflow runs and makes it very difficult to find what you’re looking for (especially for the use-case of this example which I’m also using, which is for release build workflows that actually result in a release).
  2. The filter is not as accurate as you can’t specify a complex filter pattern. I was originally attempting to use release/[0-9]+.[0-9]+.[0-9]+ as a filter pattern to target release branches with a 3-part numerical version number. However, this workaround means that this will run on all release branches. Similarly I now can’t specify to not run on release branches that end in -rc in future without a very complex if statement here, for example.

It would be great for GitHub to add first-party support for this functionality as it would improve UX and help GitHub actions be a lot more flexible for power users, which would help to achieve feature parity with other build agent competitors and make it more attractive to enterprise users with very complex requirements.