diff --git a/job-dsls/jobs/prod/prod_sync_branches.groovy b/job-dsls/jobs/prod/prod_sync_branches.groovy new file mode 100644 index 00000000..fb354615 --- /dev/null +++ b/job-dsls/jobs/prod/prod_sync_branches.groovy @@ -0,0 +1,32 @@ +/** +* Sync branches of a repository from different GH organizations +*/ +def scriptTemplate = this.getClass().getResource("job-scripts/prod_sync_branches.jenkinsfile").text +def parsedScript = scriptTemplate.replaceAll(/<%=\s*(\w+)\s*%>/) { config[it[1]] ?: '' } + +def folderPath = "PROD" +folder(folderPath) + + +pipelineJob("${folderPath}/sync-branches") { + description('This job sync branches of a repository from different GitHub organizations.') + + parameters { + stringParam('REPOSITORIES', '', 'List of repositories separated by comma, i.e. kogito-runtimes,kogito-apps') + stringParam('BRANCH', '', 'The branch that is going to be synced, i.e. 8.45.x') + stringParam('SOURCE_ORGANIZATION', 'apache', 'The GitHub organization where the original branch is') + stringParam('TARGET_ORGANIZATION', 'kiegroup', 'The GitHub organization where the branch will be pushed') + } + + logRotator { + numToKeep(20) + } + + definition { + cps { + script(parsedScript) + sandbox() + } + } + +} diff --git a/job-dsls/src/main/resources/job-scripts/prod_sync_branches.jenkinsfile b/job-dsls/src/main/resources/job-scripts/prod_sync_branches.jenkinsfile new file mode 100644 index 00000000..654838a7 --- /dev/null +++ b/job-dsls/src/main/resources/job-scripts/prod_sync_branches.jenkinsfile @@ -0,0 +1,62 @@ +@Library('jenkins-pipeline-shared-libraries')_ + +pipeline { + agent { + label 'kie-rhel8 && !built-in' + } + + stages { + stage('Clean workspace') { + steps { + cleanWs() + } + } + + stage('Print variables') { + steps { + println "[INFO] REPOSITORIES: ${REPOSITORIES}" + println "[INFO] BRANCH: ${BRANCH}" + println "[INFO] SOURCE_ORGANIZATION: ${SOURCE_ORGANIZATION}" + println "[INFO] TARGET_ORGANIZATION: ${TARGET_ORGANIZATION}" + } + } + + stage('Sync branch') { + steps { + script { + def repositoriesList = REPOSITORIES.split(',').collect{it as String} + for (repository in repositoriesList) { + cloneSourceRepository(repository) + addTargetAsRemote(repository) + pushBranch(repository) + } + } + } + } + } + + post { + always { + cleanWs() + } + } +} + +def cloneSourceRepository(String repositoryName) { + def repository = !SOURCE_ORGANIZATION.equals("apache") ? repositoryName : "incubator-kie-${repositoryName}" // remove when incubator is removed from the repos name + def repositoryUrl = "https://github.com/${SOURCE_ORGANIZATION}/${repository}.git" + sh "git clone --branch ${BRANCH} ${repositoryUrl} ${repositoryName}" +} + +def addTargetAsRemote(String repositoryName) { + dir (repositoryName) { + def remoteUrl = "https://github.com/${TARGET_ORGANIZATION}/${repositoryName}.git" + githubscm.addRemote(TARGET_ORGANIZATION, remoteUrl) + } +} + +def pushBranch(String repositoryName) { + dir (repositoryName) { + githubscm.pushObject(TARGET_ORGANIZATION, BRANCH, "kie-ci3") + } +}