bugfix: add cleanws in pipelines (!20)

Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/20
Co-authored-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
Co-committed-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
This commit is contained in:
aleksandr.vodyanov
2024-02-27 17:56:26 +03:00
committed by Aleksandr Vodyanov
parent 7c906923e4
commit 8098ef59be
5 changed files with 162 additions and 128 deletions

View File

@@ -38,68 +38,76 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"),
''' '''
) { ) {
node(POD_LABEL) { node(POD_LABEL) {
stage('Synchronize NextCloud with Nexus') { try {
container(name: 'linux') { stage('Synchronize NextCloud with Nexus') {
NextCloud nextCloud = new NextCloud(this, env.JENKINS_OMP_NEXTCLOUD_URL, env.JENKINS_OMP_NEXTCLOUD_CREDENTIALS) container(name: 'linux') {
Nexus nexus = new Nexus(this, env.JENKINS_NEXUS_URL, env.JENKINS_NEXUS_CREDENTIALS) NextCloud nextCloud = new NextCloud(this, env.JENKINS_OMP_NEXTCLOUD_URL, env.JENKINS_OMP_NEXTCLOUD_CREDENTIALS)
def nextCloudDate Nexus nexus = new Nexus(this, env.JENKINS_NEXUS_URL, env.JENKINS_NEXUS_CREDENTIALS)
def nexusDate def nextCloudDate
// Путь до файла в нексусе, передаваемый в метод Nexus().upload для корректной работы с Nexus API (" " заменен на "%20") def nexusDate
String nexusPath = "" // Путь до файла в нексусе, передаваемый в метод Nexus().upload для корректной работы с Nexus API (" " заменен на "%20")
// Путь до файла, передаваемый в метод fileInfo, для корректной работы c NextCloud API String nexusPath = ""
String customPath = "" // Путь до файла, передаваемый в метод fileInfo, для корректной работы c NextCloud API
// Путь до файла в нексусе без служебных символов String customPath = ""
String customNexusPath = "" // Путь до файла в нексусе без служебных символов
// Дата последнего изменения файла в Нексус String customNexusPath = ""
String nexusLastModified = "" // Дата последнего изменения файла в Нексус
// Дата последнего изменения файла в НекстКлауд String nexusLastModified = ""
String nextCloudLastModified = "" // Дата последнего изменения файла в НекстКлауд
// Путь до файла на хосте String nextCloudLastModified = ""
String localPath = "" // Путь до файла на хосте
// Имя файла без пути String localPath = ""
String fileName = "" // Имя файла без пути
// Полный URL артефакта в нексус String fileName = ""
String nexusFileUrl = "" // Полный URL артефакта в нексус
// Список файлов в репозитории нексуса String nexusFileUrl = ""
String[] nexusFiles = nexus.search(nexusRepository) // Список файлов в репозитории нексуса
// Список файлов в Некст клауд String[] nexusFiles = nexus.search(nexusRepository)
List nextCloudFiles = nextCloud.search("ext-a.vodyanov/5.0.0") // Список файлов в Некст клауд
List nextCloudFiles = nextCloud.search("ext-a.vodyanov/5.0.0")
nextCloudFiles.each { nextCloudFile ->
customPath = nextCloudFile.replace("remote.php/dav/files/", "") nextCloudFiles.each { nextCloudFile ->
nexusPath = customPath.replace("/ext-a.vodyanov/", "") customPath = nextCloudFile.replace("remote.php/dav/files/", "")
fileName = nexusPath.split("/").last() nexusPath = customPath.replace("/ext-a.vodyanov/", "")
nexusPath = nexusPath.replace("/$fileName", "") fileName = nexusPath.split("/").last()
customPath = customPath.replace("%20", " ") nexusPath = nexusPath.replace("/$fileName", "")
customNexusPath = customPath.replace("/ext-a.vodyanov/", "") customPath = customPath.replace("%20", " ")
nexusFileUrl = "$env.JENKINS_NEXUS_URL/repository/$nexusRepository/$customNexusPath" customNexusPath = customPath.replace("/ext-a.vodyanov/", "")
nexusFileUrl = "$env.JENKINS_NEXUS_URL/repository/$nexusRepository/$customNexusPath"
if (nexusFiles.contains("$nexusFileUrl")) {
// Проверяем когда было последнее обновление файла на нексусе и некстклауде и сравниваем даты if (nexusFiles.contains("$nexusFileUrl")) {
nextCloudLastModified = nextCloud.fileInfo(customPath).lastModified // Проверяем когда было последнее обновление файла на нексусе и некстклауде и сравниваем даты
nexusLastModified = nexus.fileInfo(nexusRepository, "$nexusPath/$fileName").lastModified nextCloudLastModified = nextCloud.fileInfo(customPath).lastModified
nextCloudDate = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z").parse(nextCloudLastModified) nexusLastModified = nexus.fileInfo(nexusRepository, "$nexusPath/$fileName").lastModified
nexusDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse(nexusLastModified) nextCloudDate = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z").parse(nextCloudLastModified)
if (nexusDate.time < nextCloudDate.time) { nexusDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse(nexusLastModified)
localPath = nextCloud.download(nextCloudFile) if (nexusDate.time < nextCloudDate.time) {
nexus.upload([ localPath = nextCloud.download(nextCloudFile)
artifactPath: localPath, nexus.upload([
repository: nexusRepository, artifactPath: localPath,
path: nexusPath repository: nexusRepository,
]) path: nexusPath
sh "rm '${localPath}'" ])
} sh "rm '${localPath}'"
} else { }
localPath = nextCloud.download(nextCloudFile) } else {
nexus.upload([ localPath = nextCloud.download(nextCloudFile)
artifactPath: localPath, nexus.upload([
repository: nexusRepository, artifactPath: localPath,
path: nexusPath repository: nexusRepository,
]) path: nexusPath
sh "rm '${localPath}'" ])
} sh "rm '${localPath}'"
} }
} }
}
}
}
catch(err) {
echo 'ERROR: ' + err.getMessage()
currentBuild.result = 'FAILURE'
} finally {
cleanWs()
} }
} }
} }

View File

@@ -74,38 +74,46 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"),
""" """
) { ) {
node(POD_LABEL) { node(POD_LABEL) {
stage('Create branch') { try {
currentBuild.description = "Build from ${params.SOURCE_BRANCH_NAME}" stage('Create branch') {
String versionPattern = (params.BRANCH_TYPE == 'Release') ? /^\d+\.\d+.*/ : /^\d+\.\d+\.\d+.*/ currentBuild.description = "Build from ${params.SOURCE_BRANCH_NAME}"
Gitea tavroRepo = new Gitea(this, apiRepoURL, "${env.JENKINS_GIT_CREDENTIALS_HTTP}") String versionPattern = (params.BRANCH_TYPE == 'Release') ? /^\d+\.\d+.*/ : /^\d+\.\d+\.\d+.*/
branch = params.BRANCH_TYPE.toLowerCase() + params.VERSION Gitea tavroRepo = new Gitea(this, apiRepoURL, "${env.JENKINS_GIT_CREDENTIALS_HTTP}")
println "Branch name: ${branch}" 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+.*/))) { if (!params.VERSION.matches(versionPattern) || \
println('You must specify correct version, see description!') (params.BRANCH_TYPE == 'Release' && params.VERSION.matches(/^\d+\.\d+\.\d+.*/))) {
error() println('You must specify correct version, see description!')
} error()
}
Boolean result = tavroRepo.createBranch(params.SOURCE_BRANCH_NAME, branch)
Boolean result = tavroRepo.createBranch(params.SOURCE_BRANCH_NAME, branch)
if (!result) { if (!result) {
println("Branch doesn't create, maybe ${branch} already exists") println("Branch doesn't create, maybe ${branch} already exists")
error() error()
} else if (params.BRANCH_TYPE == 'Release') { } else if (params.BRANCH_TYPE == 'Release') {
stage('Create SharedLib tag') { stage('Create SharedLib tag') {
Gitea sharedLibRepo = new Gitea(this, Gitea sharedLibRepo = new Gitea(this,
"${apiRepoURL}/DevOps/jenkins-shared-lib", "${apiRepoURL}/DevOps/jenkins-shared-lib",
"${env.JENKINS_GIT_CREDENTIALS_HTTP}") "${env.JENKINS_GIT_CREDENTIALS_HTTP}")
sharedLibRepo.createTag('master', releaseBranch) sharedLibRepo.createTag('master', releaseBranch)
}
} }
} }
}
stage('Run build') { stage('Run build') {
sleep(5) sleep(5)
build(job: "TAVRO Team/TAVRO/${branch}") build(job: "TAVRO Team/TAVRO/${branch}")
}
}
catch(err) {
echo 'ERROR: ' + err.getMessage()
currentBuild.result = 'FAILURE'
} finally {
cleanWs()
} }
} }
} }

View File

@@ -32,29 +32,37 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"),
""" """
) { ) {
node(POD_LABEL) { node(POD_LABEL) {
stage('Add PR link to Eva task') { try {
Map dataJSON = readJSON text: params.DATA_JSON stage('Add PR link to Eva task') {
String prLink = dataJSON.pull_request.url Map dataJSON = readJSON text: params.DATA_JSON
String taskPattern = '[A-Z]+(-[A-Z]+)?-[0-9]+' String prLink = dataJSON.pull_request.url
// Eva Api adds name with Spaces, but returnes 500 response code String taskPattern = '[A-Z]+(-[A-Z]+)?-[0-9]+'
String prName = dataJSON.pull_request.title.replace(' ','_') // Eva Api adds name with Spaces, but returnes 500 response code
List prTasks = prName.findAll(taskPattern) String prName = dataJSON.pull_request.title.replace(' ','_')
Eva eva = new Eva(this, env.JENKINS_EVA_URL, env.JENKINS_EVA_CREDENTIALS) List prTasks = prName.findAll(taskPattern)
Eva eva = new Eva(this, env.JENKINS_EVA_URL, env.JENKINS_EVA_CREDENTIALS)
if (prTasks.isEmpty()) { if (prTasks.isEmpty()) {
prTasks = dataJSON.pull_request.body.findAll(taskPattern) prTasks = dataJSON.pull_request.body.findAll(taskPattern)
} }
prTasks.each { String taskCode -> prTasks.each { String taskCode ->
String taskId = eva.getTaskId(taskCode) String taskId = eva.getTaskId(taskCode)
if (taskId) { if (taskId) {
eva.createLink(taskId, prName, prLink) eva.createLink(taskId, prName, prLink)
println "Eva task $taskCode linked with PR $prLink" println "Eva task $taskCode linked with PR $prLink"
} else { } else {
println "Eva task $taskCode doesn't exist" println "Eva task $taskCode doesn't exist"
}
} }
} }
} }
catch(err) {
echo 'ERROR: ' + err.getMessage()
currentBuild.result = 'FAILURE'
} finally {
cleanWs()
}
} }
} }

View File

@@ -89,6 +89,8 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"),
} }
} catch (groovy.lang.MissingPropertyException e) { } catch (groovy.lang.MissingPropertyException e) {
println 'This job should run through gitea webhook:\n' + e println 'This job should run through gitea webhook:\n' + e
} finally {
cleanWs()
} }
} }
} }

View File

@@ -32,29 +32,37 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"),
''' '''
) { ) {
node(POD_LABEL) { node(POD_LABEL) {
stage('Get repository') { try {
def git = new Git(this, "$env.JENKINS_GIT_CREDENTIALS_SSH") 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", git.clone([urlRepo: "$env.JENKINS_GIT_REPOSITORY_SSH_URL/$params.PROJECT_NAME",
branch: "$params.BRANCH_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)
} }
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()
} }
} }
} }