From 4b96e40909a8406d1bf071feebc96218d089ed16 Mon Sep 17 00:00:00 2001 From: Rustam Tagaev Date: Tue, 26 Nov 2024 18:19:48 +0300 Subject: [PATCH] [DO-1361] add some new methods to Git and Nexus (#47) Co-authored-by: Rustam Tagaev Reviewed-on: https://git.avroid.tech/DevOps/jenkins-shared-lib/pulls/47 Reviewed-by: Denis Patrakeev Co-authored-by: Rustam Tagaev Co-committed-by: Rustam Tagaev --- src/tech/avroid/api/Nexus.groovy | 25 +++++++++++++++- src/tech/avroid/kube/PodTemplates.groovy | 38 ++++++++++++++++++++---- src/tech/avroid/scm/Git.groovy | 32 ++++++++++++++++++-- 3 files changed, 87 insertions(+), 8 deletions(-) diff --git a/src/tech/avroid/api/Nexus.groovy b/src/tech/avroid/api/Nexus.groovy index a093dbd..e06d439 100644 --- a/src/tech/avroid/api/Nexus.groovy +++ b/src/tech/avroid/api/Nexus.groovy @@ -31,6 +31,7 @@ class Nexus implements Serializable { @param type String - type of repository @return String - artifact url in Nexus */ + @Deprecated public String upload(Map args = [:], String type = "raw") { String artifactName = args.artifactName ?: args.artifactPath.split('/').last() String artifactUrl = args.artifactUrl ?: "${host}/repository/${args.repository}/${args.path}/${artifactName}" @@ -138,5 +139,27 @@ class Nexus implements Serializable { Object dataJSON = script.readJSON text: res.content return dataJSON.items[0].assets[0] - } + } + + /** + * Функция загружает артефакт в Pypi-репозиторий Nexus + @param repository String - Имя репозитория в Nexus. + @param artifact String - Путь до артефакта. + @param artifactName String - Имя артефакта которое будет отображаться в Nexus. (optional) + */ + public void uploadPypiArtifact(Map args = [:]) { + String artifactName = args.artifactName ?: args.artifact.split('/').last() + + script.httpRequest( + url: "${host}/service/rest/v1/components?repository=${args.repository}", + httpMode: 'POST', + quiet: false, + formData: [[contentType: 'APPLICATION_FORM_DATA', + name: 'pypi.asset', + fileName: artifactName, + uploadFile: args.artifact]], + authentication: credentials + ) + } + } diff --git a/src/tech/avroid/kube/PodTemplates.groovy b/src/tech/avroid/kube/PodTemplates.groovy index ae2f850..0e46848 100644 --- a/src/tech/avroid/kube/PodTemplates.groovy +++ b/src/tech/avroid/kube/PodTemplates.groovy @@ -19,6 +19,7 @@ class PodTemplates implements Serializable { effect: NoSchedule """ } + public void jnlp(body) { this.script.podTemplate( containers: [ @@ -92,7 +93,6 @@ class PodTemplates implements Serializable { public void docker(body) { this.script.podTemplate( - // serviceAccount: 'jenkins-privileged', imagePullSecrets: this.dockerCreds, containers: [ this.script.containerTemplate( @@ -104,9 +104,6 @@ class PodTemplates implements Serializable { ], ttyEnabled: true, command: '/usr/local/bin/dockerd-entrypoint.sh', - // args: """--insecure-registry=${registry} \ - // --bip=192.168.222.1/24 \ - // --storage-driver=overlay""", privileged: true, resourceRequestCpu: '500m', resourceLimitCpu: '4', @@ -118,7 +115,6 @@ class PodTemplates implements Serializable { instanceCap: 1, showRawYaml: false, volumes: [ - // this.script.secretVolume(secretName: 'docker-config', mountPath: '/home/jenkins/.docker'), this.script.emptyDirVolume(memory: false, mountPath: '/var/lib/docker'), this.script.emptyDirVolume(memory: false, mountPath: '/home/jenkins/.local'), this.script.emptyDirVolume(memory: false, mountPath: '/home/jenkins/.cache'), @@ -131,4 +127,36 @@ class PodTemplates implements Serializable { } } + public void git(imageVersion='v2.45.2', body) { + this.script.podTemplate( + containers: [ + this.script.containerTemplate( + alwaysPullImage: true, + name: 'git', + image: "${registry}/docker-hub-proxy/alpine/git:${imageVersion}", + envVars: [ + this.script.containerEnvVar(key: 'HOME', value: '/home/jenkins'), + ], + ttyEnabled: true, + command: "cat", + resourceRequestCpu: '100m', + resourceLimitCpu: '100m', + resourceRequestMemory: '32Mi', + resourceLimitMemory: '32Mi', + workingDir: '/jenkins', + ), + ], + instanceCap: 1, + showRawYaml: false, + volumes: [ + this.script.emptyDirVolume(memory: false, mountPath: '/tmp'), + ], + workspaceVolume: this.script.emptyDirWorkspaceVolume(false), + ) + + { + body.call() + } + } + } diff --git a/src/tech/avroid/scm/Git.groovy b/src/tech/avroid/scm/Git.groovy index 0493546..3dab852 100644 --- a/src/tech/avroid/scm/Git.groovy +++ b/src/tech/avroid/scm/Git.groovy @@ -43,7 +43,8 @@ class Git implements Serializable { * - trackingSubmodules: Boolean - Use last commit in .gitmodules, default false * - shallow: Boolean - clone with depth = 1 * - listFiles: Boolean - List cloned files, default false - * @return git vars: GIT_BRANCH, GIT_CHECKOUT_DIR, GIT_PREVIOUS_COMMIT, GIT_PREVIOUS_SUCCESSFUL_COMMIT, GIT_URL + * @return git vars: GIT_BRANCH, GIT_CHECKOUT_DIR, GIT_PREVIOUS_COMMIT, + * GIT_PREVIOUS_SUCCESSFUL_COMMIT, GIT_URL, GIT_COMMIT */ public Map clone(Map args = [:]) { Map defaultArgs = [ @@ -88,7 +89,7 @@ class Git implements Serializable { * @param String gitUserMail */ public config(String gitUserName = "jenkins", String gitUserMail = "jenkins@example.com") { - script.sh """ + script.sh """#!/bin/sh git config user.name ${gitUserName} git config user.email ${gitUserMail} """ @@ -187,4 +188,31 @@ class Git implements Serializable { return this.script.sh(script: "git tag $args.tagName $args.pointsAt", returnStdout: true).trim() } + + + /** + * Get commit's author name and email + * @param commit - git hash commit + * @return Map authorInfo: Autor name and email example autor = getAutor(commit: '1234567890') + * print autor.name + * print autor.email + * @required `git` command + */ + public Map getAuthor(Map args = [:]) { + this.script.sh '''#!/bin/sh + git config --global --add safe.directory '*' + ''' + Map authorInfo = [ + name: this.script.sh( + script: """#!/bin/sh + git show -s --format='%an' ${args.commit}""", + returnStdout: true).trim(), + email: this.script.sh( + script: """#!/bin/sh + git show -s --format='%ae' ${args.commit}""", + returnStdout: true).trim() + ] + + return authorInfo + } }