How to get source branch name when event is on push master?

Hi,

I am an github action step: semantic-release that I do not want to run when the source branch contains some name like “create-pull-request-by-robot” on push to master.

I would like to let other on push to master to bump the version for me except when the source branch is matching some naming scheme. Is it possible?

I am trying to use a if statement but from the documentation it looks like “github.base_ref” is only available from pull request…

Thanks,
Jason

@jasontsaitks ,

it looks like “github.base_ref” is only available from pull request…

Yes, The properties (github.base_ref and github.head_ref) are only available when the event that triggers the workflow run is related to pull request (pull_request, pull_request_review, pull_request_review_comment or pull_request_target).

In your case, as a workaround, you can try to set the workflow likes as this:

on:
  push:
    branches: 
      - master
  pull_request:
    types: [closed]
    branches: 
      - master

jobs:
  job1:
    if: |
      (github.event_name == 'push' && !contains(github.event.head_commit.message, 'Merge pull request')) ||
      (github.event_name == 'pull_request' && github.event.pull_request.merged == true &&
       github.head_ref != 'create-pull-request-by-robot' &&
       github.head_ref != 'create-pull-request-by-robot-01' &&
       github.head_ref != 'create-pull-request-by-robot-02')

Description:

  1. When pushing some commits to a branch via a PR merge from another branch, GitHub will automatically
    generate the message of the head commit for the push event likes as this format “Merge pull request #<pr_number> from . . .”.
    merge-push

  2. Once a PR is merged, GitHub will automatically close this PR. The activity type ‘closed’ of ‘pull_request’ event occurs.

  3. With above configurations, the job1 will run in the following situations:

    • When directly pushing some commits to master branch instead of the PR merge from another branch, and the message of the head commit does not contain “Merge pull request”.
      The branch ref (github.ref) is master, and the commit SHA (github.sha) is the latest commit that triggered the workflow run on master.

    • When some commits are merged from another branch into master via a PR, and the source branch (head branch, github.head_ref) of the PR is not ‘create-pull-request-by-robot’, ‘create-pull-request-by-robot-01’ and ‘create-pull-request-by-robot-02’ (if need, you can add or replace with more other branches).
      The branch ref (github.ref) is master, and the commit SHA (github.sha) is the latest commit that triggered the workflow run on master.

  4. If you want to skip a step in the job, you can add the same if statement in the step level if conditional (jobs.<job_id>.steps.if).