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

RANCHER-2057: Refactor 'updateModule' pipeline source code to support later code re-use #799

Draft
wants to merge 3 commits into
base: RANCHER-1334
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,60 +123,8 @@ ansiColor('xterm') {
logger.info("Skip Maven/Gradle build stage for non-Github module source: ${params.MODULE_SOURCE}")
Utils.markStageSkippedForConditional('[Maven/Gradle] Compile')
} else {
// Check if Maven or Gradle build tool is used
module.buildTool = fileExists("${module.name}/pom.xml") ? 'maven' : 'gradle'

switch (module.buildTool) {
case 'maven':
logger.info("Maven build tool is used")
module.buildDir = "${env.WORKSPACE}/${module.name}"

dir(module.buildDir) {
// Read module version from pom.xml
String moduleVersion = readMavenPom(file: 'pom.xml').version

// Load Module Details for Maven builds
module.loadModuleDetails("${module.name}-${moduleVersion}.${featureHash}")

// Build Module as Maven Project
withMaven(
jdk: "${common.selectJavaBasedOnAgent(params.AGENT)}".toString(),
maven: Constants.MAVEN_TOOL_NAME,
traceability: false,
options: [artifactsPublisher(disabled: true)])
{
sh """
mvn versions:set -DnewVersion=${module.getVersion()}
mvn package ${mavenArguments}
""".stripIndent().trim()
}
}
break

case 'gradle':
logger.info("Gradle build tool is used")
module.buildDir = "${env.WORKSPACE}/${module.name}/service"

dir(module.buildDir) {
// Read module version from gradle.properties
String moduleVersion = readProperties(file: "gradle.properties")['appVersion']

// Append -SNAPSHOT to the version if it's not a snapshot version
if(!moduleVersion.contains("-SNAPSHOT")) {
moduleVersion += "-SNAPSHOT"
}

// Load Module Details for Gradle builds
module.loadModuleDetails("${module.name}-${moduleVersion}.${featureHash}")

// Build Module as Gradle Project
sh "./gradlew ${commonGradleOpts} -PappVersion=${module.getVersion()} assemble"
}
break

default:
throw new RuntimeException("Unsupported build tool for Eureka module: ${module.buildTool}")
}
// Compile Application Module from Source Code
deployAppModule.compile(logger, module, featureHash, mavenArguments, commonGradleOpts)
}
}

Expand All @@ -187,70 +135,30 @@ ansiColor('xterm') {
Utils.markStageSkippedForConditional('[Docker] Build and push')
} else {
// Put Container Image to Amazon ECR
docker.withRegistry("https://${Constants.ECR_FOLIO_REPOSITORY}", "ecr:${Constants.AWS_REGION}:${Constants.ECR_FOLIO_REPOSITORY_CREDENTIALS_ID}") {
// Check if ECR Repository exists, if not create it
common.checkEcrRepoExistence(module.name)

dir(module.buildDir) {
switch (module.buildTool) {
case 'maven':
module.modDescriptorPath = "${module.buildDir}/target/ModuleDescriptor.json"

// Build and Push Container Image
docker.build("${module.name}:${module.version}", '--no-cache=true --pull=true .').push()
break

case 'gradle':
module.modDescriptorPath = "${module.buildDir}/build/resources/main/okapi/ModuleDescriptor.json"

// Build Container Image
String shellCmd = """
echo '[INFO] Build Container Image'
./gradlew ${commonGradleOpts} -PdockerRepo=folioci -PappVersion=${module.version} buildImage

echo '[INFO] Rename Local Container Image'
docker tag folioci/${module.name}:${module.version}.${env.BUILD_NUMBER} ${module.name}:${module.version}

echo '[INFO] Get rid of BUILD_NUMBER in Module Descriptor'
[ -e ${module.modDescriptorPath} ] && sed -i 's#${module.version}.${env.BUILD_NUMBER}#${module.version}#g' ${module.modDescriptorPath}
""".stripIndent().trim()

sh(label: 'Build Container Image', script: shellCmd)

// Push Container Image to Amazon ECR
docker.image("${module.name}:${module.version}").push()
break

default:
throw new RuntimeException("Unsupported build tool for Eureka module: ${module.buildTool}")
}
}
}

// Upload Module Descriptor to Eureka Module Registry
sh( label: "Upload Module Descriptor to Eureka Module Registry",
returnStdout: false,
script: "[ -e ${module.modDescriptorPath} ] && curl -sS -X PUT ${Constants.EUREKA_REGISTRY_URL}${module.name}-${module.version} --upload-file ${module.modDescriptorPath}"
)

// Save Module Descriptor to Module instance for further usage
module.descriptor = [readJSON(file: module.modDescriptorPath)]
deployAppModule.buildAndPushContainerImage(logger, module)
}
}

stage('Generate context') {
eureka.getExistedTenantsForModule(module).values()
.each {namespace.addTenant(it)}
stage('[CURL] Upload module descriptor') {
// Skip Stage if module source is not Github
if (!params.MODULE_SOURCE.contains('github')) {
logger.info("Skip Docker build stage for non-Github module source: ${params.MODULE_SOURCE}")
Utils.markStageSkippedForConditional('[Docker] Build and push')
} else {
// Put Module Descriptor to Application Module Registry
deployAppModule.putModuleDescriptorToRegistry(logger, module)
}
}

if(!(namespace.tenants))
throw new Exception("There are no tenants with the module ${module.getName()}")
stage('[REST] Get Tenants with Module') {
deployAppModule.getTenantsWithModule(logger, eureka, module, namespace)
}

stage("Retrieve module's sidecar"){
folioHelm.withKubeConfig(namespace.getClusterName()) {
String sidecarImage =
kubectl.getDeploymentContainerImageName(namespace.getNamespaceName(), module.getName(), "sidecar")
.replace(':', '-')
kubectl.getDeploymentContainerImageName(namespace.getNamespaceName(), module.getName(), "sidecar")
.replace(':', '-')

// In case updated module doesn't have Sidecar let's seek for it in other modules.
if(!sidecarImage) {
Expand All @@ -275,58 +183,8 @@ ansiColor('xterm') {
logger.debug("Namespace modules: ${namespace.modules.installJsonObject}")
}

// 1. Register Application Descriptor with Updated Module Version
stage('[Rest] Update App Descriptor') {
try {
updatedAppInfoMap = eureka.updateAppDescriptorFlow(namespace.applications, module)
} catch (e) {
logger.warning("Failed to register new application descriptor: ${e}")
eureka.removeResourcesOnFailFlow(updatedAppInfoMap, module)
throw e
}
}

// 2. Enable New Module Version (Discovery) for Application
stage('[Rest] Module Discovery') {
try {
eureka.runModuleDiscoveryFlow(module)
} catch (e) {
logger.warning("Failed to perform new module discovery: ${e}")
eureka.removeResourcesOnFailFlow(updatedAppInfoMap, module)
throw e
}
}

// 3. Deploy New Module Version to Rancher Namespace via Helm
stage('[Helm] Deploy Module Ver') {
try {
folioHelm.withKubeConfig(namespace.clusterName) {
folioHelm.deployFolioModule(namespace, module.name, module.version, false, namespace.defaultTenantId)
echo "Module ${module.name}-${module.version} deployed to ${namespace.clusterName} namespace ${namespace.workspaceName}"
kubectl.checkDeploymentStatus(module.name, namespace.namespaceName, "300")
echo "Waiting another 2 minutes to get module initialized..."
sleep time: 2, unit: 'MINUTES'
}
} catch (e) {
logger.warning("Failed to deploy new module version via Helm: ${e}")
eureka.removeResourcesOnFailFlow(updatedAppInfoMap, module)
throw e
}
}

// 4. Enable Application Descriptor with Updated Module Version for Tenants in Namespace
stage('[Rest] Enable New App') {
try {
eureka.enableApplicationsOnTenantsFlow(namespace.tenants, updatedAppInfoMap)
} catch (e) {
logger.warning("Failed to enable (entitle) new application version: ${e}")
eureka.removeResourcesOnFailFlow(updatedAppInfoMap, module)
throw e
}
}

stage('[Rest] Remove Stale Resources') {
eureka.removeStaleResourcesFlow(namespace.applications, updatedAppInfoMap, module)
stage('Update Module Version') {
deployAppModule.updateModuleVersionFlow(logger, eureka, module, namespace)
}

} catch (e) {
Expand Down
Loading