Files
devops-lib-gcp/vars/eksCICD.groovy
T
2026-08-26 02:02:24 +05:30

151 lines
6.0 KiB
Groovy

import com.meesho.stages.hotFix
import com.meesho.stages.checkOut
import com.meesho.stages.buildObjHelper
import com.meesho.stages.notify
import com.meesho.utilities.getYamlParameter
import com.meesho.utilities.constructParam
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatterBuilder
def call(Map repo) {
ansiColor('xterm') {
def allowedUsers = ['turbo-turtle', 'ringmaster-bot', 'yeleswaram.teja@meesho.com', 'mahak.jain@meesho.com', 'naveen.vellingiri@meesho.com', 'vignesh.ganesan@meesho.com', 'siddharth.g@meesho.com','aryaman.parida@meesho.com','shatwik.pandey@meesho.com' ,'toolchain-jenkins']
def userId = null
try {
def cause = currentBuild.rawBuild.getCause(hudson.model.Cause.UserIdCause)
if (cause) {
userId = cause.getUserId()
}
} catch (Exception e) {
log.error("Failed to get user ID: ${e.message}")
}
if (userId && !allowedUsers.contains(userId)) {
echo "\u001B[1;31m========================================\n[ERROR] Build triggered by unauthorized user: ${userId}\n\nPlease use Ringmaster to trigger builds and deployments: https://ringmaster.meeshogcp.in/applications/cicd/home\n========================================\u001B[0m"
error("Build triggered by unauthorized user: ${userId}.")
} else if (!userId) {
echo "\u001B[1;31m========================================\n[ERROR] Could not determine the user who triggered the build\n\nThis might be a scheduled or system-triggered build.\n========================================\u001B[0m"
error("Could not determine the user who triggered the build")
} else {
echo "Starting the build....."
}
}
env.STARTTIME = getDateTime()
env.FAILURE = 'FAILURE'
echo "CLOUD_PROVIDER : ${env.CLOUD_PROVIDER}"
echo "INFRA_ENV : ${env.INFRA_ENV}"
switch (env.CLOUD_PROVIDER) {
case 'GCP':
echo 'Running on GCP'
gcpInfra(repo)
break
case 'AWS':
echo 'Running on AWS'
awsInfra(repo)
break
default:
log.error('Not running on AWS or GCP')
currentBuild.result = env.FAILURE
def msg = 'Job Failed. Error: Not running on AWS or GCP'
notify.run(msg)
}
}
def gcpInfra (Map repo) {
def isSidecarNeeded = repo.get('useSidecar', false)
def yamlName = isSidecarNeeded ? "${env.INFRA_ENV}-sidecar-pod.yaml" : "${env.INFRA_ENV}-pod.yaml"
def podyaml = "org/meesho/${yamlName}"
echo "Architecture Check: useSidecar=${isSidecarNeeded}. Loading ${podyaml}"
podTemplate(yaml: libraryResource(podyaml)) {
node(POD_LABEL) {
container('devops-tools') {
commonCICDFlow(repo)
}
}
}
}
def awsInfra (Map repo) {
node('EKS') {
commonCICDFlow(repo)
}
}
def commonCICDFlow (Map repo) {
def constructParam = new constructParam()
def ymlObj = new getYamlParameter()
def checkObj = new checkOut()
def buildObjHelper = new buildObjHelper()
def hotFixObj = new hotFix()
def notify = new notify()
def param = [:]
def msg = 'Job Passed'
env.msg = msg
env.error_msg_to_db = ''
timestamps {
ansiColor('xterm') {
try {
env.deploymentStartTime = new Date().format('yyyy-MM-dd HH:mm:ss')
checkObj.run(repo)
param = ymlObj.getParam(repo.repo_name)
def maintainer = param.maintainer ?: 'jenkins-user'
def buildObj = buildObjHelper.run(param.build_tool)
constructParam.run(param)
param['skip_notify'] = env.skip_notify
log.info(param)
hotFixObj.run(param.repo_name)
// Pre-build hooks: user scripts declared under
// environment.<env>.hooks.pre_build in the repo's config.yaml.
// constructParam has merged the resolved env block into param,
// so param.hooks holds the current environment's hooks. A
// blocking hook failure fails the build before the docker build.
runHooks(param, 'pre_build')
buildObj.run(param)
// Post-build hooks: run after a successful build.
runHooks(param, 'post_build')
// Auto-trigger the standalone ai-blitz-jobs (coverage-only) job
// once the per-repo CI has succeeded. Internally gated on
// PR / hot-fix / toolchain / mainline-branch (see helper), and
// fire-and-forget — coverage observability must never block
// or fail this build. Repos opt out via skip_coverage_trigger
// in config.yaml.
triggerCoverageOnly(param)
}
catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
currentBuild.result = 'ABORTED'
log.info(currentBuild.result)
env.msg = 'Job Aborted'
env.error_msg_to_db = 'Job ABORTED by the User'
}
catch (Exception e) {
if (env.msg == msg) {
log.error(e.toString())
currentBuild.result = env.FAILURE
env.msg = 'Job Failed. Error: ' + e.toString()
}
}
finally {
env.deploymentEndTime = new Date().format('yyyy-MM-dd HH:mm:ss')
notify.run(param)
}
}
}
}
@NonCPS
def getDateTime() {
// Get the current date and time in IST
def currentDateTime = ZonedDateTime.now()
// Create a formatter for the desired pattern
def formatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd'T'HH:mm:ss")
.appendOffset('+HH:mm', '+00:00')
.toFormatter()
// Format the current date and time using the formatter
def formattedDateTime = currentDateTime.format(formatter)
return formattedDateTime
}