DO-90/fixed_ssh_connect (#6)

*Update git class
* Add jenkins class
This commit is contained in:
Aleksandr Vodyanov
2023-10-05 09:34:12 +00:00
parent 613d06db4e
commit 12bca2e713
2 changed files with 128 additions and 4 deletions

View File

@@ -6,9 +6,18 @@ package tech.avroid.scm
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
@@ -23,7 +32,7 @@ class Git implements Serializable {
}
/**
* Method to clone a Git repository
* Method clones a Git repository
* @param args Map - Arguments for cloning
* - urlRepo: String - HTTP URL of the repository
* - branch: String - Branch name, default "main"
@@ -56,13 +65,94 @@ class Git implements Serializable {
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${args.path}"],
[$class: 'SubmoduleOption', disableSubmodules: args.disableSubmodules,
recursiveSubmodules: args.recursiveSubmodules,
parentCredentials: args.parentCredentials,
trackingSubmodules: args.trackingSubmodules]],
userRemoteConfigs: [[url: "${args.urlRepo}"]]
parentCredentials: args.parentCredentials,
trackingSubmodules: args.trackingSubmodules]],
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"
*/
public push() {
script.sshagent([this.creds]) {
script.sh """
git push
"""
}
}
/**
* 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()
}
}