0
0
Jenkinsdevops~5 mins

Source code management setup in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Source code management setup in Jenkins helps you connect your Jenkins jobs to your code repositories. This lets Jenkins automatically get your code to build, test, and deploy it without manual copying.
When you want Jenkins to automatically pull the latest code from GitHub for building.
When you need to trigger Jenkins jobs after code changes in a repository.
When you want to keep your build process connected to your version control system.
When you want to manage multiple branches or repositories in Jenkins jobs.
When you want to track which code version was used for each build.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git url: 'https://github.com/example-user/example-repo.git', branch: 'main'
            }
        }
        stage('Build') {
            steps {
                echo 'Building the project...'
            }
        }
    }
}

This Jenkinsfile defines a pipeline with two stages: Checkout and Build.

The git step in the Checkout stage connects Jenkins to the GitHub repository URL and checks out the main branch.

This setup ensures Jenkins pulls the latest code before building.

Commands
This command updates or creates a Jenkins job using the job configuration file. It applies the source code management settings defined in the job config.
Terminal
jenkins-jobs --conf jenkins.ini update example-job.yaml
Expected OutputExpected
Job example-job updated successfully
--conf - Specifies the Jenkins server configuration file
This command triggers the Jenkins job named 'example-job' to start a build using the configured source code repository.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build example-job
Expected OutputExpected
Started build #1
-s - Specifies the Jenkins server URL
This command shows the console output of build number 1 for the job 'example-job' to verify the source code was checked out and build started.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 console example-job 1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] git Fetching changes from the remote Git repository [Pipeline] echo Building the project... [Pipeline] End of Pipeline Finished: SUCCESS
-s - Specifies the Jenkins server URL
Key Concept

If you remember nothing else from this pattern, remember: Jenkins needs a source code repository URL and branch configured to pull your code automatically before building.

Common Mistakes
Not specifying the correct repository URL in the Jenkinsfile or job config.
Jenkins cannot find the code to build and the job fails at checkout.
Always double-check the repository URL and branch name are correct and accessible.
Forgetting to trigger the Jenkins job after setting up source code management.
No build happens, so code changes are not tested or deployed.
Run the build command or configure automatic triggers to start builds on code changes.
Using an incorrect or missing Jenkins server URL in CLI commands.
CLI commands fail to connect to Jenkins and cannot control jobs.
Use the correct Jenkins server URL with the -s flag in CLI commands.
Summary
Configure the Jenkinsfile with the git repository URL and branch to enable code checkout.
Use Jenkins CLI commands to update jobs and trigger builds connected to source code.
Check build console output to verify Jenkins successfully pulled the code and started the build.