Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jforge/6e6e047aecb6fb484b7b8390f650512a to your computer and use it in GitHub Desktop.

Select an option

Save jforge/6e6e047aecb6fb484b7b8390f650512a to your computer and use it in GitHub Desktop.
git clone scripted pipeline
// Disclaimer: this is provided “as is” without warranty of any kind, either expressed or implied and such is to be used at your own risk.
// Dependencies
// ----------------------
// Setting up Github credential with Jenkins's Credential Manager ( https://www.youtube.com/watch?v=HSA_mZoADSw&t=40s )
// Jenkins Plugin to Clean Workspace ( https://plugins.jenkins.io/ws-cleanup/ )
// Large File Storage ( https://git-lfs.github.com/ )
// Variables
// ----------------------
github_source = {name_of_branch_or_tag}
github_org = {name_of_github_organization_or_user}
github_repo = {name_of_github_repository}
credentials_id = {github_login_saved_to_jenkins_credentials_manager}
jenkins_node = {name_of_the_jenkins_node_being_used}
// Function(s)
// ----------------------
def handleCheckout = {
def regGithubSource = ~/^v[\d|.]+/
is_tag = regGithubSource.matcher(github_source).matches()
if (is_tag == true) {
gitref = 'refs/tags/'
} else {
gitref = 'refs/heads/'
}
checkout([
$class: 'GitSCM',
branches: [[name: gitref + github_source]],
doGenerateSubmoduleConfigurations: false,
extensions: [
[
$class: 'CloneOption',
noTags: false,
shallow: false,
depth: 0,
reference: ''
],
[
$class: 'SubmoduleOption',
depth: 1,
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
shallow: true,
trackingSubmodules: false
],
[$class: 'GitLFSPull']
],
submoduleCfg: [],
userRemoteConfigs: [[ credentialsId: credentials_id, url: 'https://github.com/' + github_org + '/' + github_repo + '.git']]
]);
}
// Pipeline
// ----------------------
node (jenkins_node) {
stage('Git Clone Scripted Pipeline') {
handleCheckout()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment