### Checkout scmGit example Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitStep/help.html An example of the more powerful 'checkout scmGit' step, which is the preferred method for SCM checkout operations in Jenkins Pipelines. It supports a wider range of functionalities than the basic 'git' step. ```groovy checkout scmGit( branches: [[name: 'main']], userRemoteConfigs: [[url: 'https://git-server/user/repository.git']] ) ``` -------------------------------- ### Default Name Mapping Example Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/traits/DiscoverOtherRefsTrait/help-nameMapping.html Illustrates the default name mapping behavior when no custom mapping is provided. If a ref is 'test/*/merged', the default mapping results in 'test-@{1}'. ```text namespace_before_wildcard-@{1} ``` -------------------------------- ### Custom Name Mapping Example Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/traits/DiscoverOtherRefsTrait/help-nameMapping.html Use this mapping to define a custom name for discovered refs. The '@{1}' placeholder will be replaced by the first wildcard in the ref. For example, 'test-@{1}' will map a ref like 'test/*/merged' to 'test-merged'. ```text test-@{1} ``` -------------------------------- ### Git Username and Password Binding (Batch) Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Bind Git username and password credentials for authenticated HTTP operations in a Pipeline job using the `withCredentials` step. This example demonstrates usage within a `bat` step. ```groovy withCredentials([gitUsernamePassword(credentialsId: 'my-credentials-id', gitToolName: 'git-tool')]) { bat 'git submodule update --init --recursive' } ``` -------------------------------- ### Git Username and Password Binding (Shell) Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Bind Git username and password credentials for authenticated HTTP operations in a Pipeline job using the `withCredentials` step. This example demonstrates usage within a `sh` step. ```groovy withCredentials([gitUsernamePassword(credentialsId: 'my-credentials-id', gitToolName: 'git-tool')]) { sh 'git fetch --all' } ``` -------------------------------- ### Git Username and Password Binding (Powershell) Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Bind Git username and password credentials for authenticated HTTP operations in a Pipeline job using the `withCredentials` step. This example demonstrates usage within a `powershell` step. ```groovy withCredentials([gitUsernamePassword(credentialsId: 'my-credentials-id', gitToolName: 'git-tool')]) { powershell 'git push' } ``` -------------------------------- ### Prepare a Reference Repository Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/hudson/plugins/git/extensions/impl/SubmoduleOption/help-reference.html Initialize a bare Git repository and add remote URLs for subprojects. Then, fetch all remote branches to populate the reference repository. ```bash git init --bare git remote add SubProject1 https://gitrepo.com/subproject1 git remote add SubProject2 https://gitrepo.com/subproject2 git fetch --all ``` -------------------------------- ### Initialize and Fetch Multiple Git Repositories Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Use these commands to create a bare repository that caches commits from multiple remote repositories. This can reduce data transfer and disk usage when used as a reference repository during cloning. ```bash mkdir multirepository-cache.git cd multirepository-cache.git git init --bare git remote add parent https://github.com/jenkinsci/git-plugin git remote add child-1 https://github.com/jenkinsci/git-client-plugin git remote add child-2 https://github.com/jenkinsci/platformlabeler-plugin git fetch --all ``` -------------------------------- ### Git step with defaults Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitStep/help.html Clones from the specified repository using default settings. This is a simplified shorthand for basic checkout operations. ```groovy git 'https://github.com/jenkinsci/git-plugin.git' ``` -------------------------------- ### Wipe out repository before checkout Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Use `deleteDir()` before checkout to ensure a fully fresh workspace by removing all existing files. ```groovy deleteDir() checkout scmGit( branches: [[name: '*/master']], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/ws-cleanup-plugin.git']]) ``` -------------------------------- ### Checkout Git Repository with Defaults Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/hudson/plugins/git/GitSCM/help.html Checks out from the git plugin source repository using the https protocol, with no credentials specified, and defaults to the master branch. ```groovy checkout scmGit( userRemoteConfigs: [ [ url: 'https://github.com/jenkinsci/git-plugin' ] ] ) ``` -------------------------------- ### Disable Git Plugin NotifyCommit Access Control via System Property Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Set the `hudson.plugins.git.GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL` system property when starting Jenkins to manage access control for `notifyCommit` requests. Use `disabled-for-polling` to allow unauthenticated polling requests. ```java java -Dhudson.plugins.git.GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL=disabled-for-polling -jar jenkins.war ``` -------------------------------- ### Git step with SSH and private key credential Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitStep/help.html Clones a repository using SSH protocol and a private key credential. Ensure the credential ID matches a configured private key credential in Jenkins. ```groovy git credentialsId: 'my-private-key-credential-id', url: 'git@github.com:jenkinsci/git-client-plugin.git' ``` -------------------------------- ### Configure Git Plugin Settings with Configuration as Code Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc This YAML snippet demonstrates how to configure various global settings for the Git Plugin using the Jenkins Configuration as Code plugin. It includes options for Git hooks, repository browsing, and performance-related features. ```yaml security: gitHooks: allowedOnAgents: false allowedOnController: false unclassified: scmGit: addGitTagAction: false allowSecondFetch: false createAccountBasedOnEmail: false disableGitToolChooser: false globalConfigEmail: "jenkins-user@example.com" globalConfigName: "jenkins-user" hideCredentials: false showEntireCommitSummaryInChanges: true useExistingAccountWithSameEmail: false ``` -------------------------------- ### Checkout without fetching tags Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Use this when you need to access the repository without considering tags, saving time and disk space. ```groovy checkout scmGit( branches: [[name: 'master']], extensions: [ cloneOption(noTags: true) ], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']]) ``` -------------------------------- ### Configure Sparse Checkout Paths Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Specify directories or files to include during a sparse checkout. Requires Git version 1.7.10 or later. Useful for reducing workspace size. ```groovy checkout scmGit( branches: [[name: 'master']], extensions: [ sparseCheckout(sparseCheckoutPaths: [[path: 'src'], [path: 'Makefile']]) ], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']]) ``` -------------------------------- ### Checkout Git Repository with HTTPS and Credentials Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Checkout a Git repository using HTTPS protocol with username/password credentials. The credential ID must be pre-configured in Jenkins. ```groovy checkout scmGit( branches: [[name: 'v4.9.x']], userRemoteConfigs: [[credentialsId: 'my-username-password-id', url: 'https://github.com/jenkinsci/git-plugin.git']]) ``` -------------------------------- ### Checkout Git Repository with SSH and Private Key Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/hudson/plugins/git/GitSCM/help.html Checks out from a Git repository using SSH protocol and a private key credential. Ensure the credential ID matches your private key credential in Jenkins. ```groovy checkout changelog: false, scm: scmGit( userRemoteConfigs: [ [ credentialsId: 'my-private-key-credential-id', url: 'git@github.com:jenkinsci/git-client-plugin.git' ] ] ) ``` -------------------------------- ### Previous Git Plugin Pipeline Syntax Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc This is a previous Pipeline syntax for checking out SCM using the Git plugin, which included deprecated options like `doGenerateSubmoduleConfigurations` and `submoduleCfg`. ```groovy checkout([$class: 'GitSCM', branches: [[name: 'master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']]]) ``` -------------------------------- ### Checkout with a shallow clone Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Employ a shallow clone to reduce data traffic by requesting a limited number of commits from the tip of the branch. This can save time, data transfer, and disk space. ```groovy checkout scmGit( branches: [[name: '*/master']], extensions: [ cloneOption(shallow: true) ], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/ws-cleanup-plugin.git']]) ``` -------------------------------- ### Configure First Build Changelog in Pipeline Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Enables the generation of a changelog for the first build on a branch by using the most recent commit. This is useful when the first build has no predecessor for comparison. ```groovy checkout scmGit( branches: [[name: 'master']], extensions: [ firstBuildChangelog() ], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']]) ``` -------------------------------- ### Batch Git Username/Password Binding Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitUsernamePasswordBinding/help-credentialsId.html This snippet demonstrates setting Git credentials for use in Windows batch commands. The `credentialsId` must correspond to a valid Jenkins credential. ```groovy withCredentials(\[gitUsernamePassword(credentialsId: 'my-credentials-id', gitToolName: 'git-tool')\]) { bat 'git submodule update --init --recursive' } ``` -------------------------------- ### Checkout Git Repository with Large File Support Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Checkout a Git repository with Large File Support (LFS) enabled. This is useful for projects that manage large binary files. ```groovy checkout scmGit( branches: [[name: 'stable-3.x']], extensions: [ lfs() ], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']]) ``` -------------------------------- ### Checkout Git Repository with Specific Branch Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/hudson/plugins/git/GitSCM/help.html Checks out from a specified Git repository using the https protocol and a particular branch. No credentials are required. ```groovy checkout scmGit( branches: [[ name: 'stable-2.289' ]], userRemoteConfigs: [ [ url: 'https://github.com/jenkinsci/jenkins.git' ] ] ) ``` -------------------------------- ### Git Server Post-Receive Hook for Notifications Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Add this line to your `hooks/post-receive` file on the Git server to notify Jenkins of commits. Replace placeholders with your Jenkins URL and an access token. ```bash curl "http://yourserver/git/notifyCommit?url=&token=" ``` -------------------------------- ### Shell Git Username/Password Binding Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitUsernamePasswordBinding/help-credentialsId.html Use this snippet to set Git username and password credentials for shell commands. Ensure the `credentialsId` matches your Jenkins credential. ```groovy withCredentials(\[gitUsernamePassword(credentialsId: 'my-credentials-id', gitToolName: 'git-tool')\]) { sh 'git fetch --all' } ``` -------------------------------- ### Checkout and prune stale branches/tags Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Enable prune tags and prune branches extensions to remove remote tracking branches and tags from the local workspace if they no longer exist on the remote. ```groovy checkout scmGit( branches: [[name: 'master']], extensions: [ pruneStaleBranch(), pruneTags(true) ], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/ws-cleanup-plugin.git']]) ``` -------------------------------- ### Checkout with Changelog Disabled Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/hudson/plugins/git/GitSCM/help.html Use this to checkout from a Git repository via HTTPS without computing the changelog. Changelog computation is disabled when 'changelog: false'. ```groovy checkout changelog: false, scm: scmGit(userRemoteConfigs: \ [ \ [ url: 'https://github.com/jenkinsci/credentials-plugin' ] ]) ``` -------------------------------- ### Fetch Multiple Specific Branches Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/hudson/plugins/git/UserRemoteConfig/help-refspec.html Multiple refspecs can be specified, separated by spaces, to fetch several specific branches. ```git +refs/heads/master:refs/remotes/origin/master +refs/heads/develop:refs/remotes/origin/develop ``` -------------------------------- ### Checkout with a narrow refspec Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Use a narrow refspec to save time, data transfer, and disk space when you only need to access specific references, such as the master branch. ```groovy checkout scmGit( branches: [[name: '*/master']], extensions: [ cloneOption(honorRefspec: true) ], userRemoteConfigs: [[refspec: '+refs/heads/master:refs/remotes/origin/master', url: 'https://github.com/jenkinsci/ws-cleanup-plugin.git']]) ``` -------------------------------- ### Git step with Git protocol and polling disabled Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitStep/help.html Clones a repository using the Git protocol and disables polling. This configuration is useful for specific build scenarios where polling is not desired. ```groovy git url: 'git://github.com/jenkinsci/git-plugin.git', poll: false ``` -------------------------------- ### Git step with HTTPS and specific branch Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitStep/help.html Checks out a specific local branch from a repository using HTTPS. Note that remote branch names, SHA-1 hashes, and tag names are not supported by the 'git' step and require the 'checkout scmGit' step instead. ```groovy git branch: 'stable-2.492', url: 'https://github.com/jenkinsci/jenkins.git' ``` -------------------------------- ### Git Step with HTTPS and Polling Disabled Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitStep/help.html Checkout from a repository using HTTPS protocol with polling disabled. If poll is false, the remote repository will not be polled for changes. ```groovy git poll: false, url: 'https://github.com/jenkinsci/platformlabeler-plugin.git' ``` -------------------------------- ### PowerShell Git Username/Password Binding Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitUsernamePasswordBinding/help-credentialsId.html Configure Git username and password credentials for PowerShell commands. The `credentialsId` should reference your Jenkins stored credentials. ```groovy withCredentials(\[gitUsernamePassword(credentialsId: 'my-credentials-id', gitToolName: 'git-tool')\]) { powershell 'git push' } ``` -------------------------------- ### Clean Git Plugin Actions and Builds Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc This script iterates through Git actions associated with a run, cleans up old builds by removing actions, and updates the run. It's useful for managing build history and freeing up resources. ```groovy for (action in gitActions) { action.buildsByBranchName = new HashMap(); hudson.plugins.git.Revision r = action.getLastBuiltRevision(); if (r != null) { for (branch in r.getBranches()) { action.buildsByBranchName.put(fixNull((String) branch.getName()), action.lastBuild) } } run.actions.remove(action) run.actions.add(action) run.save(); runcounter++; } } } if (runcounter > 0) { println(" -->> cleaned: " + runcounter + " runs"); } } } if (counter > 0) { println("-- cleaned: " + counter + " builds"); } } ``` -------------------------------- ### Checkout with Polling Disabled Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/hudson/plugins/git/GitSCM/help.html Use this to checkout from a Git repository via the Git protocol without polling for changes. Polling is disabled when 'poll: false'. ```groovy checkout poll: false, scm: scmGit(userRemoteConfigs: \ [ \ [ url: 'git://git.kernel.org/pub/scm/git/git.git' ] ]) ``` -------------------------------- ### Configure Git Plugin NotifyCommit Access Control via Groovy Script Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Set the `hudson.plugins.git.GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL` value using a Groovy script in the `init.groovy.d` directory. This method is useful for managing Jenkins settings with Groovy post-initialization scripts. ```groovy hudson.plugins.git.GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL='disabled-for-polling' ``` -------------------------------- ### Checkout Git Repository with Specific Branch Source: https://github.com/jenkinsci/git-plugin/blob/master/README.adoc Checkout a specific branch of a Git repository using HTTPS. This is useful for targeting stable versions or feature branches. ```groovy checkout scmGit( branches: [[name: 'stable-3.x']], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']]) ``` -------------------------------- ### Git step with HTTPS, specific branch, and changelog disabled Source: https://github.com/jenkinsci/git-plugin/blob/master/src/main/resources/jenkins/plugins/git/GitStep/help.html Checks out a specific branch using HTTPS and disables changelog calculation. Set 'changelog' to 'false' to prevent changelog computation. ```groovy git changelog: false, url: 'https://github.com/jenkinsci/jenkins.git' ```