0
0
Jenkinsdevops~15 mins

Branch selection and branch specifier in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Branch selection and branch specifier
📖 Scenario: You are setting up a Jenkins pipeline to build code from a specific branch in a Git repository. You want to practice selecting branches using branch specifiers in your pipeline configuration.
🎯 Goal: Build a Jenkins pipeline script that checks out code from a Git repository using a branch specifier to select the feature/login branch.
📋 What You'll Learn
Create a variable to hold the Git repository URL.
Create a variable to hold the branch specifier for the feature/login branch.
Use the checkout step with the Git plugin to clone the repository using the branch specifier.
Print the branch name being checked out.
💡 Why This Matters
🌍 Real World
In real projects, Jenkins pipelines often need to build code from specific branches like feature branches or release branches. Using branch specifiers helps automate this selection.
💼 Career
Understanding branch selection in Jenkins is essential for DevOps engineers to automate builds and deployments accurately based on branch workflows.
Progress0 / 4 steps
1
Set up Git repository URL
Create a variable called gitRepoUrl and set it to the string https://github.com/example/repo.git.
Jenkins
Need a hint?

Use def to declare the variable and assign the exact URL string.

2
Add branch specifier variable
Create a variable called branchName and set it to the string feature/login.
Jenkins
Need a hint?

Use def to declare the variable and assign the exact branch name string.

3
Checkout the Git repository using branch specifier
Use the checkout step to clone the repository from gitRepoUrl using the branch specifier branchName. Use the syntax: branches: [[name: branchName]], userRemoteConfigs: [[url: gitRepoUrl]] with $class: 'GitSCM'.
Jenkins
Need a hint?

Use the checkout step with $class: 'GitSCM' and specify branches and userRemoteConfigs as lists of maps.

4
Print the branch being checked out
Write a println statement to display the text Checking out branch: feature/login using the variable branchName.
Jenkins
Need a hint?

Use println with string interpolation to print the branch name.