Skip to content

Commit

Permalink
Updating platform jobs for pluggable scm
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmanshu.singh committed Jan 24, 2017
1 parent 31d1bc9 commit 4e00f6f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 57 deletions.
39 changes: 39 additions & 0 deletions bootstrap/Platform_Management/Setup_Pluggable_Library.groovy
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
130 changes: 73 additions & 57 deletions projects/jobs/jobs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.')
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -211,6 +226,7 @@ def cartridgeFolder = folder(cartridgeFolderName) {
}
dsl {
external("cartridge/**/jenkins/jobs/dsl/*.groovy")
additionalClasspath("job_dsl_additional_classpath")
}

}
Expand Down
1 change: 1 addition & 0 deletions workspaces/jobs/jobs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down

0 comments on commit 4e00f6f

Please sign in to comment.