import com.meesho.utilities.gitActions import com.meesho.utilities.buTeamMapping import com.meesho.utilities.getYamlParameter import com.meesho.utilities.constructTemplate import com.meesho.stages.multiBranchPipeline import com.meesho.stages.deployArgoCD def call(Map params){ podTemplate(yaml: libraryResource('org/meesho/pod.yaml')) { node(POD_LABEL) { container('devops-tools') { timestamps{ ansiColor("xterm"){ env.GITHUB_CRED = 'svc-devops-meesho' def gitObj = new gitActions() def bu_team_obj = new buTeamMapping() def constructObj = new constructTemplate() def application_info_map = [:] def repo_bu = "" stage('App Repo: cloning the repo'){ gitObj.clone("${WORKSPACE}", params.repo_name, params.repo_main_branch) } stage('APP Repo: Create Jenkinsfile(gcp-main)'){ if (!params.skip_jenkinsfile){ def repo_branch_name = "gcp-main" def config = ["repo_name":params.repo_name] gitObj.branchCheckOut(params.repo_name, repo_branch_name) dir(params.repo_name){ sh "chmod 777 Jenkinsfile" constructObj.renderTemplate(config, 'Jenkinsfile', 'Jenkinsfile') gitObj.add('.', 'Jenkinsfile') } def commit_status = gitObj.codeCommit(repo_name, repo_branch_name, 'Generating build and deployments files') if (commit_status == 0) { gitObj.codePush(repo_name, repo_branch_name) } } else { log.info("skipping this stage") } } stage('Helm Repo: Update values_property file'){ def yaml_obj = new getYamlParameter() gitObj.clone("${WORKSPACE}","devops-helm-charts","main") def helm_repo_name = "devops-helm-charts" def helm_checkout_branch = "gcp-migration-${params.repo_name}-${env.BUILD_NUMBER}" def repo_branch_name = "gcp-main" gitObj.branchCheckOut("devops-helm-charts", helm_checkout_branch) gitObj.branchCheckOut(params.repo_name, repo_branch_name) def build_config = yaml_obj.getParam(params.repo_name) if (build_config['copy_file'] || (build_config['environment'] && build_config['environment']['prd']['copy_file'])){ // voilating naming convention willing to avoid confilct with deployment variables log.info("Checking copy_file path") def buSrt = bu_team_obj.get_bu_initials(build_config["bu"]) def teamSrt = bu_team_obj.get_team_initials(build_config["team"]) def copy_path = (build_config['copy_file'])? build_config.copy_file.path : build_config.environment.prd.copy_file.path if (copy_path.startsWith("gs:")){ log.info("Copy file path already pointing to gcs") } else { def file_name = copy_path.split('/')[-1] def new_path = "gs://gcs-${buSrt}-${teamSrt}-config-prd/${params.repo_name}/${file_name}" if(build_config.copy_file){ build_config.copy_file.path = new_path } else { build_config.environment.prd.copy_file.path = new_path } dir(params.repo_name){ sh "chmod 777 config.yaml" writeYaml file: 'config.yaml', data: build_config, overwrite: true } gitObj.add(params.repo_name, 'config.yaml') def config_commit = gitObj.codeCommit(repo_name, repo_branch_name, 'Changed copy_file path to gcs') if (config_commit == 0) { gitObj.codePush(repo_name, repo_branch_name) } log.warning("Path for copy_file has been changed to gcs. Please upload the contents to ${new_path} from ${copy_path}") } } if (build_config.containsKey('environment')) { Map envrionment_config = build_config['environment'].collectEntries { key, value -> "prd".matches(key)? value: [:]} build_config.remove('environment') build_config.putAll(envrionment_config) } repo_bu = build_config.bu env.skip_user_input = params.skip_userinput def wait_obj = new deployArgoCD() def deployment_order = build_config.deployment_order timeout(unit: 'SECONDS', time: 300) { userInput = wait_obj.wait_for_user_input(build_config.deployment_order) } if (userInput == '') { log.info('No Deployments selected. Running remaining steps.') return } else if (!userInput.contains('All')) { deployment_order = userInput.split(',') as List } for (deployment in deployment_order){ def application_config = yaml_obj.getParam(params.repo_name,"deployments/${deployment}.yaml") def bu_short = bu_team_obj.get_bu_initials(application_config["bu"]) def BU = application_config["bu"] def team_short = bu_team_obj.get_team_initials(application_config["team"]) //def app_name_short = application_config.app_name.substring(0, Math.min(originalString.length(), 15)) def app_name_short = application_config.app_name def values_version = env.cicd_environment == 'prd' ? 'values_v3' : 'values_v2' def value_properties_path = "${values_version}/${bu_short}/${team_short}/${deployment}/values_properties.yaml" def value_properties_content = yaml_obj.getParam(helm_repo_name,value_properties_path) def canary_default = ['progressDeadlineSeconds': 300,'analysisInterval': '120s','analysisThreshold': 5,'analysisMaxWeight': 5,'analysisStepWeight': 5,'analysisMetrics':['thresholdRangeMin': 0.99,'interval': '1m'],'skipAnalysis': true] // Set/change value_properties values for migration value_properties_content['as_min']= 1 log.info("Setting canary skipAnalysis to true") if (value_properties_content['canary']){ value_properties_content['canary']['skipAnalysis'] = true } else { value_properties_content['canary'] = canary_default } log.info("Canary skipAnalysis has been set to true") log.info("set min and nodeselector") if (value_properties_content['ingress_class']!= "contour-external"){ if(value_properties_content['host']){ value_properties_content.host = application_config['app_name']+".prd.meesho.int" } else if(value_properties_content.hosts){ for (host_arr in value_properties_content.hosts){ host_arr.host = application_config['app_name']+".prd.meesho.int" } } if (application_config.priority_v2 == "up0" || application_config.priority_v2 == "sp0" || application_config.priority_v2 == "cp0"){ value_properties_content.ingress_class = "contour-internal-0" } else{ value_properties_content.ingress_class = "contour-internal-1" } } else if (value_properties_content['ingress_class'] == "external"){ value_properties_content.ingress_class = "contour-external" } log.info("set host") if (value_properties_content.serviceAccount){ value_properties_content.serviceAccount.annotations.remove('eks.amazonaws.com/role-arn') value_properties_content.serviceAccount.annotations['iam.gke.io/gcp-service-account'] = "sa-${bu_short}-prd-${app_name_short}@meesho-${BU}-prd-0622.iam.gserviceaccount.com" } log.info("set service account") dir(helm_repo_name){ sh "chmod 777 ${value_properties_path}" writeYaml file: value_properties_path, data: value_properties_content, overwrite: true } def host = (value_properties_content.host)? value_properties_content.host : value_properties_content.hosts[0].host log.info(host) application_info_map[application_config.app_name]=[ "bu": application_config.bu, "team": application_config.team, "host": host, "ingress_class": value_properties_content.ingress_class ] log.info(application_info_map) gitObj.add(helm_repo_name,value_properties_path) } commit_status = gitObj.codeCommit(helm_repo_name, helm_checkout_branch, "Modified all application of ${params.repo_name} repo") if (commit_status == 0) { try{ gitObj.codePush(helm_repo_name, helm_checkout_branch) } catch(Exception e){ log.error("Error in code push") return } try{ pr_num = gitObj.createPR(params.repo_name, helm_repo_name, "main", helm_checkout_branch, "Merge ${params.repo_name} changes") gitObj.mergePR(helm_repo_name, pr_num, helm_checkout_branch) gitObj.deleteBranch(helm_repo_name, "main", helm_checkout_branch) } catch(Exception ex){ log.error("error in code push") log.error(ex) log.info("Deleting the branch") gitObj.deleteBranch(helm_repo_name, "main", helm_checkout_branch) } } } stage('Jenkins: Create Jenkins job'){ def job_obj = new multiBranchPipeline() job_obj.applicationOnboard(repo_bu,params.repo_name) log.info("Jenkins job created") } // stage('GCP: Create DNS record'){ // def zone="pvt-meesho-admin-prd-meesho-int" // def project="meesho-admin-prd-0622" // def app_name_list = application_info_map.collect{ it.key } // log.info(app_name_list) // for (app_name in app_name_list) { // def host=application_info_map[app_name].host // def ingress_class=application_info_map[app_name].ingress_class // def BU=application_info_map[app_name].bu // def dns_status= sh(script: "gcloud dns --project=${project} record-sets describe ${host} --zone=${zone} --type='CNAME' ",returnStatus: true) // if( dns_status != 0 ){ // dns_status = sh(script: "gcloud dns --project=${project} record-sets create ${host} --zone=${zone} --type=CNAME --ttl='300' --rrdatas='${ingress_class}.${BU}.prd.prd.meesho.int.' ",returnStatus: true) // if(dns_status == 0 ){ // log.info("DNS recordSet ${host} created successfully") // } // else{ // log.info("DNS recordset failed with status ${dns_status}") // } // } // } // } // stage('GCP: Create workload identity'){ // def bu_short=bu_team_obj.get_bu_initials(application_info_map[app_name].bu) // def BU = application_info_map[app_name].bu // def project="meesho-${BU}-prd-0622" // def app_name_list = application_info_map.collect{ it.key } // for (app_name in app_name_list){ // if (params.provide_service_role){ // //def app_name_short= app_name.substring(0, Math.min(originalString.length(), 15)) // def app_name_short=app_name // def sa = "sa-${bu_short}-prd-${app_name_short}" // sh(script: "gcloud iam service-accounts create ${sa} --display-name='service account for ${app_name}' --project=${project} ") // sh(script: "gcloud iam service-accounts add-iam-policy-binding --role roles/iam.workloadIdentityUser --member 'serviceAccount:${sa}@meesho-${BU}-prd-0622.iam.gserviceaccount.com' --project=meesho-${BU}-prd-0622") // sh(script: "gcloud iam service-accounts add-iam-policy-binding --role roles/${params.SA_role} --member 'serviceAccount:${sa}@meesho-${BU}-prd-0622.iam.gserviceaccount.com' --project=meesho-${BU}-prd-0622") // } // } // } stage('CoreDNS: Add entry for svc-svc'){ def yaml_obj = new getYamlParameter() def coredns_repo_name = "devops-infra-helm-charts" def coredns_checkout_branch = "gcp-migration-${params.repo_name}-${env.BUILD_NUMBER}" gitObj.clone("${WORKSPACE}",coredns_repo_name,"main") gitObj.branchCheckOut(coredns_repo_name, coredns_checkout_branch) def coredns_path= "helm-templates/coredns/values.yaml" def coredns_content = yaml_obj.getParam(coredns_repo_name,coredns_path) def app_name_list = application_info_map.collect{ it.key } for (app_name in app_name_list) { def host=application_info_map[app_name].host def ingress_class=application_info_map[app_name].ingress_class def BU=application_info_map[app_name].bu coredns_content.rewrites[host]="${ingress_class}-${BU}-prd" log.info("coredns entry for ${host} is set") dir(coredns_repo_name){ sh "chmod 777 ${coredns_path}" writeYaml file: coredns_path, data: coredns_content, overwrite: true } gitObj.add(coredns_repo_name,coredns_path) } commit_status = gitObj.codeCommit(coredns_repo_name, coredns_checkout_branch, "Modified all application of ${params.repo_name} repo") if (commit_status == 0) { try{ gitObj.codePush(coredns_repo_name, coredns_checkout_branch) } catch(Exception e){ log.error("Error in code push") return } try{ pr_num = gitObj.createPR(params.repo_name, coredns_repo_name, "main", coredns_checkout_branch, "Merge ${params.repo_name} changes") gitObj.mergePR(coredns_repo_name, pr_num, coredns_checkout_branch) gitObj.deleteBranch(coredns_repo_name, "main", coredns_checkout_branch) } catch(Exception ex){ log.error("error in code push") log.error(ex) log.info("Deleting the branch") gitObj.deleteBranch(coredns_repo_name, "main", coredns_checkout_branch) } } } } } } } } }