How to Get the Current Branch Within Github Actions

Github Actions is a continuous integration and delivery platform that helps automate various software development workflows.

One of the common use cases for Github Actions is to build and deploy applications based on the branch that the code changes are made to.

In this tutorial, we will learn how to get the current branch name within Github Actions.


The branch name is a crucial piece of information, as it allows us to differentiate between different environments such as development, staging, and production.

Before we dive into the code, let’s first take a look at the Github Actions environment variables that are available to us.

Github Actions Environment Variables

Github Actions provides several environment variables that you can use in your workflows.

Some of the most commonly used variables are:

  • GITHUB_ACTOR: The name of the person or app that initiated the workflow.
  • GITHUB_WORKSPACE: The default root directory for your repository.
  • GITHUB_EVENT_NAME: The name of the event that triggered the workflow.
  • GITHUB_EVENT_PATH: The path to the JSON file that contains the complete payload of the event.

Getting the Current Branch Name

To get the current branch name, we can use the following code in our workflow file:

steps:
  - name: Get Current Branch
    id: current_branch
    run: echo "Current branch is ${{ env.GITHUB_REF }}"

Here, we are using the env object and accessing the GITHUB_REF variable, which contains the full reference (e.g., refs/heads/master) of the branch that the code changes were made to.

If we only want to get the name of the branch, we can use the following code:

steps:
  - name: Get Current Branch
    id: current_branch
    run: echo "Current branch is ${{ env.GITHUB_REF#refs/heads/ }}"

In this example, we are using parameter expansion syntax to remove the refs/heads/ prefix from the GITHUB_REF variable.

Using the Current Branch Name in Workflows

Now that we have the current branch name, we can use it in our workflows to make decisions based on which branch the code changes were made to.

For example, we can deploy to different environments based on the branch name:

steps:
  - name: Get Current Branch
    id: current_branch
    run: echo "Current branch is ${{ env.GITHUB_REF#refs/heads/ }}"
  
  - name: Deploy to Staging
    if: env.GITHUB_REF == 'refs/heads/staging'
    run: echo "Deploying to Staging environment..."
  
  - name: Deploy to Production
    if: env.GITHUB_REF == 'refs/heads/master'
    run: echo "Deploying to Production environment..."

In this example, we are checking the value of the GITHUB_REF variable and deploying to either the staging or production environment based on the branch name.


Conclusion

In this tutorial, we learned how to get the current branch name within Github Actions and how to use it in our workflows to make decisions based on which branch the code changes were made to.

This is just one example of how you can use environment variables in Github Actions to automate your software development workflows.

By using the current branch name, you can easily differentiate between different environments and deploy your code to the appropriate environment.

This helps ensure that your application is deployed to the right place and helps prevent any accidental deployments to the wrong environment.

In conclusion, getting the current branch name is a useful feature in Github Actions and can greatly help streamline your continuous integration and delivery process.

If you’re interested in learning more about Github Actions and other ways to automate your software development workflows, be sure to check out the official documentation and explore the many available examples and tutorials online.