CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > Print Git Branch Name in Jenkins Pipeline

Print Git Branch Name in Jenkins Pipeline

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: August 31, 2020 · 3 min read · 2 Comments
Share: X in 🔗
logo jenkins

If you’re setting up a Jenkins pipeline, you may need to know how to print the Git branch name. Being able to see the branch name can be very useful in a Jenkins pipeline as it allows you to do specific tasks based on the branch. In this post, we’ll provide a detailed guide on how to print the Git branch name in a Jenkins pipeline, including code snippets and examples.

Printing the Default Git Branch Name

To print the default Git branch name, you can use the following code:

scm.branches[0].name

This will print the branch name, such as */staging.

Printing the Actual Git Branch Name

If you want to print the actual Git branch name, you can use the following code:

scm.branches[0].name.split("/")[1]

This will print the branch name, such as staging. The split() function is used to split the string into an array, and the [1] specifies that we want to access the second element of the array (the actual branch name).

Assigning the Git Branch Name to a Variable

You may also want to assign the Git branch name to a variable. This can be done with the following code:

def branchName = scm.branches[0].name.split("/")[1]

Using the Git Branch Name in a Jenkins Pipeline

Now that we have covered the basics of printing and assigning the Git branch name in a Jenkins pipeline, let us look at a few examples of how this can be used.

Performing Different Tasks Based on the Branch Name

One potential use case is to perform different tasks based on the branch name. For instance, you may have a staging branch and a production branch, and you want to deploy to different environments based on the branch. You can use an if statement to check the branch name and execute different steps accordingly:

if (branchName == 'staging') {
  // deploy to staging environment
} else if (branchName == 'production') {
  // deploy to production environment
}

Sending Notifications Based on the Branch Name

Another use case is to send notifications based on the branch name. Let’s say you have a slack integration set up for your Jenkins pipeline, and you want to send a notification to a specific channel based on the branch. You can use a switch statement to send the notification to the appropriate channel:

switch (branchName) {
  case 'staging':
    // send notification to staging channel
    break;
  case 'production':
    // send notification to production channel
    break;
  default:
    // send notification to default channel
}

Conclusion

As you can see, being able to print and use the Git branch name in a Jenkins pipeline can be very useful in various scenarios. By following the steps and examples in this guide, you should now have a good understanding of how to print and use the Git branch name in your Jenkins pipeline.

Related Posts:

  • How To Print Git Commit Message In Jenkins Pipeline
  • Clean Up Jenkins Home Directory
  • Control Statements in Java
  • MySQL Commands for Developers
  • Scrollable ResultSet in JDBC
  • Spring Boot Async Tasks: A Complete Guide to…
Tags:devopsgitjenkins
Was this article helpful?
← Previous ArticleClean Up Jenkins Home Directory
Next Article →String Interpolation In Jenkins

2 thoughts on “Print Git Branch Name in Jenkins Pipeline”

  1. Daksh
    July 14, 2022 at 2:09 pm

    Only getting master as the ouput evertime.

    Log in to Reply
    • Coder Sathi
      July 14, 2022 at 5:31 pm

      If you are getting master as output then this is because your Jenkins is doing SCM checkout from the master branch. You have to change the branch in your Jenkins Job Configuration. I hope this will help you.
      Thanks

      Log in to Reply

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap