Jenkins Source Code Management with Git
Source Code Management (SCM) integration tells Jenkins where your code lives and how to access it. Git is the most widely used version control system, and Jenkins has first-class support for it through the Git plugin.
Without SCM integration, Jenkins cannot access your code. Configuring SCM correctly is one of the most important steps in setting up any Jenkins job.
How Jenkins Uses Git
When a Jenkins job runs, it performs a Git checkout — it downloads the latest code from the repository into a local folder called the workspace. Every build starts with fresh code from the branch you specify.
Git Checkout Flow
Jenkins Build Starts
|
v
Jenkins connects to Git server
(GitHub / GitLab / Bitbucket / self-hosted)
|
v
Authenticates with credentials
(Username+Password or SSH key or Token)
|
v
Clones or fetches the repository
|
v
Checks out the specified branch or commit
|
v
Code is now in the Jenkins workspace
|
v
Build steps run on the checked-out code
Configuring Git in a Freestyle Job
In the job configuration page, scroll to the Source Code Management section and select Git.
Fields to Fill In
| Field | What to Enter | Example |
|---|---|---|
| Repository URL | The full URL to your Git repo | https://github.com/myteam/myapp.git |
| Credentials | Select saved login credentials | github-token (stored in Jenkins) |
| Branch Specifier | Which branch to check out | */main or */develop or */feature/login |
Authentication Methods
Git repositories require authentication. Jenkins supports three main authentication methods. Choose the one that matches your Git server and security policy.
Method 1: Username and Password
Enter your Git username and password (or a Personal Access Token for GitHub/GitLab). This is the simplest method for HTTPS-based repositories.
GitHub HTTPS URL: https://github.com/org/repo.git Credentials type: Username with password Username: your-github-username Password: your-personal-access-token (not your actual password)
GitHub removed password authentication in 2021. Always use a Personal Access Token (PAT) instead of your account password.
Method 2: SSH Key
SSH uses a key pair — a private key stored in Jenkins and a public key registered in your Git server. This is the recommended method for production use because no password is transmitted.
Step 1: Generate an SSH key pair on your Jenkins server
ssh-keygen -t ed25519 -C "jenkins@mycompany.com"
This creates:
~/.ssh/id_ed25519 (private key — stays on Jenkins server)
~/.ssh/id_ed25519.pub (public key — goes to GitHub)
Step 2: Add the public key to GitHub
GitHub → Settings → SSH Keys → New SSH key
Paste the content of id_ed25519.pub
Step 3: Add the private key to Jenkins credentials
Manage Jenkins → Credentials → Add Credential
Kind: SSH Username with private key
Paste the private key content
Step 4: Use the SSH repository URL in the job
git@github.com:org/repo.git
Method 3: Secret Text (API Token)
Some Git servers use API tokens as standalone secrets without a username. Store the token in Jenkins as a "Secret text" credential and reference it in the job.
Branch Specifier Patterns
The branch specifier tells Jenkins which branch to build. Jenkins uses pattern matching, so you can target specific branches or groups of branches.
Pattern Meaning
*/main Only the main branch
*/develop Only the develop branch
** Every branch (build all branches)
*/feature/* Every branch starting with "feature/"
*/release-* Every branch starting with "release-"
${BRANCH_NAME} Use the environment variable (for Multibranch Pipelines)
Additional Git Behaviors
Jenkins provides advanced Git options under the "Additional Behaviours" dropdown in the SCM section. These fine-tune how Jenkins handles the repository.
| Behaviour | What It Does |
|---|---|
| Clean before checkout | Removes leftover files from the previous build |
| Checkout to a sub-directory | Places the code in a sub-folder instead of the workspace root |
| Wipe out repository and force clone | Deletes the local copy and re-clones fresh every time |
| Merge before build | Merges a target branch before building (useful for PR validation) |
| Sparse checkout paths | Only checks out specific folders (useful for large monorepos) |
Multiple Repositories in One Job
Jenkins lets you check out more than one Git repository in a single job. Click "Add Repository" in the SCM section to add a second repo. Each repository goes into its own sub-directory in the workspace.
Workspace structure with two repositories:
workspace/
├── frontend/ ← Repository 1 (UI code)
└── backend/ ← Repository 2 (API code)
The Jenkins Workspace
The workspace is the folder on the build agent where Jenkins places your code and runs all build commands. Each job gets its own workspace folder.
Default workspace path on Linux: /var/lib/jenkins/workspace/job-name/ Default workspace path on Windows: C:\Users\jenkins\workspace\job-name\ Everything Jenkins runs during a build happens inside this folder.
After a build completes, workspace files remain on disk until the next build or until you clean them manually. For jobs that produce large temporary files, enable "Clean before checkout" to reclaim disk space.
Key Points
- Git SCM configuration tells Jenkins where your code is and how to access it.
- Use Personal Access Tokens (not passwords) for HTTPS authentication with modern Git servers.
- SSH key authentication is the recommended method for production environments.
- Branch specifiers use wildcard patterns —
*/maintargets only main,**targets all branches. - The workspace is where Jenkins downloads and builds your code on the build agent.
