Bird
Raised Fist0
Jenkinsdevops~5 mins

Credentials for Git access in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
When Jenkins needs to get code from a Git repository, it must prove it has permission. Credentials are like keys that Jenkins uses to unlock the code safely without asking you every time.
When Jenkins runs a job that pulls code from a private Git repository.
When you want to automate builds without typing your Git username and password each time.
When you need to securely store Git access tokens or SSH keys for Jenkins pipelines.
When multiple Jenkins jobs use the same Git credentials and you want to manage them centrally.
When you want to avoid exposing your Git passwords in pipeline scripts.
Commands
This command adds Git credentials to Jenkins using an XML file. It securely stores the username and password or token Jenkins will use to access Git.
Terminal
jenkins-cli create-credentials-by-xml system::system::jenkins _ < git-credentials.xml
Expected OutputExpected
Credentials added successfully
→
system::system::jenkins - Specifies the Jenkins system scope for the credentials
→
_ - Indicates the credentials domain (default domain here)
This command lists all credentials stored in Jenkins under the system scope so you can verify your Git credentials were added.
Terminal
jenkins-cli list-credentials system::system::jenkins _
Expected OutputExpected
ID: git-credentials Description: Jenkins Git Access Type: Username with password
→
system::system::jenkins - Specifies the Jenkins system scope for the credentials
→
_ - Indicates the credentials domain (default domain here)
This command triggers a Jenkins pipeline job that uses the stored Git credentials to clone the repository without asking for username or password.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1 [Pipeline] git Cloning repository https://github.com/example/my-app.git Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: Jenkins stores Git credentials securely so your pipelines can access private repositories without exposing passwords.

Common Mistakes
Adding Git credentials directly in pipeline scripts as plain text.
This exposes sensitive information and risks leaking passwords in logs or code repositories.
Store credentials securely in Jenkins Credentials Manager and reference them by ID in your pipeline.
Not specifying the correct scope or domain when adding credentials.
Jenkins may not find the credentials during the build, causing authentication failures.
Use the correct scope like 'system::system::jenkins' and domain '_' when adding or listing credentials.
Using username and password instead of SSH keys or personal access tokens.
Passwords can expire or require frequent changes, causing pipeline failures.
Prefer SSH keys or personal access tokens for more stable and secure Git access.
Summary
Add Git credentials securely to Jenkins using the CLI or UI.
Verify credentials are stored correctly before running pipelines.
Use stored credentials in Jenkins pipelines to access private Git repositories without exposing secrets.

Practice

(1/5)
1. What is the main purpose of adding credentials in Jenkins for Git access?
easy
A. To create new Git repositories automatically
B. To speed up the Jenkins build process
C. To enable Jenkins to run on multiple nodes
D. To allow Jenkins to securely access private Git repositories

Solution

  1. Step 1: Understand Jenkins and Git integration

    Jenkins needs credentials to authenticate and access private Git repositories securely.
  2. Step 2: Identify the purpose of credentials

    Credentials store authentication details like username/password or SSH keys for Git access.
  3. Final Answer:

    To allow Jenkins to securely access private Git repositories -> Option D
  4. Quick Check:

    Credentials = Secure Git access [OK]
Hint: Credentials = secure login for Jenkins to Git [OK]
Common Mistakes:
  • Thinking credentials speed up builds
  • Confusing credentials with Jenkins nodes
  • Assuming credentials create repos
2. Which Jenkins menu path do you use to add Git credentials?
easy
A. Manage Jenkins > Configure System
B. Manage Jenkins > Manage Credentials
C. New Item > Pipeline
D. Build History > Credentials

Solution

  1. Step 1: Locate where Jenkins stores credentials

    Jenkins stores credentials under 'Manage Credentials' accessible from 'Manage Jenkins'.
  2. Step 2: Confirm the correct menu path

    The correct path to add or manage Git credentials is 'Manage Jenkins > Manage Credentials'.
  3. Final Answer:

    Manage Jenkins > Manage Credentials -> Option B
  4. Quick Check:

    Manage Credentials = Add Git credentials [OK]
Hint: Add credentials under Manage Jenkins > Manage Credentials [OK]
Common Mistakes:
  • Choosing Configure System instead of Manage Credentials
  • Selecting New Item which creates jobs
  • Looking in Build History which shows past builds
3. Given this Jenkins pipeline snippet, what does credentials('git-cred-id') represent?
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        git url: 'https://github.com/example/repo.git', credentialsId: 'git-cred-id'
      }
    }
  }
}
medium
A. The Jenkins credential ID used for Git authentication
B. The Jenkins agent label
C. The branch name to checkout
D. The ID of the Git repository

Solution

  1. Step 1: Understand the credentialsId parameter

    In the git step, credentialsId specifies which Jenkins stored credential to use for authentication.
  2. Step 2: Identify what 'git-cred-id' means

    'git-cred-id' is the ID of the credential Jenkins uses to access the Git repo securely.
  3. Final Answer:

    The Jenkins credential ID used for Git authentication -> Option A
  4. Quick Check:

    credentialsId = Jenkins Git credential ID [OK]
Hint: credentialsId links Jenkins to stored Git credentials [OK]
Common Mistakes:
  • Confusing credentialsId with repo URL
  • Thinking it is branch name
  • Assuming it is agent label
4. You added Git credentials in Jenkins but your pipeline fails with authentication errors. What is the most likely cause?
medium
A. The Jenkins agent is offline
B. The Git repository URL is public and does not require credentials
C. The credential ID used in the pipeline does not match the stored credential ID
D. The pipeline syntax is missing the agent any line

Solution

  1. Step 1: Check the error cause

    Authentication errors usually mean Jenkins cannot find or use the correct credentials.
  2. Step 2: Verify credential ID usage

    If the pipeline uses a credential ID that does not match any stored credential, authentication fails.
  3. Final Answer:

    The credential ID used in the pipeline does not match the stored credential ID -> Option C
  4. Quick Check:

    Credential ID mismatch = auth error [OK]
Hint: Match pipeline credentialId exactly with stored ID [OK]
Common Mistakes:
  • Ignoring credential ID mismatch
  • Blaming public repo for auth errors
  • Assuming agent offline causes auth errors
  • Missing agent line causes auth errors
5. You want to use SSH keys stored as Jenkins credentials to access a private Git repo in a pipeline. Which credential type should you add in Jenkins to support this?
hard
A. SSH Username with private key
B. Secret text
C. Username with password
D. Certificate

Solution

  1. Step 1: Identify credential types for Git SSH access

    SSH access requires a private key and username, so Jenkins must store these as credentials.
  2. Step 2: Choose the correct Jenkins credential type

    Jenkins provides 'SSH Username with private key' to store SSH keys for Git authentication.
  3. Final Answer:

    SSH Username with private key -> Option A
  4. Quick Check:

    SSH keys = SSH Username with private key [OK]
Hint: Use SSH Username with private key for Git SSH access [OK]
Common Mistakes:
  • Using username/password for SSH key access
  • Choosing secret text which is for tokens
  • Selecting certificate which is for SSL
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