From 8098ef59be5dd93129c68dcc8df01679ac6496ea Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Tue, 27 Feb 2024 17:56:26 +0300 Subject: [PATCH] bugfix: add cleanws in pipelines (!20) Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/20 Co-authored-by: aleksandr.vodyanov Co-committed-by: aleksandr.vodyanov --- pipelines/OMP/nextcloud_sync.groovy | 132 ++++++++++-------- pipelines/Release-activity/tavro.groovy | 62 ++++---- .../gitea-events/eva-pr-link-webhook.groovy | 44 +++--- pipelines/gitea-events/jobs-runner.groovy | 2 + pipelines/jobs-dsl/jobs-dsl.groovy | 50 ++++--- 5 files changed, 162 insertions(+), 128 deletions(-) diff --git a/pipelines/OMP/nextcloud_sync.groovy b/pipelines/OMP/nextcloud_sync.groovy index aa66d23..4eed4fb 100644 --- a/pipelines/OMP/nextcloud_sync.groovy +++ b/pipelines/OMP/nextcloud_sync.groovy @@ -38,68 +38,76 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"), ''' ) { node(POD_LABEL) { - stage('Synchronize NextCloud with Nexus') { - container(name: 'linux') { - NextCloud nextCloud = new NextCloud(this, env.JENKINS_OMP_NEXTCLOUD_URL, env.JENKINS_OMP_NEXTCLOUD_CREDENTIALS) - Nexus nexus = new Nexus(this, env.JENKINS_NEXUS_URL, env.JENKINS_NEXUS_CREDENTIALS) - def nextCloudDate - def nexusDate - // Путь до файла в нексусе, передаваемый в метод Nexus().upload для корректной работы с Nexus API (" " заменен на "%20") - String nexusPath = "" - // Путь до файла, передаваемый в метод fileInfo, для корректной работы c NextCloud API - String customPath = "" - // Путь до файла в нексусе без служебных символов - String customNexusPath = "" - // Дата последнего изменения файла в Нексус - String nexusLastModified = "" - // Дата последнего изменения файла в НекстКлауд - String nextCloudLastModified = "" - // Путь до файла на хосте - String localPath = "" - // Имя файла без пути - String fileName = "" - // Полный URL артефакта в нексус - String nexusFileUrl = "" - // Список файлов в репозитории нексуса - String[] nexusFiles = nexus.search(nexusRepository) - // Список файлов в Некст клауд - List nextCloudFiles = nextCloud.search("ext-a.vodyanov/5.0.0") - - nextCloudFiles.each { nextCloudFile -> - customPath = nextCloudFile.replace("remote.php/dav/files/", "") - nexusPath = customPath.replace("/ext-a.vodyanov/", "") - fileName = nexusPath.split("/").last() - nexusPath = nexusPath.replace("/$fileName", "") - customPath = customPath.replace("%20", " ") - customNexusPath = customPath.replace("/ext-a.vodyanov/", "") - nexusFileUrl = "$env.JENKINS_NEXUS_URL/repository/$nexusRepository/$customNexusPath" - - if (nexusFiles.contains("$nexusFileUrl")) { - // Проверяем когда было последнее обновление файла на нексусе и некстклауде и сравниваем даты - nextCloudLastModified = nextCloud.fileInfo(customPath).lastModified - nexusLastModified = nexus.fileInfo(nexusRepository, "$nexusPath/$fileName").lastModified - nextCloudDate = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z").parse(nextCloudLastModified) - nexusDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse(nexusLastModified) - if (nexusDate.time < nextCloudDate.time) { - localPath = nextCloud.download(nextCloudFile) - nexus.upload([ - artifactPath: localPath, - repository: nexusRepository, - path: nexusPath - ]) - sh "rm '${localPath}'" - } - } else { - localPath = nextCloud.download(nextCloudFile) - nexus.upload([ - artifactPath: localPath, - repository: nexusRepository, - path: nexusPath - ]) - sh "rm '${localPath}'" - } - } - } + try { + stage('Synchronize NextCloud with Nexus') { + container(name: 'linux') { + NextCloud nextCloud = new NextCloud(this, env.JENKINS_OMP_NEXTCLOUD_URL, env.JENKINS_OMP_NEXTCLOUD_CREDENTIALS) + Nexus nexus = new Nexus(this, env.JENKINS_NEXUS_URL, env.JENKINS_NEXUS_CREDENTIALS) + def nextCloudDate + def nexusDate + // Путь до файла в нексусе, передаваемый в метод Nexus().upload для корректной работы с Nexus API (" " заменен на "%20") + String nexusPath = "" + // Путь до файла, передаваемый в метод fileInfo, для корректной работы c NextCloud API + String customPath = "" + // Путь до файла в нексусе без служебных символов + String customNexusPath = "" + // Дата последнего изменения файла в Нексус + String nexusLastModified = "" + // Дата последнего изменения файла в НекстКлауд + String nextCloudLastModified = "" + // Путь до файла на хосте + String localPath = "" + // Имя файла без пути + String fileName = "" + // Полный URL артефакта в нексус + String nexusFileUrl = "" + // Список файлов в репозитории нексуса + String[] nexusFiles = nexus.search(nexusRepository) + // Список файлов в Некст клауд + List nextCloudFiles = nextCloud.search("ext-a.vodyanov/5.0.0") + + nextCloudFiles.each { nextCloudFile -> + customPath = nextCloudFile.replace("remote.php/dav/files/", "") + nexusPath = customPath.replace("/ext-a.vodyanov/", "") + fileName = nexusPath.split("/").last() + nexusPath = nexusPath.replace("/$fileName", "") + customPath = customPath.replace("%20", " ") + customNexusPath = customPath.replace("/ext-a.vodyanov/", "") + nexusFileUrl = "$env.JENKINS_NEXUS_URL/repository/$nexusRepository/$customNexusPath" + + if (nexusFiles.contains("$nexusFileUrl")) { + // Проверяем когда было последнее обновление файла на нексусе и некстклауде и сравниваем даты + nextCloudLastModified = nextCloud.fileInfo(customPath).lastModified + nexusLastModified = nexus.fileInfo(nexusRepository, "$nexusPath/$fileName").lastModified + nextCloudDate = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z").parse(nextCloudLastModified) + nexusDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse(nexusLastModified) + if (nexusDate.time < nextCloudDate.time) { + localPath = nextCloud.download(nextCloudFile) + nexus.upload([ + artifactPath: localPath, + repository: nexusRepository, + path: nexusPath + ]) + sh "rm '${localPath}'" + } + } else { + localPath = nextCloud.download(nextCloudFile) + nexus.upload([ + artifactPath: localPath, + repository: nexusRepository, + path: nexusPath + ]) + sh "rm '${localPath}'" + } + } + } + } + } + catch(err) { + echo 'ERROR: ' + err.getMessage() + currentBuild.result = 'FAILURE' + } finally { + cleanWs() } } } diff --git a/pipelines/Release-activity/tavro.groovy b/pipelines/Release-activity/tavro.groovy index 7e212aa..3ad81be 100644 --- a/pipelines/Release-activity/tavro.groovy +++ b/pipelines/Release-activity/tavro.groovy @@ -74,38 +74,46 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"), """ ) { node(POD_LABEL) { - stage('Create branch') { - currentBuild.description = "Build from ${params.SOURCE_BRANCH_NAME}" - String versionPattern = (params.BRANCH_TYPE == 'Release') ? /^\d+\.\d+.*/ : /^\d+\.\d+\.\d+.*/ - Gitea tavroRepo = new Gitea(this, apiRepoURL, "${env.JENKINS_GIT_CREDENTIALS_HTTP}") - branch = params.BRANCH_TYPE.toLowerCase() + params.VERSION - println "Branch name: ${branch}" - - if (!params.VERSION.matches(versionPattern) || \ - (params.BRANCH_TYPE == 'Release' && params.VERSION.matches(/^\d+\.\d+\.\d+.*/))) { - println('You must specify correct version, see description!') - error() - } - - Boolean result = tavroRepo.createBranch(params.SOURCE_BRANCH_NAME, branch) + try { + stage('Create branch') { + currentBuild.description = "Build from ${params.SOURCE_BRANCH_NAME}" + String versionPattern = (params.BRANCH_TYPE == 'Release') ? /^\d+\.\d+.*/ : /^\d+\.\d+\.\d+.*/ + Gitea tavroRepo = new Gitea(this, apiRepoURL, "${env.JENKINS_GIT_CREDENTIALS_HTTP}") + branch = params.BRANCH_TYPE.toLowerCase() + params.VERSION + println "Branch name: ${branch}" + + if (!params.VERSION.matches(versionPattern) || \ + (params.BRANCH_TYPE == 'Release' && params.VERSION.matches(/^\d+\.\d+\.\d+.*/))) { + println('You must specify correct version, see description!') + error() + } + + Boolean result = tavroRepo.createBranch(params.SOURCE_BRANCH_NAME, branch) - if (!result) { - println("Branch doesn't create, maybe ${branch} already exists") - error() - } else if (params.BRANCH_TYPE == 'Release') { - stage('Create SharedLib tag') { - Gitea sharedLibRepo = new Gitea(this, - "${apiRepoURL}/DevOps/jenkins-shared-lib", - "${env.JENKINS_GIT_CREDENTIALS_HTTP}") + if (!result) { + println("Branch doesn't create, maybe ${branch} already exists") + error() + } else if (params.BRANCH_TYPE == 'Release') { + stage('Create SharedLib tag') { + Gitea sharedLibRepo = new Gitea(this, + "${apiRepoURL}/DevOps/jenkins-shared-lib", + "${env.JENKINS_GIT_CREDENTIALS_HTTP}") - sharedLibRepo.createTag('master', releaseBranch) + sharedLibRepo.createTag('master', releaseBranch) + } } } - } - stage('Run build') { - sleep(5) - build(job: "TAVRO Team/TAVRO/${branch}") + stage('Run build') { + sleep(5) + build(job: "TAVRO Team/TAVRO/${branch}") + } + } + catch(err) { + echo 'ERROR: ' + err.getMessage() + currentBuild.result = 'FAILURE' + } finally { + cleanWs() } } } diff --git a/pipelines/gitea-events/eva-pr-link-webhook.groovy b/pipelines/gitea-events/eva-pr-link-webhook.groovy index ad38255..700fcde 100644 --- a/pipelines/gitea-events/eva-pr-link-webhook.groovy +++ b/pipelines/gitea-events/eva-pr-link-webhook.groovy @@ -32,29 +32,37 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"), """ ) { node(POD_LABEL) { - stage('Add PR link to Eva task') { - Map dataJSON = readJSON text: params.DATA_JSON - String prLink = dataJSON.pull_request.url - String taskPattern = '[A-Z]+(-[A-Z]+)?-[0-9]+' - // Eva Api adds name with Spaces, but returnes 500 response code - String prName = dataJSON.pull_request.title.replace(' ','_') - List prTasks = prName.findAll(taskPattern) - Eva eva = new Eva(this, env.JENKINS_EVA_URL, env.JENKINS_EVA_CREDENTIALS) + try { + stage('Add PR link to Eva task') { + Map dataJSON = readJSON text: params.DATA_JSON + String prLink = dataJSON.pull_request.url + String taskPattern = '[A-Z]+(-[A-Z]+)?-[0-9]+' + // Eva Api adds name with Spaces, but returnes 500 response code + String prName = dataJSON.pull_request.title.replace(' ','_') + List prTasks = prName.findAll(taskPattern) + Eva eva = new Eva(this, env.JENKINS_EVA_URL, env.JENKINS_EVA_CREDENTIALS) - if (prTasks.isEmpty()) { - prTasks = dataJSON.pull_request.body.findAll(taskPattern) - } + if (prTasks.isEmpty()) { + prTasks = dataJSON.pull_request.body.findAll(taskPattern) + } - prTasks.each { String taskCode -> - String taskId = eva.getTaskId(taskCode) + prTasks.each { String taskCode -> + String taskId = eva.getTaskId(taskCode) - if (taskId) { - eva.createLink(taskId, prName, prLink) - println "Eva task $taskCode linked with PR $prLink" - } else { - println "Eva task $taskCode doesn't exist" + if (taskId) { + eva.createLink(taskId, prName, prLink) + println "Eva task $taskCode linked with PR $prLink" + } else { + println "Eva task $taskCode doesn't exist" + } } } } + catch(err) { + echo 'ERROR: ' + err.getMessage() + currentBuild.result = 'FAILURE' + } finally { + cleanWs() + } } } diff --git a/pipelines/gitea-events/jobs-runner.groovy b/pipelines/gitea-events/jobs-runner.groovy index d11fb1a..2c2a49d 100644 --- a/pipelines/gitea-events/jobs-runner.groovy +++ b/pipelines/gitea-events/jobs-runner.groovy @@ -89,6 +89,8 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"), } } catch (groovy.lang.MissingPropertyException e) { println 'This job should run through gitea webhook:\n' + e + } finally { + cleanWs() } } } diff --git a/pipelines/jobs-dsl/jobs-dsl.groovy b/pipelines/jobs-dsl/jobs-dsl.groovy index 07260cd..f65ce88 100644 --- a/pipelines/jobs-dsl/jobs-dsl.groovy +++ b/pipelines/jobs-dsl/jobs-dsl.groovy @@ -32,29 +32,37 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"), ''' ) { node(POD_LABEL) { - stage('Get repository') { - def git = new Git(this, "$env.JENKINS_GIT_CREDENTIALS_SSH") + try { + stage('Get repository') { + def git = new Git(this, "$env.JENKINS_GIT_CREDENTIALS_SSH") - git.clone([urlRepo: "$env.JENKINS_GIT_REPOSITORY_SSH_URL/$params.PROJECT_NAME", - branch: "$params.BRANCH_NAME"]) - } - - stage('Run job dsl') { - List launchJobs = [ 'Release-activity/Tavro' ] - jobDsl( - targets: 'jobs-dsl/folders/*.groovy\n' + - 'jobs-dsl/jobs/**/*.groovy\n' + - 'jobs-dsl/views/**/*.groovy', - failOnSeedCollision: true, - lookupStrategy: 'JENKINS_ROOT', - removedJobAction: 'IGNORE', - removedViewAction: 'IGNORE', - removedConfigFilesAction: 'IGNORE' - ) - - launchJobs.each { job -> - build(job: job, wait: false) + git.clone([urlRepo: "$env.JENKINS_GIT_REPOSITORY_SSH_URL/$params.PROJECT_NAME", + branch: "$params.BRANCH_NAME"]) } + + stage('Run job dsl') { + List launchJobs = [ 'Release-activity/Tavro' ] + jobDsl( + targets: 'jobs-dsl/folders/*.groovy\n' + + 'jobs-dsl/jobs/**/*.groovy\n' + + 'jobs-dsl/views/**/*.groovy', + failOnSeedCollision: true, + lookupStrategy: 'JENKINS_ROOT', + removedJobAction: 'IGNORE', + removedViewAction: 'IGNORE', + removedConfigFilesAction: 'IGNORE' + ) + + launchJobs.each { job -> + build(job: job, wait: false) + } + } + } + catch(err) { + echo 'ERROR: ' + err.getMessage() + currentBuild.result = 'FAILURE' + } finally { + cleanWs() } } }