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

@@ -0,0 +1,34 @@
package com.ncloudtech.jenkins
import hudson.model.Result
import jenkins.model.CauseOfInterruption
import jenkins.model.Jenkins
/**
* Class is for working with Jenkins
*/
class Jenkins implements Serializable {
// See https://www.baeldung.com/java-serial-version-uid
private static final long serialVersionUID = 1L
/**
* Method cancel previous builds
* @param jobName String - name of currentJob
* @param buildNumber int - build number of current job
* @param currentBuild current build information
*/
static void cancelPreviousBuilds(String jobName, int buildNumber, def currentBuild) {
def currentJob = jenkins.model.Jenkins.instance.getItemByFullName(jobName)
for (def build : currentJob.builds) {
def exec = build.getExecutor()
if (build.isBuilding() && build.number.toInteger() < buildNumber && exec != null) {
exec.interrupt(
Result.ABORTED,
new CauseOfInterruption.UserInterruption("Job aborted by #${currentBuild.number}")
)
}
}
}
}

View File

@@ -6,9 +6,18 @@ package tech.avroid.scm
class Git implements Serializable { class Git implements Serializable {
private Script script private Script script
private String creds
// Deprecated constructor
// TODO: remove from all Jenkinsfiles
Git(Script script) { Git(Script script) {
this.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 /** 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 * @param args Map - Arguments for cloning
* - urlRepo: String - HTTP URL of the repository * - urlRepo: String - HTTP URL of the repository
* - branch: String - Branch name, default "main" * - branch: String - Branch name, default "main"
@@ -58,11 +67,92 @@ class Git implements Serializable {
recursiveSubmodules: args.recursiveSubmodules, recursiveSubmodules: args.recursiveSubmodules,
parentCredentials: args.parentCredentials, parentCredentials: args.parentCredentials,
trackingSubmodules: args.trackingSubmodules]], trackingSubmodules: args.trackingSubmodules]],
userRemoteConfigs: [[url: "${args.urlRepo}"]] userRemoteConfigs: [[credentialsId: this.creds,
url: "${args.urlRepo}"]]
]) ])
if (args.listFiles) { if (args.listFiles) {
this.script.sh "ls -la ${args.path}" 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()
}
} }