需在 workflow 中给该 step 设置 id 才能引用其 outputs;引用时建议固定到 README 给出的 commit SHA 而非浮动版本;strip_tag_prefix 等输入需显式传入否则不生效
branch-names
tj-actionsMIT活跃维护GitHub Action 插件,用于在各类触发事件中获取当前分支或标签名,方便 CI/CD 流水线按分支做条件判断。
:octocat: Github action to retrieve branch or tag names with support for all events.
- uses: tj-actions/branch-names@5250492686b253f06fa55861556d1027b067aeb5 # v9
id: branch-namesREADME 未说明,作为 GitHub Actions 在 Ubuntu、macOS、Windows 运行器上使用
GitHub Action,去掉 refs/heads 前缀取当前分支或标签名,兼容各类触发事件
GitHub Action 插件,用于在各类触发事件中获取当前分支或标签名,方便 CI/CD 流水线按分支做条件判断。
核心能力
- 输出去前缀的分支名,如 refs/heads/main 变 main
- 支持剥离标签前缀,如 v0.0.1 变 0.0.1
- 提供 is_default、is_tag 布尔输出便于条件判断
- 输出 PR 的 head_ref_branch 与 base_ref_branch
- 可选把分支名中的斜杠替换为连字符
容易踩的坑
- v9.0.2最新正式版2025-07-31 · 2025-07-31 00:48 UTC
What's Changed
- Upgraded to v9.0.1 by @github-actions[bot] in https://github.com/tj-actions/branch-names/pull/424
Full Changelog: https://github.com/tj-actions/branch-names/compare/v9...v9.0.2
- v92025-07-26 · 2025-07-26 07:31 UTC
查看更新说明
Changes in v9.0.2
What's Changed
- Upgraded to v9.0.1 by @github-actions[bot] in https://github.com/tj-actions/branch-names/pull/424
Full Changelog: https://github.com/tj-actions/branch-names/compare/v9...v9.0.2
Changes in v9.0.1
What's Changed
- build(deps): bump tj-actions/git-cliff from 1.5.0 to 2.0.2 by @dependabot[bot] in https://github.com/tj-actions/branch-names/pull/422
- build(deps): bump codacy/codacy-analysis-cli-action from 4.4.5 to 4.4.7 by @dependabot[bot] in https://github.com/tj-actions/branch-names/pull/421
Full Changelog: https://github.com/tj-actions/branch-names/compare/v9.0.0...v9.0.1
Changes in v9.0.0
What's Changed
- Upgraded to v8.2.1 by @github-actions[bot] in https://github.com/tj-actions/branch-names/pull/417
- chore: update action.yml by @jackton1 in https://github.com/tj-actions/branch-names/pull/418
- Updated README.md by @github-actions[bot] in https://github.com/tj-actions/branch-names/pull/419
- security: fix unsafe outputs by @jackton1 in https://github.com/tj-actions/branch-names/pull/420
Full Changelog: https://github.com/tj-actions/branch-names/compare/v8...v9.0.0
- v9.0.12025-07-26 · 2025-07-26 07:29 UTC
查看更新说明
What's Changed
- build(deps): bump tj-actions/git-cliff from 1.5.0 to 2.0.2 by @dependabot[bot] in https://github.com/tj-actions/branch-names/pull/422
- build(deps): bump codacy/codacy-analysis-cli-action from 4.4.5 to 4.4.7 by @dependabot[bot] in https://github.com/tj-actions/branch-names/pull/421
Full Changelog: https://github.com/tj-actions/branch-names/compare/v9.0.0...v9.0.1
- v9.0.02025-07-25 · 2025-07-25 17:50 UTC
查看更新说明
What's Changed
- Upgraded to v8.2.1 by @github-actions[bot] in https://github.com/tj-actions/branch-names/pull/417
- chore: update action.yml by @jackton1 in https://github.com/tj-actions/branch-names/pull/418
- Updated README.md by @github-actions[bot] in https://github.com/tj-actions/branch-names/pull/419
- security: fix unsafe outputs by @jackton1 in https://github.com/tj-actions/branch-names/pull/420
Full Changelog: https://github.com/tj-actions/branch-names/compare/v8...v9.0.0
- v8.2.12025-04-11 · 2025-04-11 19:28 UTC
查看更新说明
What's Changed
- fix: update sync-release-version.yml to sign commits by @jackton1 in https://github.com/tj-actions/branch-names/pull/416
Full Changelog: https://github.com/tj-actions/branch-names/compare/v8.2.0...v8.2.1
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
branch-names
Get a branch or tag name without the /ref/* prefix.
Table of Contents
Features
- Retrieve the current branch name without any prefix. (e.g.
'refs/heads/main'->'main') - Retrieve the current tag with an option to strip the prefix (e.g.
v0.0.1->v->0.0.1) - Detect actions triggered by non default branches
- Detect actions triggered by the default branch
- Supports all valid git branch names
Usage
...
steps:
- name: Get branch names.
id: branch-names
uses: tj-actions/branch-names@5250492686b253f06fa55861556d1027b067aeb5 # v9
- name: Running on the default branch.
if: steps.branch-names.outputs.is_default == 'true'
run: |
echo "Running on default: ${{ steps.branch-names.outputs.current_branch }}"
# Outputs: "Running on default: main"
- name: Running on a pull request branch.
if: steps.branch-names.outputs.is_default == 'false'
run: |
echo "Running on pr: ${{ steps.branch-names.outputs.current_branch }}"
# Outputs: "Running on pr: feature/test"
- name: Running on a pull request branch.
if: steps.branch-names.outputs.is_default == 'false'
run: |
echo "Base branch: ${{ steps.branch-names.outputs.base_ref_branch }}"
# Outputs: "Base branch: main"
- name: Running on any event.
run: |
echo "Default branch: ${{ steps.branch-names.outputs.default_branch }}"
# Outputs: "Default branch: main"
If you feel generous and want to show some extra appreciation:
Support this project with a :star:
Inputs
- uses: tj-actions/branch-names@5250492686b253f06fa55861556d1027b067aeb5 # v9
id: branch-names
with:
# Replace forward slashes with hyphens
# in branch names e.g. `feature/test`
# -> `feature-test`
# Type: boolean
# Default: "false"
replace_slashes_with_hyphens: ''
# The prefix that should be
# stripped from the branch e.g.
# `release/` -> with a branch
# `release/1.0` -> returns `1.0`
# Type: string
strip_branch_prefix: ''
# The prefix that should be
# stripped from the tag e.g.
# `v` -> with a tag
# `v0.0.1` -> returns `0.0.1`
# Type: string
strip_tag_prefix: ''
Outputs
| OUTPUT | TYPE | DESCRIPTION |
|---|---|---|
| base_ref_branch | string | The target branch of a pull request or tag e.g main |
| current_branch | string | The current branch name regardless of event_type e.g main, feature/test |
| default_branch | string | The default branch name e.g main OR master |
| head_ref_branch | string | The source branch of a pull request e.g feature/test |
| is_default | string | Returns "true" if the current branch is the default else "false". |
| is_tag | string | Returns "true" if the current branch is a tag else "false". |
| ref_branch | string | The branch that triggered the workflow run. e.g 1/merge, main |
| tag | string | The tag that triggered the workflow run. e.g v0.0.1, 0.0.1 |
Events
push*
on:
push:
branches:
- main
...
steps:
- name: Get branch names
id: branch-names
uses: tj-actions/branch-names@5250492686b253f06fa55861556d1027b067aeb5 # v9
- name: Current branch names
run: |
echo "${{ steps.branch-names.outputs.current_branch }}"
# Outputs: "main" the branch that triggered the push event.
- name: Running on the default branch.
if: steps.branch-names.outputs.is_default == 'true'
run: |
echo "Running on default: ${{ steps.branch-names.outputs.current_branch }}"
# Outputs: "Running on default: main".
- name: Running on the default branch (i.e non tag based branch).
if: steps.branch-names.outputs.is_tag == 'false' && steps.branch-names.outputs.is_default == 'true'
run: |
echo "Running on branch: ${{ steps.branch-names.outputs.current_branch }}"
# Outputs: "Running on branch: main".
- name: Get Ref brach name
run: |
echo "${{ steps.branch-names.outputs.ref_branch }}"
# Outputs: "main"
- name: Default branch name
run: |
echo "${{ steps.branch-names.outputs.default_branch }}"
# Outputs: "main" the default branch.
pull_request*
on:
pull_request:
branches:
- main
...
steps:
- name: Get branch names
id: branch-names
uses: tj-actions/branch-names@5250492686b253f06fa55861556d1027b067aeb5 # v9
- name: Current branch names
run: |
echo "${{ steps.branch-names.outputs.current_branch }}"
# Outputs: "feature/test" current PR branch.
- name: Running on a non tag based branch and a PR branch.
if: steps.branch-names.outputs.is_default == 'false'
run: |
echo "Running on branch: ${{ steps.branch-names.outputs.current_branch }}"
# Outputs: "Running on branch: feature/test".
- name: Running on a pull request (i.e non tag based branch).
if: steps.branch-names.outputs.is_tag == 'false' && steps.branch-names.outputs.is_default == 'false'
run: |
echo "Running on branch: ${{ steps.branch-names.outputs.current_branch }}"
# Outputs: "Running on branch: feature/test".
- name: Get Ref branch name
run: |
echo "${{ steps.branch-names.outputs.ref_branch }}"
# Outputs: "1/merge"
- name: Get Head Ref branch names (i.e The current pull request branch)
run: |
echo "${{ steps.branch-names.outputs.head_ref_branch }}"
# Outputs: "feature/test" current PR branch.
- name: Get Base Ref branch names (i.e The target of a pull request.)
run: |
echo "${{ steps.branch-names.outputs.base_ref_branch }}"
# Outputs: "main".
- name: Default branch names
run: |
echo "${{ steps.branch-names.outputs.default_branch }}"
# Outputs: "main" the default branch.
tag*
on:
push:
tags:
- '*'
...
steps:
- name: Get branch names
id: branch-names
uses: tj-actions/branch-names@5250492686b253f06fa55861556d1027b067aeb5 # v9
with:
strip_tag_prefix: v # Optionally strip the leading `v` from the tag.
- name: Running on a tag branch.
if: steps.branch-names.outputs.is_tag == 'true'
run: |
echo "Running on: ${{ steps.branch-names.outputs.tag }}"
# Outputs: "Running on: 0.0.1".
- name: Get the current tag
if: steps.branch-names.outputs.is_tag == 'true' # Replaces: startsWith(github.ref, 'refs/tags/')
run: |
echo "${{ steps.branch-names.outputs.tag }}"
# Outputs: "0.0.1"
Other supported events
See .github/workflows/test.yml for more examples.
Possible usage with actions/checkout
on:
pull_request:
branches:
- develop
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Get branch names.
id: branch-names
uses: tj-actions/branch-names@5250492686b253f06fa55861556d1027b067aeb5 # v9
- uses: actions/checkout@v4
with:
ref: ${{ steps.branch-names.outputs.head_ref_branch }}
- Free software: MIT license
Credits
This package was created with Cookiecutter.
Report Bugs
Report bugs at https://github.com/tj-actions/branch-names/issues.
If you are reporting a bug, please include:
- Your operating system name and version.
- Any details about your workflow that might be helpful in troubleshooting.
- Detailed steps to reproduce the bug.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
