jenkins pipeline 中使用 if switch 分支语句
pipeline 中使用 if 语句
pipeline { //创建 jenkins 参数 parameters { string(name: 'open_log', defaultValue: params.open_log, description: 'log-on: 加入 online ; log-off: 不加 online') } agent { node { label ' yunwei-node' } } stages { stage('init params') { steps { sh "echo ${params.open_log}" script { if (params.open_log.trim() == "log-on") { env.RUN_CMD = "on" } else { env.RUN_CMD = "off" } } sh "echo ${env.RUN_CMD}" } } } }
pipeline 中使用 switch 语句
pipeline { //创建 jenkins 参数 parameters { // string(name: 'project_name', defaultValue: 'spring-boot-demo-helloworld', description: '项目名字') // string(name: 'version', defaultValue: '0.0.1-SNAPSHOT', description: 'pkg 打包版本') choice(name: 'deploy_choice', choices: ['all', 'canary_1', 'canary_other'], description: 'all 全部; canary_1 先发一个; canary_other 再发余下') } agent { node { label 'slave-node' } } stages { stage('init params') { steps { // 设置环境变量 script { env.LIMIT_MEMORY = params.limit_memory != null ? "${params.limit_memory}" : 512 // 单位 m ,此处不加单位, 应用于 java 程序和 request env.K8S_LIMIT_MAX_MEMORY = params.limit_max_memory != null ? "${params.limit_max_memory}" : 1500 //单位 m ,此处不加单位, 应用于 limit mem 达到此值 pod 重启 } } } // Deploy to Kubernetes stage('Deploy to Kubernetes') { steps { script{ switch(params.deploy_choice) { case "canary_1": println("deploy canary_1"); sh "kubectl -n ${env.DEPLOY_ENV} apply -f scripts/${env.CANARY_DEPLOYMENT_FILE}" sh "kubectl -n ${env.DEPLOY_ENV} apply -f scripts/${env.SERVICE_FILE}" break; case "canary_other": println("deploy canary_other"); sh "kubectl -n ${env.DEPLOY_ENV} apply -f scripts/${env.PLOYMENT_FILE}" sh "kubectl -n ${env.DEPLOY_ENV} apply -f scripts/${env.HPA_CONFI_FILE}" sh "kubectl -n ${env.DEPLOY_ENV} apply -f scripts/${env.SERVICE_FILE}" break; default: println("deploy all"); sh "kubectl -n ${env.DEPLOY_ENV} apply -f scripts/${env.PLOYMENT_FILE} -f scripts/${env.CANARY_DEPLOYMENT_FILE}" sh "kubectl -n ${env.DEPLOY_ENV} apply -f scripts/${env.SERVICE_FILE}" sh "kubectl -n ${env.DEPLOY_ENV} apply -f scripts/${env.HPA_CONFI_FILE}" break; } } // 检查POD是否替换完毕 sh "/opt/jenkins-nodes-prd/check_tags/check_servre-canary.sh ${env.DEPLOY_ENV} ${params.project_name}" } } } }
0顶
0 踩
共 0 条评论