Step-by-Step Process to Get Current Branch name in GitHub Actions
Step 1: Create Workflow File
- Navigate to your GitHub repository.
- Inside your repository, create a directory named .github/workflows/.
- Inside the .github/workflows/ directory, create a new file named retrieve-branch-name.yml.
Step 2: Define Workflow
Open the retrieve-branch-name.yml file for editing.
Define the workflow YAML with the following content:
name: Retrieve Branch Name
on:
push:
branches:
- '*'
jobs:
retrieve-branch:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Display Branch Name
run: echo "Current Branch: ${{ github.ref }}"
This workflow triggers on any push event to any branch, checks out the repository, and then displays the current branch name in the GitHub Actions console.
Step 3: Commit and Push
- Save the changes to the retrieve-branch-name.yml file.
- Commit the changes with an appropriate commit message.
- Push the commit to your GitHub repository.
Step 4: Monitor Workflow Execution
- Navigate to the main page of your GitHub repository.
- Click on the “Actions” tab.
- You should see the workflow listed under the “All workflows” section.
- Click on the workflow to view its execution.
Step 5: View Workflow Details
- Click on the workflow run you want to view.
- You’ll see the workflow’s progress.
- Click on each step to view its activity and outputs.
Step 6: Verify Output
- In the workflow run details, navigate to the “Display Branch Name” step.
- You should see the output of the echo command, displaying the current branch name.
How to Get Current Branch name in GitHub Actions
GitHub Actions rightfully deserves credit for the way the workflow automation in the GitHub is being redefined. No matter it has been about continuous integration, deployment or the automation of any custom operation, GitHub Actions provide a framework that is advanced enough to make these tasks easier and quicker. An example of typical authorization pattern which is used in a workflow is retrieving the name of the current branch, which can be important for optionally assessing the execution flow or providing informative outputs. This article will take us through the steps of how to retrieve the current branch name in GitHub actions without extending a typical the work time.