Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pluggable #1

Merged
merged 1 commit into from Feb 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 68 additions & 16 deletions projects/jobs/jobs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -56,45 +56,100 @@ loadCartridgeJob.with{
booleanParam('OVERWRITE_REPOS', false, 'If ticked, existing code repositories (previously loaded by the cartridge) will be overwritten. For first time cartridge runs, this property is redundant and will perform the same behavior regardless.')
}
environmentVariables {
groovy("return [SCM_KEY: org.apache.commons.lang.RandomStringUtils.randomAlphanumeric(20)]")
env('WORKSPACE_NAME',workspaceFolderName)
env('PROJECT_NAME',projectFolderName)
}
wrappers {
preBuildCleanup()
injectPasswords()
maskPasswords()
sshAgent("adop-jenkins-master")
credentialsBinding {
file('SCM_SSH_KEY', 'adop-jenkins-private')
}
}
steps {
shell('''#!/bin/bash -ex

# Create temp directory for repositories
mkdir ${WORKSPACE}/tmp

# 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
''')
environmentVariables {
propertiesFile('scm_provider_id.properties')
}
systemGroovyCommand('''import pluggable.scm.PropertiesSCMProviderDataStore
import pluggable.scm.SCMProviderDataStore
import pluggable.configuration.EnvVarProperty;
import pluggable.scm.helpers.HelperUtils
import java.util.Properties
import hudson.FilePath


String scmProviderId = build.getEnvironment(listener).get('SCM_PROVIDER_ID')
EnvVarProperty envVarProperty = EnvVarProperty.getInstance();


envVarProperty.setVariableBindings(build.getEnvironment(listener));
SCMProviderDataStore scmProviderDataStore = new PropertiesSCMProviderDataStore();
Properties scmProviderProperties = scmProviderDataStore.get(scmProviderId);

// get credentials

String credentialId = scmProviderProperties.get("loader.credentialId")

if(credentialId != null){

def username_matcher = CredentialsMatchers.withId(credentialId);

def available_credentials = CredentialsProvider.lookupCredentials(
StandardUsernameCredentials.class
);

credentialInfo = [CredentialsMatchers.firstOrNull(available_credentials, username_matcher).username,
CredentialsMatchers.firstOrNull(available_credentials, username_matcher).password];

channel = build.workspace.channel;
fp = new FilePath(channel, build.workspace.toString() + "/" + build.getEnvVars()["SCM_KEY"])
fp.write("SCM_USERNAME="+credentialInfo[0]+"\\nSCM_PASSWORD="+credentialInfo[1], null);
}
'''){
classpath('$WORKSPACE/job_dsl_additional_classpath/')
}
shell('''#!/bin/bash -ex

# We trust everywhere
echo -e "#!/bin/sh\nexec ssh -i ${SCM_SSH_KEY} -o StrictHostKeyChecking=no \"\\\$@\"\n" > ${WORKSPACE}/custom_ssh
echo -e "#!/bin/sh
exec ssh -i ${SCM_SSH_KEY} -o StrictHostKeyChecking=no \"\\\$@\"
" > ${WORKSPACE}/custom_ssh
chmod +x ${WORKSPACE}/custom_ssh
export GIT_SSH="${WORKSPACE}/custom_ssh"

# Clone Cartridge
git clone ${CARTRIDGE_CLONE_URL} cartridge
echo "INFO: cloning ${CARTRIDGE_CLONE_URL}"
# we don't want to show the password
set +x
if ( [ ${CARTRIDGE_CLONE_URL%://*} == "https" ] || [ ${CARTRIDGE_CLONE_URL%://*} == "http" ] ) && [ -f ${WORKSPACE}/${SCM_KEY} ]; then
source ${WORKSPACE}/${SCM_KEY}
git clone ${CARTRIDGE_CLONE_URL%://*}://${SCM_USERNAME}:${SCM_PASSWORD}@${CARTRIDGE_CLONE_URL#*://} cartridge
else
git clone ${CARTRIDGE_CLONE_URL} cartridge
fi
set -x

# Find the cartridge
export CART_HOME=$(dirname $(find -name metadata.cartridge | head -1))


# Create temp directory for repositories
mkdir ${WORKSPACE}/tmp

# 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
cd ${WORKSPACE}/${CART_HOME}/infra
Expand All @@ -115,9 +170,6 @@ 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 Down Expand Up @@ -228,7 +280,7 @@ def cartridgeFolder = folder(cartridgeFolderName) {
external("cartridge/**/jenkins/jobs/dsl/*.groovy")
additionalClasspath("job_dsl_additional_classpath")
}

shell('rm -f $WORKSPACE/$SCM_KEY')
}
scm {
git {
Expand Down