[DO-1444] add_sync_job (!61)

Co-authored-by: Rustam Tagaev <rustam.tagaev@avroid.tech>
Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/61
Reviewed-by: Denis Patrakeev <denis.patrakeev@avroid.team>
Reviewed-by: Aleksandr Vodyanov <aleksandr.vodyanov@avroid.team>
This commit is contained in:
Rustam Tagaev
2024-12-28 12:05:06 +03:00
parent d7cc254692
commit 176a300735
3 changed files with 182 additions and 0 deletions

View File

@@ -17,3 +17,8 @@ folder('Cloud/Deploy') {
displayName('Deploy')
description('Deploy jobs')
}
folder('Cloud/Automation') {
displayName('Automation')
description('Automation jobs')
}

View File

@@ -0,0 +1,28 @@
pipelineJob('Cloud/Automation/repo-sync') {
description("Sync repo")
definition {
cpsScm {
scm {
git {
remote {
url("${JENKINS_GIT_REPOSITORY_URL}/DevOps/jenkins-pipelines.git")
credentials("${JENKINS_GIT_CREDENTIALS_HTTP}")
}
branch('master')
}
}
scriptPath('pipelines/Cloud/Automation/repo-sync.groovy')
}
}
properties {
disableConcurrentBuilds()
pipelineTriggers {
triggers {
cron {
spec('H * * * *')
}
}
}
}
}

View File

@@ -0,0 +1,149 @@
@Library('shared-lib') _
import tech.avroid.kube.PodTemplates
import tech.avroid.scm.Git
import tech.avroid.jenkins.Notifications
import tech.avroid.jenkins.Jenkins
properties([
buildDiscarder(logRotator(artifactDaysToKeepStr: '',
artifactNumToKeepStr: '10',
daysToKeepStr: '',
numToKeepStr: '10')),
disableConcurrentBuilds(),
pipelineTriggers([
cron('H * * * *')
])
])
String repoOrg = "Apps-Backend"
String gitUserName = 'jenkins'
String gitUserEmail = 'jenkins@avroid.tech'
// for push to repo add jenkins user to https://git.avroid.tech/org/Apps-Backend/teams/serviceteam-rw/repositories
List repos = [
[
projectName: 'avroid-m-docker',
cloudRepoURL: 'git@git.avroid.cloud:messenger/avroid-m-docker.git',
avroidRepoURL: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoOrg}/avroid-m-docker.git",
origin: 'avroid-m-docker',
branches: ['dev', 'master'],
cloneBranch: 'master'
],
[
projectName: 'avroid-m-backend-api',
cloudRepoURL: 'git@git.avroid.cloud:messenger/avroid-m-backend-api.git',
avroidRepoURL: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoOrg}/avroid-m-backend-api.git",
origin: 'avroid-m-backend-api',
branches: ['dev', 'master'],
cloneBranch: 'master'
],
[
projectName: 'avroid-m-backend-upload',
cloudRepoURL: 'git@git.avroid.cloud:messenger/avroid-m-backend-upload.git',
avroidRepoURL: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoOrg}/avroid-m-backend-upload.git",
origin: 'avroid-m-backend-upload',
branches: ['dev', 'master'],
cloneBranch: 'master'
],
]
Map configuration = [
vaultUrl: env.JENKINS_VAULT_URL,
vaultCredentialId: 'vault-role',
engineVersion: 2
]
List gitCreds = [
[path: 'team-devops/accounts/ldap[avroid.cloud]/service_accounts/svc.jenkins', engineVersion: 2,
secretValues:
[
[envVar: 'cloudKey', vaultKey: 'openssh.private.key'],
]
],
[path: 'team-devops/accounts/ldap/service_accounts/svc_jenkins', engineVersion: 2,
secretValues:
[
[envVar: 'avroidKey', vaultKey: 'openssh.private.key'],
]
]
]
Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH)
PodTemplates slaveTemplates = new PodTemplates(this, env.JENKINS_DOCKER_REGISTRY, ["${env.JENKINS_K8S_HARBOR_SECRET}"])
slaveTemplates.jnlp {
slaveTemplates.git {
try {
withVault([configuration: configuration, vaultSecrets: gitCreds]) {
node(POD_LABEL){
container('git'){
ansiColor('xterm') {
stage('Prepare ssh config'){
sh """#!/bin/sh
# mkdir -p /root/.ssh
echo "
Host git.avroid.cloud
Hostname git.avroid.cloud
IdentityFile ~/.ssh/cloudKey
User git
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
Host git.avroid.tech
Hostname git.avroid.tech
IdentityFile ~/.ssh/avroidKey
User git
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
" > ~/.ssh/config
echo "${avroidKey}" > ~/.ssh/avroidKey
echo "${cloudKey}" > ~/.ssh/cloudKey
chmod 600 ~/.ssh/*Key
"""
}
stage('Sync repo'){
repos.each { repo ->
dir(repo.projectName) {
git.clone([urlRepo: repo.avroidRepoURL, branch: repo.cloneBranch])
println "Start sync repo ${repo.projectName}"
git.config(gitUserName, gitUserEmail)
git.remote(origin: repo.origin, url: repo.cloudRepoURL)
repo.branches.each { branch ->
git.fetch(origin: repo.origin, branch: branch)
git.checkout(origin: 'origin', branch: branch)
git.merge(origin: repo.origin, branch: branch)
git.push(origin: 'origin', branch: branch)
}
}
}
}
}
}
}
}
} catch(err) {
errorMessage = err.getMessage()
println 'ERROR: ' + errorMessage
currentBuild.result = 'FAILURE'
String emailSubject = "${currentBuild.currentResult}. Pipeline task: ${currentBuild.fullDisplayName}"
Notifications.email(
script: this,
subject: emailSubject,
errorString: errorMessage,
recipientProviders: [],
to: ""
)
}
}
}