From baeb703fd8ab2640aed5bf2101509f10d282c897 Mon Sep 17 00:00:00 2001 From: "dharmanshu.singh" Date: Wed, 4 Jan 2017 11:54:52 +0000 Subject: [PATCH] Updating platform jobs for pluggable scm --- .../Setup_Pluggable_Library.groovy | 39 ++++++ projects/jobs/jobs.groovy | 130 ++++++++++-------- workspaces/jobs/jobs.groovy | 1 + 3 files changed, 113 insertions(+), 57 deletions(-) create mode 100644 bootstrap/Platform_Management/Setup_Pluggable_Library.groovy diff --git a/bootstrap/Platform_Management/Setup_Pluggable_Library.groovy b/bootstrap/Platform_Management/Setup_Pluggable_Library.groovy new file mode 100644 index 0000000..e4bc0e7 --- /dev/null +++ b/bootstrap/Platform_Management/Setup_Pluggable_Library.groovy @@ -0,0 +1,39 @@ +// Constants +def pluggableGitURL = "https://github.com/Accenture/adop-pluggable-scm" + +def platformManagementFolderName= "/Platform_Management" +def platformManagementFolder = folder(platformManagementFolderName) { displayName('Platform Management') } + +// Jobs +def setupPluggable = freeStyleJob(platformManagementFolderName + "/Setup_Pluggable_Library") + +// Setup setup_cartridge +setupPluggable.with{ + environmentVariables { + keepBuildVariables() + keepSystemVariables() + } + wrappers { + preBuildCleanup() + } + steps { + shell('''#!/bin/bash -ex + +echo "Extracting Pluggable library to additonal classpath location: ${PLUGGABLE_SCM_PROVIDER_PATH}" +cp -r src/main/groovy/pluggable/ ${PLUGGABLE_SCM_PROVIDER_PATH} +echo "******************" + +echo "Library contents: " +ls ${PLUGGABLE_SCM_PROVIDER_PATH}pluggable/scm/ +''') + } + scm { + git { + remote { + name("origin") + url("${pluggableGitURL}") + } + branch("*/master") + } + } +} diff --git a/projects/jobs/jobs.groovy b/projects/jobs/jobs.groovy index 030244a..95d1246 100644 --- a/projects/jobs/jobs.groovy +++ b/projects/jobs/jobs.groovy @@ -2,10 +2,20 @@ def gerritBaseUrl = "ssh://jenkins@gerrit:29418" def cartridgeBaseUrl = gerritBaseUrl + "/cartridges" def platformToolsGitUrl = gerritBaseUrl + "/platform-management" +def scmPropertiesPath = "${PLUGGABLE_SCM_PROVIDER_PROPERTIES_PATH}" // Folders def workspaceFolderName = "${WORKSPACE_NAME}" +// Dynamic values +def customScmNamespace = "${CUSTOM_SCM_NAMESPACE}" +String namespaceValue = null +if (customScmNamespace == "true"){ + namespaceValue = '"${SCM_NAMESPACE}"' +} else { + namespaceValue = 'null' +} + def projectFolderName = workspaceFolderName + "/${PROJECT_NAME}" def projectFolder = folder(projectFolderName) @@ -29,6 +39,15 @@ def loadCartridgeCollectionJob = workflowJob(cartridgeManagementFolderName + "/L // Setup Load_Cartridge loadCartridgeJob.with{ parameters{ + activeChoiceParam('SCM_PROVIDER') { + description('Your chosen SCM Provider and the appropriate cloning protocol') + filterable() + choiceType('SINGLE_SELECT') + scriptlerScript('retrieve_scm_props.groovy') + } + if (customScmNamespace == "true"){ + stringParam('SCM_NAMESPACE', '', 'The namespace for your SCM provider which will prefix your created repositories') + } choiceParam('CARTRIDGE_CLONE_URL', cartridge_list, 'Cartridge URL to load') stringParam('CARTRIDGE_FOLDER', '', 'The folder within the project namespace where your cartridge will be loaded into.') stringParam('FOLDER_DISPLAY_NAME', '', 'Display name of the folder where the cartridge is loaded.') @@ -45,12 +64,15 @@ loadCartridgeJob.with{ injectPasswords() maskPasswords() sshAgent("adop-jenkins-master") + credentialsBinding { + file('SCM_SSH_KEY', 'adop-jenkins-private') + } } steps { shell('''#!/bin/bash -ex # We trust everywhere -echo -e "#!/bin/sh\nexec ssh -o StrictHostKeyChecking=no \"\\\$@\"\n" > ${WORKSPACE}/custom_ssh +echo -e "#!/bin/sh\nexec ssh -i ${SCM_SSH_KEY} -o StrictHostKeyChecking=no \"\\\$@\"\n" > ${WORKSPACE}/custom_ssh chmod +x ${WORKSPACE}/custom_ssh export GIT_SSH="${WORKSPACE}/custom_ssh" @@ -60,64 +82,18 @@ git clone ${CARTRIDGE_CLONE_URL} cartridge # Find the cartridge export CART_HOME=$(dirname $(find -name metadata.cartridge | head -1)) -# Check if the user has enabled Gerrit Code reviewing -if [ "$ENABLE_CODE_REVIEW" == true ]; then - permissions_repo="${PROJECT_NAME}/permissions-with-review" -else - permissions_repo="${PROJECT_NAME}/permissions" -fi - -# Check if folder was specified -if [ -z ${CARTRIDGE_FOLDER} ] ; then - echo "Folder name not specified..." - repo_namespace="${PROJECT_NAME}" -else - echo "Folder name specified, changing project namespace value.." - repo_namespace="${PROJECT_NAME}/${CARTRIDGE_FOLDER}" -fi -# Create repositories +# Create temp directory for repositories mkdir ${WORKSPACE}/tmp -cd ${WORKSPACE}/tmp -while read repo_url; do - if [ ! -z "${repo_url}" ]; then - repo_name=$(echo "${repo_url}" | rev | cut -d'/' -f1 | rev | sed 's#.git$##g') - target_repo_name="${repo_namespace}/${repo_name}" - # Check if the repository already exists or not - repo_exists=0 - list_of_repos=$(ssh -n -o StrictHostKeyChecking=no -p 29418 jenkins@gerrit gerrit ls-projects --type code) - - for repo in ${list_of_repos} - do - if [ ${repo} = ${target_repo_name} ]; then - echo "Found: ${repo}" - repo_exists=1 - break - fi - done - - # If not, create it - if [ ${repo_exists} -eq 0 ]; then - ssh -n -o StrictHostKeyChecking=no -p 29418 jenkins@gerrit gerrit create-project --parent "${permissions_repo}" "${target_repo_name}" - else - echo "Repository already exists, skipping create: ${target_repo_name}" - fi - - # Populate repository - git clone ssh://jenkins@gerrit:29418/"${target_repo_name}" - cd "${repo_name}" - git remote add source "${repo_url}" - git fetch source - if [ "$OVERWRITE_REPOS" == true ]; then - git push origin +refs/remotes/source/*:refs/heads/* - else - set +e - git push origin refs/remotes/source/*:refs/heads/* - set -e - fi - cd - - fi -done < ${WORKSPACE}/${CART_HOME}/src/urls.txt + +# Copy pluggable SCM package into workspace +mkdir ${WORKSPACE}/job_dsl_additional_classpath +cp -r ${PLUGGABLE_SCM_PROVIDER_PATH}pluggable $WORKSPACE/job_dsl_additional_classpath + +# Output SCM provider ID to a properties file +echo SCM_PROVIDER_ID=$(echo ${SCM_PROVIDER} | cut -d "(" -f2 | cut -d ")" -f1) > scm_provider_id.properties +echo GIT_SSH="${GIT_SSH}" >> scm_provider.properties + # Provision one-time infrastructure if [ -d ${WORKSPACE}/${CART_HOME}/infra ]; then @@ -139,6 +115,9 @@ if [ -d ${WORKSPACE}/${CART_HOME}/jenkins/jobs ]; then fi fi ''') + environmentVariables { + propertiesFile('scm_provider_id.properties') + } systemGroovyCommand(''' import jenkins.model.* import groovy.io.FileType @@ -165,6 +144,42 @@ fileList.each { jenkinsInstace.getItem(projectName,jenkinsInstace).createProjectFromXML(jobName, xmlStream) } ''') + dsl { + text('''import pluggable.scm.*; + +// Instantiate your SCM provider where your repos will be created +SCMProvider scmProvider = SCMProviderHandler.getScmProvider("${SCM_PROVIDER_ID}", binding.variables) + +def workspace = "${WORKSPACE}" +def projectFolderName = "${PROJECT_NAME}" +def cartridgeFolder = "${CARTRIDGE_FOLDER}" +def overwriteRepos = "${OVERWRITE_REPOS}" +def scmNamespace = ''' + namespaceValue + ''' +def codeReviewEnabled = "${ENABLE_CODE_REVIEW}" + +String repoNamespace = null; + +// Check if a custom SCM namespace has been provided +if (scmNamespace != null && !scmNamespace.isEmpty()){ + println("Custom SCM namespace specified...") + repoNamespace = scmNamespace +} else { + // Check if a folder is specified + println("Custom SCM namespace not specified, using default project namespace...") + if (cartridgeFolder == ""){ + println("Folder name not specified...") + repoNamespace = projectFolderName + } else { + println("Folder name specified, changing project namespace value..") + repoNamespace = projectFolderName + "/" + cartridgeFolder + } +} + +// Create your SCM repositories +scmProvider.createScmRepos(workspace, repoNamespace, codeReviewEnabled, overwriteRepos) + ''') + additionalClasspath("job_dsl_additional_classpath/") + } conditionalSteps { condition { shell ('''#!/bin/bash @@ -211,6 +226,7 @@ def cartridgeFolder = folder(cartridgeFolderName) { } dsl { external("cartridge/**/jenkins/jobs/dsl/*.groovy") + additionalClasspath("job_dsl_additional_classpath") } } diff --git a/workspaces/jobs/jobs.groovy b/workspaces/jobs/jobs.groovy index 5cbfd07..e58786f 100644 --- a/workspaces/jobs/jobs.groovy +++ b/workspaces/jobs/jobs.groovy @@ -15,6 +15,7 @@ def generateProjectJob = freeStyleJob(projectManagementFolderName + "/Generate_P generateProjectJob.with{ parameters{ stringParam("PROJECT_NAME","","The name of the project to be generated.") + booleanParam('CUSTOM_SCM_NAMESPACE', false, 'Enables the option to provide a custom project namespace for your SCM provider') stringParam("ADMIN_USERS","","The list of users' email addresses that should be setup initially as admin. They will have full access to all jobs within the project.") stringParam("DEVELOPER_USERS","","The list of users' email addresses that should be setup initially as developers. They will have full access to all non-admin jobs within the project.") stringParam("VIEWER_USERS","","The list of users' email addresses that should be setup initially as viewers. They will have read-only access to all non-admin jobs within the project.")