Files
jenkins-shared-lib/src/tech/avroid/scm/Git.groovy
aleksandr.vodyanov e1c0bd293f [DO-1175] shallow false (#38)
Reviewed-on: https://git.avroid.tech/DevOps/jenkins-shared-lib/pulls/38
Co-authored-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
Co-committed-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
2024-10-08 17:55:29 +03:00

188 lines
5.7 KiB
Groovy

package tech.avroid.scm
/**
* Class for working with Git
*/
class Git implements Serializable {
private Script script
private String creds
// Deprecated constructor
// TODO: remove from all Jenkinsfiles
Git(Script script) {
this.script = script
this.creds = script.env.JENKINS_GIT_CREDENTIALS_SSH
}
Git(Script script, String creds) {
this.script = script
this.creds = creds
}
/** get current Branch name
* @return String - branch name
*/
public String getBranch() {
if (script.env.BRANCH_NAME.startsWith('PR')) {
return "${script.env.CHANGE_BRANCH}"
}
return "${script.env.BRANCH_NAME}"
}
/**
* Method clones a Git repository
* @param args Map - Arguments for cloning
* - urlRepo: String - HTTP URL of the repository
* - branch: String - Branch name, default "main"
* - path: String - Directory to clone into, default "./"
* - disableSubmodules: Boolean - Disable submodules, default true
* - recursiveSubmodules: Boolean - Get submodules recursively, default true
* - parentCredentials: Boolean - Use parent repository credentials, default true
* - trackingSubmodules: Boolean - Use last commit in .gitmodules, default false
* - shallow: Boolean - clone with depth = 1
* - listFiles: Boolean - List cloned files, default false
*/
public clone(Map args = [:]) {
Map defaultArgs = [
branch: 'main',
disableSubmodules: false,
recursiveSubmodules: true,
parentCredentials: true,
trackingSubmodules: false,
shallow: false,
path: './',
listFiles: false
]
defaultArgs.each { k, v ->
if (args[k] == null || args[k] == '' || args[k] == []) {
args[k] = v
}
}
script.checkout([$class: 'GitSCM',
branches: [[name: "${args.branch}"]],
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${args.path}"],
[$class: 'SubmoduleOption', disableSubmodules: args.disableSubmodules,
recursiveSubmodules: args.recursiveSubmodules,
parentCredentials: args.parentCredentials,
trackingSubmodules: args.trackingSubmodules,
shallow: args.shallow]],
userRemoteConfigs: [[credentialsId: this.creds,
url: "${args.urlRepo}"]]
])
if (args.listFiles) {
this.script.sh "ls -la ${args.path}"
}
}
/**
* Method set user.name and user.mail for current repository
* @param String gitUserName
* @param String gitUserMail
*/
public config(String gitUserName = "jenkins", String gitUserMail = "jenkins@example.com") {
script.sh """
git config user.name ${gitUserName}
git config user.email ${gitUserMail}
"""
}
/**
* Method executes command "git add"
* @param List fileContents - files for add to git. If empty than add all files in repository
*/
public add(List fileContents = ["."]) {
fileContents.each { fileContent ->
script.sh """
git add ${fileContent}
"""
}
}
/**
* Method executes command "git commit -m"
* @param String Message - git commit message
*/
public commit(String message) {
script.sh """
git commit -m "${message}"
"""
}
/**
* Method executes command "git push"
* @param String branch - ветка, коммит, тег, который пушится. По умолчанию пустая строка
*/
public push(String branch = '') {
script.sshagent([this.creds]) {
script.sh """
git push ${branch}
"""
}
}
/**
* Method executes command "git checkout".
* @param String branch - git branch. If empty, equals this.getBranch()
*/
public checkout(String branch = this.getBranch()) {
script.sh """
git checkout ${branch}
"""
}
/**
* Method executes command "git log".
* @param Map args:
* - int Count: count of commits
* - String format: format of output (see git log --help)
*/
public String log(Map args = [:]) {
Map defaultArgs = [
count: -1,
format: '',
]
defaultArgs.each { k, v ->
if (args[k] == null || args[k] == '' || args[k] == []) {
args[k] = v
}
}
if (args.format != '') {
args.format = "--format=\"${args.format}\""
}
return this.script.sh(script: "git log -n $args.count $args.format", returnStdout: true).trim()
}
/**
* Method executes command "git tag".
* @param Map args:
* - String tagName: имя тега
* - String pointsAt: просмотр тегов на определенном коммите
*/
public String tag(Map args = [:]) {
Map defaultArgs = [
tagName: '',
pointsAt: '',
]
defaultArgs.each { k, v ->
if (args[k] == null || args[k] == '' || args[k] == []) {
args[k] = v
}
}
if (args.pointsAt != '') {
args.pointsAt = "--points-at ${args.pointsAt}"
}
return this.script.sh(script: "git tag $args.tagName $args.pointsAt", returnStdout: true).trim()
}
}