Pouros Dev ๐Ÿš€

How to get the current branch within GitHub Actions

April 19, 2025

๐Ÿ“‚ Categories: Programming
๐Ÿท Tags: Github Github-Actions
How to get the current branch within GitHub Actions

Automating workflows is a cornerstone of contemporary package improvement. GitHub Actions empowers builders to streamline duties, from gathering and investigating to deployment. 1 communal demand inside these workflows is accessing the sanction of the actual subdivision. Understanding the subdivision sanction permits for conditional logic, personalized builds, and situation-circumstantial deployments. This station dives into assorted strategies for retrieving the actual subdivision sanction inside your GitHub Actions workflows, offering you with the instruments to optimize your automation processes.

Utilizing the github.ref Discourse

The about easy methodology makes use of the constructed-successful github.ref discourse. This discourse adaptable incorporates the afloat ref way, specified arsenic refs/heads/chief oregon refs/heads/characteristic/fresh-characteristic. To extract conscionable the subdivision sanction, we tin employment drawstring manipulation.

For illustration, successful a Bash book:

BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|^refs/heads/||') echo "Actual subdivision: $BRANCH_NAME" 

This snippet makes use of sed to distance the refs/heads/ prefix, leaving lone the subdivision sanction. This attack is elemental and wide relevant crossed antithetic runner environments.

Leveraging the github.head_ref Discourse for Propulsion Requests

Once dealing with propulsion requests, github.ref factors to the merge subdivision. To acquire the subdivision sanction of the propulsion petition’s origin subdivision, usage github.head_ref.

See this Bash illustration:

if [[ -n "${{ github.head_ref }}" ]]; past BRANCH_NAME="${{ github.head_ref }}" other BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|^refs/heads/||') fi echo "Actual subdivision: $BRANCH_NAME" 

This codification snippet prioritizes github.head_ref once disposable (throughout a propulsion petition) and falls backmost to github.ref other. This ensures close subdivision recognition successful some eventualities.

Using GitHub Actions ToolKit

The GitHub Actions ToolKit supplies a handy JavaScript act for retrieving the subdivision sanction. This act handles the logic of extracting the subdivision sanction from github.ref oregon github.head_ref, simplifying your workflow.

- sanction: Acquire Actual Subdivision makes use of: actions/checkout@v3 with: ref: ${{ github.head_ref }} Usage caput ref if disposable - makes use of: actions/github-book@v6 id: acquire-subdivision-sanction with: book: | center.setOutput('subdivision', discourse.ref.regenerate(/^refs\/heads\//, '')) - sanction: Usage subdivision sanction tally: echo "${{ steps.acquire-subdivision-sanction.outputs.subdivision }}" 

This attack is cleaner and much readable, abstracting distant the drawstring manipulation.

Precocious Subdivision Manipulation with Daily Expressions

For much analyzable situations, daily expressions tin beryllium utilized to extract circumstantial components of the subdivision sanction. For case, if your subdivision naming normal contains a summons figure, you tin extract it utilizing regex.

BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|^refs/heads/||') TICKET_NUMBER=$(echo "$BRANCH_NAME" | grep -oE '[A-Z]+-[zero-9]+') echo "Summons Figure: $TICKET_NUMBER" 

This illustration extracts a summons figure successful the format Task-123 from the subdivision sanction. This permits for dynamic workflow customization based mostly connected subdivision naming conventions.

  • Knowing the antithetic discourse variables similar github.ref and github.head_ref is important for close subdivision retrieval.
  • Leveraging assemblage-constructed actions similar the GitHub Actions ToolKit tin simplify analyzable logic.

Selecting the correct methodology relies upon connected your workflow’s circumstantial necessities. For elemental situations, nonstop drawstring manipulation with github.ref suffices. For propulsion requests oregon much analyzable logic, the GitHub Actions ToolKit oregon daily expressions supply much sturdy options.

  1. Place the due discourse adaptable (github.ref oregon github.head_ref).
  2. Instrumentality the logic to extract the subdivision sanction (drawstring manipulation, ToolKit, oregon regex).
  3. Usage the extracted subdivision sanction successful your workflow steps.

Larn much astir GitHub Actions workflows.Outer Sources:

[Infographic Placeholder: Visualizing antithetic strategies of subdivision retrieval] Often Requested Questions

Q: What if my workflow runs connected a antithetic case than propulsion oregon pull_request?

A: The github.ref discourse volition inactive incorporate the ref of the case set off. You’ll demand to set your logic based mostly connected the anticipated ref format for that case.

Mastering subdivision sanction retrieval inside GitHub Actions unlocks a fresh flat of workflow customization and automation. By knowing the strategies outlined successful this station, you tin tailor your workflows to circumstantial branches, enabling much businesslike CI/CD pipelines and automated processes. Commencement optimizing your workflows present by implementing these strategies and exploring the linked sources for additional studying. Cheque retired our another assets connected GitHub Actions to additional heighten your automation expertise.

Question & Answer :
I’m gathering Docker photos with GitHub Actions and privation to tag photos with the subdivision sanction.

I recovered the GITHUB_REF adaptable, however it outcomes successful refs/heads/characteristic-subdivision-1 and I demand lone characteristic-subdivision-1.

I added a abstracted measure for extracting subdivision sanction from $GITHUB_HEAD_REF/$GITHUB_REFยน (pr and propulsion) and fit it to the measure output:

- sanction: Extract subdivision sanction ammunition: bash tally: echo "subdivision=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT id: extract_branch 

last that, I tin usage it successful the adjacent steps with steps.<step_id>.outputs.subdivision:

- sanction: Propulsion to ECR id: ecr makes use of: jwalton/gh-ecr-propulsion@maestro with: entree-cardinal-id: ${{ secrets and techniques.AWS_ACCESS_KEY_ID }} concealed-entree-cardinal: ${{ secrets and techniques.AWS_SECRET_ACCESS_KEY }} part: america-westbound-2 representation: eng:${{ steps.extract_branch.outputs.subdivision }} 

ยน $GITHUB_HEAD_REF connected pull_request (pr) and $GITHUB_REF connected propulsion. Statement:

| Adaptable | Statement | |---|---| | `GITHUB_HEAD_REF` | The caput ref oregon origin subdivision of the propulsion petition successful a workflow tally. This place is lone fit once the case that triggers a workflow tally is both `pull_request` oregon `pull_request_target`. For illustration, `characteristic-subdivision-1`. ([origin](https://github.com/github/docs/blob/main/content/actions/learn-github-actions/variables.md)) | | `GITHUB_REF` | The full-shaped ref of the subdivision oregon tag that triggered the workflow tally. For workflows triggered by `propulsion`, this is the subdivision oregon tag ref that was pushed. For workflows triggered by `pull_request`, this is the propulsion petition merge subdivision. For workflows triggered by `merchandise`, this is the merchandise tag created. For another triggers, this is the subdivision oregon tag ref that triggered the workflow tally. This is lone fit if a subdivision oregon tag is disposable for the case kind. The ref fixed is full-fashioned, that means that for branches the format is `refs/heads/`, for propulsion requests it is `refs/propulsion//merge`, and for tags it is `refs/tags/`. For illustration, `refs/heads/characteristic-subdivision-1`. ([origin](https://github.com/github/docs/blob/main/data/reusables/actions/ref-description.md)) |
Afloat statement of these and each another [Default situation variables - Larn Gihtub Actions](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables) ([archived transcript](https://web.archive.org/web/20230524111325/https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables)).

Wanting for the Microsoft Github Act Discourse named github? Seat the reply by ysfaran and/oregon the reply by Dusan Plavak.