0
0
Jenkinsdevops~5 mins

Git repository configuration in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want Jenkins to get your project code, you need to tell it where your Git repository is. This setup helps Jenkins find and use your code automatically.
When you want Jenkins to build your project from the latest code in GitHub.
When you need Jenkins to test your code every time someone adds new changes.
When you want Jenkins to deploy your app using the code stored in a Git repository.
When you want to automate your software delivery by connecting Jenkins with your Git repo.
When you want Jenkins to track different branches or tags in your Git repository.
Commands
This command creates a new Jenkins job named 'my-job' using the configuration defined in 'config.xml', which includes the Git repository settings.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 create-job my-job < config.xml
Expected OutputExpected
Created job: my-job
This command retrieves the configuration of the Jenkins job 'my-job' to verify the Git repository settings are correctly applied.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 get-job my-job
Expected OutputExpected
<?xml version="1.0" encoding="UTF-8"?> <project> <scm class="hudson.plugins.git.GitSCM" plugin="git@4.10.0"> <configVersion>2</configVersion> <userRemoteConfigs> <hudson.plugins.git.UserRemoteConfig> <url>https://github.com/example-user/example-repo.git</url> </hudson.plugins.git.UserRemoteConfig> </userRemoteConfigs> <branches> <hudson.plugins.git.BranchSpec> <name>*/main</name> </hudson.plugins.git.BranchSpec> </branches> </scm> </project>
This command starts a build of the 'my-job' Jenkins job, which will clone the Git repository and run the configured build steps.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job
Expected OutputExpected
Started build #1 for job my-job
This command shows the console output of build number 1 of the 'my-job' to check if the Git repository was cloned successfully.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 console my-job 1
Expected OutputExpected
[Pipeline] Start of Pipeline Cloning repository https://github.com/example-user/example-repo.git Checking out Revision abcdef1234567890abcdef1234567890abcdef12 (main) [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: Jenkins needs the exact Git repository URL and branch to fetch your code automatically.

Common Mistakes
Using an incorrect or incomplete Git repository URL in Jenkins job configuration.
Jenkins cannot find or clone the repository, causing build failures.
Always use the full HTTPS or SSH URL of the Git repository, including the correct protocol and domain.
Not specifying the branch name or using a wrong branch pattern.
Jenkins may build the wrong branch or fail to find the branch, leading to unexpected code versions.
Specify the correct branch pattern like '*/main' or '*/master' to match the branch you want Jenkins to build.
Not providing credentials when the Git repository is private.
Jenkins cannot access the repository and cloning fails.
Configure Jenkins with proper credentials (SSH keys or username/password) to access private repositories.
Summary
Create a Jenkins job with Git repository URL and branch configured.
Verify the job configuration to ensure the Git settings are correct.
Trigger a build to let Jenkins clone the repository and run the pipeline.
Check build logs to confirm the repository was cloned successfully.