From 12bca2e7130d2f37c7736b59390a8f129e045622 Mon Sep 17 00:00:00 2001 From: Aleksandr Vodyanov Date: Thu, 5 Oct 2023 09:34:12 +0000 Subject: [PATCH] DO-90/fixed_ssh_connect (#6) *Update git class * Add jenkins class --- src/tech/avroid/jenkins/Jenkins.groovy | 34 +++++++++ src/tech/avroid/scm/Git.groovy | 98 ++++++++++++++++++++++++-- 2 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 src/tech/avroid/jenkins/Jenkins.groovy diff --git a/src/tech/avroid/jenkins/Jenkins.groovy b/src/tech/avroid/jenkins/Jenkins.groovy new file mode 100644 index 0000000..77074f7 --- /dev/null +++ b/src/tech/avroid/jenkins/Jenkins.groovy @@ -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}") + ) + } + } + } +} diff --git a/src/tech/avroid/scm/Git.groovy b/src/tech/avroid/scm/Git.groovy index 3f7e632..0a087c5 100644 --- a/src/tech/avroid/scm/Git.groovy +++ b/src/tech/avroid/scm/Git.groovy @@ -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() + } }