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) {
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()
}
}
}