Jenkinsfile 4.59 KB
#!groovy
pipeline {
    agent { label 'master' }

    parameters {
		choice(name:'clusterId',choices:'aws\nyoho-web-gray\nyoho-ops\nyoho-web\nyoho-media\nyoho-java\n', description: '集群的信息')
		string(name:'projectName', defaultValue: 'qmc', description: 'GITLAB应用名称')
		string(name:'projectGitGroup', defaultValue: 'test', description: 'GITLAB所在的组')
		string(name:'deployName', defaultValue: 'yoho-ops-qmc', description: '应用名称')
		string(name:'imageSource', defaultValue: 'yoho-ops', description: '镜像存储的仓库')
		string(name:'branch', defaultValue: 'master', description: 'git分支名称')
    }
	
	
    //常量参数,初始确定后一般不需更改
    environment{
        QA_EMAIL='jimi.ji@yoho.cn'
		NODE_ENV='production'
		KUBERNETES_DEPLOY_YAML='git@git.yoho.cn:docker/k8s-deploy-yaml.git'
    }
	
    options {
        //保持构建的最大个数
        buildDiscarder(logRotator(numToKeepStr: '20')) 
    }
	
    stages {
		stage('init envrionment and global var') { 
			steps {
				script {
					if(params.projectName==''){
						error "no project ${params.projectName}"
					}
					def IMAGEVERSION = new Date().format("yyyyMMddHHmmss")
					IMAGEVERSION = IMAGEVERSION.trim()
					PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
					QCLOUD_IMAGE_STORE="ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${IMAGEVERSION}"
					if(fileExists("${PROJECT_WORK_DIR}/")){
						echo "starting delete the dir ${PROJECT_WORK_DIR}"
						sh "rm -rf ${PROJECT_WORK_DIR}"
					}
					DEPLOY_YAML_WORK_DIR="${env.WORKSPACE}/${k8s-deploy-yaml}"
					if(fileExists("${DEPLOY_YAML_WORK_DIR}/")){
						echo "starting delete the deploy yaml : ${DEPLOY_YAML_WORK_DIR}"
						sh "rm -rf ${DEPLOY_YAML_WORK_DIR}"
					}
				}
				echo "PRINT THE  PARA projectName ${params.projectName}......"
				echo "PRINT THE  PARA projectGitGroup ${params.projectGitGroup}......"
				echo "PRINT THE  PARA deployName ${params.deployName}......"
				echo "PRINT THE  PARA imageSource ${params.imageSource}......"	
				echo "PRINT THE  PARA clusterId ${params.clusterId}......"	
				echo "PRINT THE  PARA branch ${params.branch}......"
				echo "PRINT THE  PARA PROJECT_WORK_DIR ${PROJECT_WORK_DIR}......"
				echo "PRINT THE  PARA QCLOUD_IMAGE_STORE ${QCLOUD_IMAGE_STORE}......"
			}
		}
	
		stage('git clone project code && deploy yaml') { 
			steps {
				script {
					echo "begin clone the project code"
					sh "git clone -b ${params.branch} git@git.yoho.cn:${params.projectGitGroup}/${params.projectName}.git ${PROJECT_WORK_DIR}"
					sh "git clone ${env.KUBERNETES_DEPLOY_YAML} ${DEPLOY_YAML_WORK_DIR}"
					echo "starting check the project ${PROJECT_WORK_DIR}"					
					if(!fileExists("${PROJECT_WORK_DIR}/docker.sh")){
						echo "you need docker.sh file in project ${params.projectName}"
						error  "you need docker.sh file in project ${params.projectName}"
					}
					if(!fileExists("${PROJECT_WORK_DIR}/Dockerfile")){
						echo "you need Dockerfile file in project ${params.projectName}"
						error  "you need Dockerfile file in project ${params.projectName}"
					}
				}
			}
		}	
		
		stage('build clean package project') { 
			steps {
				script {
					sh "sh ${PROJECT_WORK_DIR}/docker.sh ${PROJECT_WORK_DIR}"	
					sh "tree ${PROJECT_WORK_DIR}"	
				}
			}
		}	

		stage('build and push images to qcloud') { 
			steps {
				script {
					sh "docker build -t ${QCLOUD_IMAGE_STORE} ${PROJECT_WORK_DIR}"	
					sh "docker push ${QCLOUD_IMAGE_STORE}"
				}
			}
		}
		
		// stage('deploy to qcloud') { 
		// 	steps {
		// 		script {
		// 			if(params.callBack!='noNeedDeploy'){
		// 				build job: 'Docker-Image-Deplopy', parameters: [
		// 				string(name: 'callBack', value: "noNeedCallBack"), 
		// 				string(name: 'clusterId', value: "${params.clusterId}"), 
		// 				string(name: 'serviceName', value: "${params.deployName}"), 
		// 				string(name: 'containerName', value: "${params.deployName}"), 
		// 				string(name: 'imageInfo', value: "${QCLOUD_IMAGE_VERSION}"), 
		// 				string(name: 'deployEnv', value: 'qcloud')]
		// 			}
		// 		}
		// 	}
		// }
    }

	// post {
	// 	always {
	// 		script {
	// 			echo "Pipeline is done,begin send mail"
	// 			deleteDir() 
	// 			if(params.callBack!='noNeedCallBack'){
	// 				sh "curl '${params.callBack}&status=${currentBuild.currentResult}&jobID=${JOB_NAME},${BUILD_NUMBER}'"
	// 			}
	// 			mail to: "${env.QA_EMAIL},${params.userMail}",
	// 			subject: "PineLine '${JOB_NAME}' (${BUILD_NUMBER}),构建结果: ${currentBuild.currentResult}",
	// 			body: "构建的内容为:${params.projectName},提交人员:${params.userMail}"
	// 		}
	// 	}
	// }
}