Skip to content

Instantly share code, notes, and snippets.

@gene-pavlovsky
Created October 18, 2023 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gene-pavlovsky/dcb93598f46ae0b512dff7f5c0088f32 to your computer and use it in GitHub Desktop.
Save gene-pavlovsky/dcb93598f46ae0b512dff7f5c0088f32 to your computer and use it in GitHub Desktop.
Jenkins Shared Library
#!/usr/bin/env groovy
def call(Map params) {
return withCredentials([ sshUserPrivateKey(credentialsId: 'git-account', keyFileVariable: 'SSH_KEY_FILE') ]) {
return withEnv([ 'GIT_SSH_COMMAND=ssh -i ${SSH_KEY_FILE}' ]) {
params.script = """#!/usr/bin/env -S bash -e
# Can be useful if you `cd` to another repo
git_config() {
if git rev-parse --is-inside-work-tree &>/dev/null; then
git config user.name 'John Johnson'
git config user.email 'john@example.com'
git config push.default current
fi
}
git_config
set -v
${params.script}
"""
return sh(params)
}
}
}
def call(String script) { call(script: script) }
/**
* @param remoteUrl
* remote repo URL
* @param targetDir
* local repo directory
* @param [tags=false]
* whether to fetch tags
* @param [branches]
* list of branches to fetch. If not specified, all the branches are fetched.
* @param [cloneMode='blobless']
* clone mode ('full', 'blobless', 'treeless', 'shallow')
*
* @see https://git-scm.com/docs/partial-clone
* @see https://github.blog/2020-12-21-get-up-to-speed-with-partial-clone-and-shallow-clone/
*/
void checkoutRepo(Map params) {
if (!params.remoteUrl || !params.targetDir) throw new IllegalArgumentException('Missing required param `remoteUrl` or `targetDir`')
final getCloneModeOptions = { switch(it) {
case 'blobless': return '--filter=blob:none' // Download all reachable commits and trees while fetching blobs on-demand. These clones are best for developers and build environments that span multiple builds.
case 'treeless': return '--filter=tree:0' // Download all reachable commits while fetching trees and blobs on-demand. These clones are best for build environments where the repository will be deleted after a single build, but you still need access to commit history.
case 'shallow': return '--depth=1' // Truncate the commit history to reduce the clone size. This creates some unexpected behavior issues, limiting which Git commands are possible. These clones also put undue stress on later fetches, so they are strongly discouraged for developer use. They are helpful for some build environments where the repository will be deleted after a single build.
case 'full': return ''
default: throw new IllegalArgumentException("Clone mode must be one of: 'blobless', 'treeless', 'shallow', 'full' - got '${it}'")
} }
final fetch = [
'git fetch',
getCloneModeOptions(params.cloneMode ?: 'blobless'),
params.tags ? '--tags' : '--no-tags',
'origin',
(params.branches ?: []).join(' ')
].findAll({ it }).join(' ')
call """
mkdir -p ${params.targetDir}
cd ${params.targetDir}
[[ \$(git rev-parse --show-toplevel 2>/dev/null) = "\$PWD" ]] || git init -b master
git_config
git remote add origin '${params.remoteUrl}' 2>/dev/null || true
$fetch
"""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment