📖 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
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
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
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
Hint
Use println with string interpolation to print the branch name.
Practice
(1/5)
1. What does the Branch Specifier field in Jenkins do?
easy
A. It tells Jenkins which branch or branches to build.
B. It specifies the build timeout duration.
C. It defines the build environment variables.
D. It sets the Jenkins user permissions.
Solution
Step 1: Understand the purpose of Branch Specifier
The Branch Specifier is used to tell Jenkins which branch or branches to build from the source code repository.
Step 2: Eliminate unrelated options
Timeout, environment variables, and user permissions are unrelated to branch selection.
Final Answer:
It tells Jenkins which branch or branches to build. -> Option A
Quick Check:
Branch Specifier = Select branch to build [OK]
Hint: Branch Specifier selects branches to build in Jenkins [OK]
Common Mistakes:
Confusing branch specifier with build timeout
Thinking it sets environment variables
Assuming it controls user permissions
2. Which of the following is the correct syntax to specify the master branch in Jenkins Branch Specifier?
easy
A. refs/remotes/master
B. refs/tags/master
C. refs/heads/master
D. refs/branches/master
Solution
Step 1: Recall Git reference format for branches
Branches in Git are referenced as refs/heads/branch-name. So the master branch is refs/heads/master.
Step 2: Identify incorrect references
refs/tags/ is for tags, refs/remotes/ is for remote tracking branches, and refs/branches/ is not a valid Git ref.
Final Answer:
refs/heads/master -> Option C
Quick Check:
Branch refs start with refs/heads/ [OK]
Hint: Use refs/heads/branch-name for branch specifier [OK]
Common Mistakes:
Using refs/tags/ instead of refs/heads/
Confusing remote refs with local branch refs
Using invalid refs/branches/ prefix
3. Given the branch specifier refs/heads/feature/*, which branches will Jenkins build?
medium
A. Only the branch named exactly 'feature/*'.
B. Only the 'master' branch.
C. All branches except those starting with 'feature/'.
D. All branches starting with 'feature/', like 'feature/login' and 'feature/ui'.
Solution
Step 1: Understand wildcard usage in branch specifier
The asterisk (*) acts as a wildcard matching any characters after 'feature/'. So it matches all branches starting with 'feature/'.
Step 2: Analyze each option
Only the branch named exactly 'feature/*'. is incorrect because it treats '*' literally. All branches except those starting with 'feature/'. is opposite meaning. Only the 'master' branch. is unrelated.
Final Answer:
All branches starting with 'feature/', like 'feature/login' and 'feature/ui'. -> Option D
Quick Check:
Wildcard * matches multiple branches [OK]
Hint: Use * wildcard to match multiple branches [OK]
Common Mistakes:
Treating * as a literal character
Assuming it excludes matching branches
Confusing branch names with tags
4. You set the branch specifier to refs/heads/release but Jenkins builds no branches. What is the likely problem?
medium
A. The branch name is incorrect or does not exist in the repo.
B. The Jenkins server is offline.
C. The build script has syntax errors.
D. The workspace is full.
Solution
Step 1: Check branch existence
If Jenkins builds no branches, the specified branch refs/heads/release might not exist in the repository.
Step 2: Rule out unrelated issues
Server offline, build script errors, or workspace full would cause different errors or failures, not silent no build.
Final Answer:
The branch name is incorrect or does not exist in the repo. -> Option A
Quick Check:
Branch specifier must match existing branch [OK]
Hint: Verify branch exists before setting specifier [OK]
Common Mistakes:
Assuming Jenkins server status causes no build
Blaming build script syntax for branch selection
Ignoring branch existence check
5. You want Jenkins to build all branches except those starting with hotfix/. Which branch specifier pattern should you use?
hard
A. !refs/heads/*
B. refs/heads/*, !refs/heads/hotfix/*
C. refs/heads/*
D. refs/heads/hotfix/*
Solution
Step 1: Understand inclusion and exclusion patterns
Jenkins supports including all branches with refs/heads/* and excluding specific branches with !refs/heads/hotfix/*.
Step 2: Analyze options
refs/heads/*, !refs/heads/hotfix/* includes all branches but excludes hotfix branches. refs/heads/hotfix/* only includes hotfix branches. refs/heads/* includes all branches without exclusion. !refs/heads/* excludes all branches.
Final Answer:
refs/heads/*, !refs/heads/hotfix/* -> Option B
Quick Check:
Use ! to exclude branches in specifier [OK]
Hint: Use ! prefix to exclude branches in specifier [OK]
Common Mistakes:
Not using exclusion pattern with !
Only specifying excluded branches
Excluding all branches accidentally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.