DO-95/omp_from_nextCloud_to_nexus (#3)
add sync both NextCloud and Nexus Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/3 Co-authored-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech> Co-committed-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
This commit is contained in:
committed by
Aleksandr Vodyanov
parent
5dcf1cd539
commit
7eeaa65b16
4
jobs-dsl/folders/omp.groovy
Normal file
4
jobs-dsl/folders/omp.groovy
Normal file
@@ -0,0 +1,4 @@
|
||||
folder('OMP') {
|
||||
displayName('OMP')
|
||||
description('Folder contains jobs for work with OMP NextCloud storage')
|
||||
}
|
||||
32
jobs-dsl/jobs/OMP/nextcloud_sync.groovy
Normal file
32
jobs-dsl/jobs/OMP/nextcloud_sync.groovy
Normal file
@@ -0,0 +1,32 @@
|
||||
pipelineJob('OMP/nextcloud-sync') {
|
||||
logRotator {
|
||||
numToKeep(10)
|
||||
artifactNumToKeep(10)
|
||||
}
|
||||
|
||||
properties {
|
||||
disableConcurrentBuilds()
|
||||
pipelineTriggers {
|
||||
triggers {
|
||||
cron {
|
||||
spec('H 9 * * *')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
definition {
|
||||
cpsScm {
|
||||
scm {
|
||||
git {
|
||||
remote {
|
||||
url("${JENKINS_GIT_REPOSITORY_URL}/devops/jenkins-pipelines.git")
|
||||
credentials("${JENKINS_GIT_CREDENTIALS_HTTP}")
|
||||
}
|
||||
branch('master')
|
||||
}
|
||||
}
|
||||
scriptPath('pipelines/OMP/nextcloud_sync.groovy')
|
||||
}
|
||||
}
|
||||
}
|
||||
105
pipelines/OMP/nextcloud_sync.groovy
Normal file
105
pipelines/OMP/nextcloud_sync.groovy
Normal file
@@ -0,0 +1,105 @@
|
||||
@Library('shared-lib') _
|
||||
|
||||
import java.text.SimpleDateFormat
|
||||
import tech.avroid.api.NextCloud
|
||||
import tech.avroid.api.Nexus
|
||||
|
||||
properties([
|
||||
buildDiscarder(logRotator(artifactNumToKeepStr: '10',
|
||||
numToKeepStr: '10')),
|
||||
disableConcurrentBuilds(),
|
||||
|
||||
pipelineTriggers([
|
||||
cron('H 9 * * *')
|
||||
])
|
||||
])
|
||||
|
||||
nexusRepository = 'OMP-cloud'
|
||||
|
||||
podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"),
|
||||
yaml: '''
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
spec:
|
||||
containers:
|
||||
- name: linux
|
||||
image: harbor.avroid.tech/devops/base-build-image:1.0
|
||||
tty: true
|
||||
resources:
|
||||
limits:
|
||||
cpu: 300m
|
||||
memory: 256Mi
|
||||
requests:
|
||||
cpu: 300m
|
||||
memory: 256Mi
|
||||
imagePullPolicy: Always
|
||||
imagePullSecrets:
|
||||
- name: harbor-registry-secret
|
||||
'''
|
||||
) {
|
||||
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}'"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,8 @@ String getBranch(Map fullJSON) {
|
||||
return fullJSON.ref
|
||||
}
|
||||
|
||||
podTemplate(yaml: '''
|
||||
podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"),
|
||||
yaml: '''
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
spec:
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
@Library('shared-lib') _
|
||||
|
||||
import tech.avroid.scm.Git
|
||||
import tech.avroid.ssh.Ssh
|
||||
|
||||
properties([
|
||||
disableConcurrentBuilds(),
|
||||
@@ -11,7 +10,8 @@ properties([
|
||||
]),
|
||||
])
|
||||
|
||||
podTemplate(yaml: '''
|
||||
podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"),
|
||||
yaml: '''
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
spec:
|
||||
@@ -33,17 +33,10 @@ podTemplate(yaml: '''
|
||||
) {
|
||||
node(POD_LABEL) {
|
||||
stage('Get repository') {
|
||||
println(params.BRANCH_NAME)
|
||||
withCredentials(bindings: [sshUserPrivateKey(credentialsId: env.JENKINS_GIT_CREDENTIALS_SSH,
|
||||
keyFileVariable: 'SSH_KEY')]) {
|
||||
def ssh = new Ssh(this, SSH_KEY, "id_ecdsa")
|
||||
def git = new Git(this)
|
||||
def git = new Git(this, "$env.JENKINS_GIT_CREDENTIALS_SSH")
|
||||
|
||||
ssh.configureSshClient()
|
||||
git.clone([urlRepo: "$env.JENKINS_GIT_REPOSITORY_SSH_URL/$params.PROJECT_NAME",
|
||||
branch: params.BRANCH_NAME])
|
||||
ssh.cleanupSshConfiguration()
|
||||
}
|
||||
branch: "$params.BRANCH_NAME"])
|
||||
}
|
||||
|
||||
stage('Run job dsl') {
|
||||
|
||||
Reference in New Issue
Block a user