Skip to content

Instantly share code, notes, and snippets.

@medguywalker
Created September 7, 2022 20:13
Show Gist options
  • Save medguywalker/8816de7a98ddd437011677c239d65cf7 to your computer and use it in GitHub Desktop.
Save medguywalker/8816de7a98ddd437011677c239d65cf7 to your computer and use it in GitHub Desktop.
git clone declarative 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
// ----------------------
pipeline {
agent {
label jenkins_node
}
options {
skipDefaultCheckout(true)
}
stages {
stage('Git Clone Declarative Pipeline') {
agent {
label jenkins_node
}
steps {
script {
cleanWs()
handleCheckout()
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment