Bird
Raised Fist0
Jenkinsdevops~5 mins

Git plugin configuration in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
Jenkins uses the Git plugin to connect to Git repositories. This plugin helps Jenkins download your code so it can build and test it automatically.
When you want Jenkins to get the latest code from a GitHub repository before building.
When you need Jenkins to track changes in a Git repository and trigger builds automatically.
When you want to specify which branch Jenkins should build from.
When you want Jenkins to use credentials to access private Git repositories.
When you want Jenkins to clone a repository with specific options like shallow clone.
Commands
This command installs the Git plugin in Jenkins so it can connect to Git repositories.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin git
Expected OutputExpected
Installing plugin: git Downloading plugin git Success Restart Jenkins to activate the plugin.
This command creates a Jenkins job configured to use the Git plugin with the settings defined in example-job-config.xml.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 create-job example-job < example-job-config.xml
Expected OutputExpected
Created job: example-job
This command retrieves the configuration of the Jenkins job to verify the Git plugin settings.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 get-job example-job
Expected OutputExpected
<?xml version="1.0" encoding="UTF-8"?> <project> <scm class="hudson.plugins.git.GitSCM" plugin="git@4.15.0"> <configVersion>2</configVersion> <userRemoteConfigs> <hudson.plugins.git.UserRemoteConfig> <url>https://github.com/example/repo.git</url> </hudson.plugins.git.UserRemoteConfig> </userRemoteConfigs> <branches> <hudson.plugins.git.BranchSpec> <name>*/main</name> </hudson.plugins.git.BranchSpec> </branches> </scm> </project>
Key Concept

If you remember nothing else from Git plugin configuration, remember: Jenkins needs the Git plugin installed and properly set up in the job to fetch your code from the repository.

Common Mistakes
Not installing the Git plugin before configuring jobs.
Jenkins cannot connect to Git repositories without the plugin, so builds will fail.
Always install the Git plugin first using Jenkins CLI or the web interface.
Using incorrect repository URLs or branch names in the job configuration.
Jenkins will fail to clone or checkout the code, causing build errors.
Double-check the repository URL and branch name for typos and correctness.
Not providing credentials for private repositories.
Jenkins cannot access private repos without authentication, so cloning fails.
Add credentials in Jenkins and reference them in the job configuration.
Summary
Install the Git plugin in Jenkins to enable Git repository access.
Create or update Jenkins jobs to use the Git plugin with the correct repository URL and branch.
Verify the job configuration to ensure Git settings are correct before running builds.

Practice

(1/5)
1. What is the primary purpose of the Git plugin in Jenkins?
easy
A. To connect Jenkins jobs to Git repositories and fetch code
B. To build Docker images automatically
C. To deploy applications to cloud servers
D. To monitor server health and performance

Solution

  1. Step 1: Understand Jenkins Git plugin role

    The Git plugin is designed to link Jenkins jobs with Git repositories so Jenkins can get the source code.
  2. Step 2: Compare options with plugin purpose

    Only To connect Jenkins jobs to Git repositories and fetch code describes fetching code from Git repositories, which is the plugin's main job.
  3. Final Answer:

    To connect Jenkins jobs to Git repositories and fetch code -> Option A
  4. Quick Check:

    Git plugin = Connect and fetch code [OK]
Hint: Git plugin = connect Jenkins to Git repos [OK]
Common Mistakes:
  • Confusing Git plugin with deployment tools
  • Thinking Git plugin builds Docker images
  • Assuming Git plugin monitors servers
2. Which of the following is the correct syntax to checkout a Git repository in a Jenkins pipeline?
easy
A. checkout git: 'https://github.com/example/repo.git', branch: 'main'
B. git url: 'https://github.com/example/repo.git', branch: 'main'
C. git checkout 'https://github.com/example/repo.git' branch 'main'
D. checkout scm: 'https://github.com/example/repo.git', branch: 'main'

Solution

  1. Step 1: Recall Jenkins pipeline git step syntax

    The correct syntax uses the git step with named parameters like url and branch.
  2. Step 2: Validate each option

    git url: 'https://github.com/example/repo.git', branch: 'main' matches the correct syntax: git url: '...', branch: '...'. Others are incorrect or invalid syntax.
  3. Final Answer:

    git url: 'https://github.com/example/repo.git', branch: 'main' -> Option B
  4. Quick Check:

    Use git url: ..., branch: ... syntax [OK]
Hint: Use 'git url: ..., branch: ...' in pipeline [OK]
Common Mistakes:
  • Using 'checkout git:' instead of 'git url:'
  • Mixing git CLI syntax with Jenkins pipeline
  • Using 'checkout scm:' incorrectly
3. Given this Jenkins pipeline snippet:
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        git url: 'https://github.com/example/repo.git', branch: 'dev'
      }
    }
  }
}

What branch will Jenkins checkout?
medium
A. feature
B. main
C. master
D. dev

Solution

  1. Step 1: Identify branch parameter in git step

    The pipeline uses branch: 'dev' in the git step, specifying the branch to checkout.
  2. Step 2: Confirm branch checked out

    Jenkins will checkout the 'dev' branch as explicitly set in the pipeline.
  3. Final Answer:

    dev -> Option D
  4. Quick Check:

    Branch parameter sets checkout branch [OK]
Hint: Look for branch: '...' in git step [OK]
Common Mistakes:
  • Assuming default branch 'main' is used
  • Confusing 'master' with 'dev'
  • Ignoring explicit branch parameter
4. You configured a Jenkins pipeline with:
git url: 'https://github.com/example/repo.git', branch: 'main', credentialsId: 'git-creds'

But Jenkins fails to clone with authentication error. What is the likely cause?
medium
A. The credentialsId is missing or incorrect
B. The branch name is misspelled
C. The git URL is invalid
D. The Jenkins agent is offline

Solution

  1. Step 1: Analyze authentication failure cause

    Authentication errors usually mean Jenkins cannot use the provided credentials to access the repo.
  2. Step 2: Check credentialsId correctness

    If credentialsId is missing or wrong, Jenkins cannot authenticate, causing failure.
  3. Final Answer:

    The credentialsId is missing or incorrect -> Option A
  4. Quick Check:

    Authentication error = credentials issue [OK]
Hint: Check credentialsId for authentication errors [OK]
Common Mistakes:
  • Blaming branch name for auth errors
  • Ignoring credentialsId parameter
  • Assuming URL is always the problem
5. You want Jenkins to checkout a private Git repo using SSH keys stored in Jenkins credentials. Which configuration is correct in a pipeline script?
hard
A. git url: 'https://github.com/example/repo.git', credentialsId: 'ssh-key-id'
B. git url: 'git@github.com:example/repo.git', branch: 'main'
C. git url: 'git@github.com:example/repo.git', credentialsId: 'ssh-key-id'
D. git url: 'https://github.com/example/repo.git', branch: 'main'

Solution

  1. Step 1: Identify SSH URL format for private repo

    SSH URLs use the format git@github.com:owner/repo.git to connect via SSH keys.
  2. Step 2: Confirm credentialsId usage for SSH keys

    To use stored SSH keys, credentialsId must be set with the SSH key ID.
  3. Step 3: Validate options

    git url: 'git@github.com:example/repo.git', credentialsId: 'ssh-key-id' correctly uses SSH URL and credentialsId. The options using HTTPS require username/password or token credentials, not SSH keys. git url: 'git@github.com:example/repo.git', branch: 'main' misses credentialsId.
  4. Final Answer:

    git url: 'git@github.com:example/repo.git', credentialsId: 'ssh-key-id' -> Option C
  5. Quick Check:

    SSH URL + credentialsId for SSH keys [OK]
Hint: Use SSH URL with credentialsId for SSH keys [OK]
Common Mistakes:
  • Using HTTPS URL with SSH credentials
  • Omitting credentialsId for private repos
  • Confusing branch with credentials
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard