How to trigger GitHub actions on push of current branch?

Viewed 652

I have created the following GitHub Actions workflow.

name: Testing CI

on:
  push:
    branches: [ my-branch ]

Here in branches: [my-branch] I am putting the name of the current branch.

But I want the workflow file to automatically take the name of the current branch.

Is there a parameter I can pass branches: [my-branch] here to automatically take the branch name?

2 Answers

You should not specify a branch name, then, in order for your action to run on push for all branches: on: [push]

Then you can use EthanSK/git-branch-name-action in order to get the current branch name.

That action uses process.env.GITHUB_REF,
and is the equivalent of this solution:
export GIT_BRANCH=$(echo ${GITHUB_REF:-dev} | sed s/.*\\///g).

Just use this And it will run on push of any branch:

on: [push]
Related