branch-names

tj-actionsMIT活跃维护

GitHub Action 插件,用于在各类触发事件中获取当前分支或标签名,方便 CI/CD 流水线按分支做条件判断。

:octocat: Github action to retrieve branch or tag names with support for all events.

v9.0.2发布于 2025-07-31 00:48 UTC236star30fork同步于 2026-11-07 07:32 UTC

怎么上手

由 README 提炼,执行前请确认
- uses: tj-actions/branch-names@5250492686b253f06fa55861556d1027b067aeb5 # v9
  id: branch-names
硬件要求按 README 说明

README 未说明,作为 GitHub Actions 在 Ubuntu、macOS、Windows 运行器上使用

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

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
  • 可选把分支名中的斜杠替换为连字符

容易踩的坑

需在 workflow 中给该 step 设置 id 才能引用其 outputs;引用时建议固定到 README 给出的 commit SHA 而非浮动版本;strip_tag_prefix 等输入需显式传入否则不生效

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v9.0.2
  1. v9.0.2最新正式版2025-07-31 · 2025-07-31 00:48 UTC

    What's Changed

    Full Changelog: https://github.com/tj-actions/branch-names/compare/v9...v9.0.2

  2. v92025-07-26 · 2025-07-26 07:31 UTC
    查看更新说明

    Changes in v9.0.2

    What's Changed

    Full Changelog: https://github.com/tj-actions/branch-names/compare/v9...v9.0.2


    Changes in v9.0.1

    What's Changed

    Full Changelog: https://github.com/tj-actions/branch-names/compare/v9.0.0...v9.0.1


    Changes in v9.0.0

    What's Changed

    Full Changelog: https://github.com/tj-actions/branch-names/compare/v8...v9.0.0


  3. v9.0.12025-07-26 · 2025-07-26 07:29 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/tj-actions/branch-names/compare/v9.0.0...v9.0.1

  4. v9.0.02025-07-25 · 2025-07-25 17:50 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/tj-actions/branch-names/compare/v8...v9.0.0

  5. v8.2.12025-04-11 · 2025-04-11 19:28 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/tj-actions/branch-names/compare/v8.2.0...v8.2.1

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    branch-names

    Ubuntu Mac OS Windows Public workflows that use this action.

    Codacy Badge CI Update release version.

    All Contributors

    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:

    Buy me a coffee

    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

    OUTPUTTYPEDESCRIPTION
    base_ref_branchstringThe target branch of a pull request or tag e.g main
    current_branchstringThe current branch name regardless of event_type e.g main, feature/test
    default_branchstringThe default branch name e.g main OR master
    head_ref_branchstringThe source branch of a pull request e.g feature/test
    is_defaultstringReturns "true" if the current branch is the default else "false".
    is_tagstringReturns "true" if the current branch is a tag else "false".
    ref_branchstringThe branch that triggered the workflow run. e.g 1/merge, main
    tagstringThe 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 }}
    

    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!