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

    parameters {
		string(name:'callBack', defaultValue: 'noNeedCallBack', description: '回调服务地址')
		choice(name:'clusterId',choices:'aws\nyoho-media\nyoho-ops\nyoho-web\nyoho-web-gray', description: '集群的信息')
		string(name:'projectName', defaultValue: 'yoho-now', description: 'GITLAB应用名称')
		string(name:'projectGitGroup', defaultValue: 'YOHONow', description: 'GITLAB所在的组')
		string(name:'deployName', defaultValue: 'java-yoho-now', description: '应用名称')
		string(name:'branch', defaultValue: 'master', description: 'git分支名称')
		string(name:'userMail', defaultValue: 'hua.qu@yoho.cn', description: '触发的人员名单')
		choice(name:'nodeEnv',choices:'yoho-gray\nyoho-online\n', description: '选择环境')
		string(name:'imageSource', defaultValue: 'yoho-ops', description: '镜像存储的仓库')
    }
	
    environment{
        CRED_ID='2cb1761b-b258-4e3b-843f-8f0dab16d770'
        QA_EMAIL='hua.qu@yoho.cn'
		DOCKER_BUILD_GIT='git@git.yoho.cn:test/docker-shell.git'	
    }
	
	//保持构建的最大个数
    options {
        buildDiscarder(logRotator(numToKeepStr: '20')) 
    }
	
    //pipeline的各个阶段场景
    stages {
		stage('init && check parameters') { 
			steps {
				script {
				  	if(params.projectName==''){
						error 'no projectName'
					}
				  	if(params.deployName==''){
						error 'no deployName'
					}
				  	if(params.nodeEnv==''){
						error 'no nodeEnv'
					}
					if(params.branch==''){
						error 'no branch'
					}
				  	if(params.imageSource==''){
						error 'no imageSource'
					}
					def date = new Date()   
					def formattedDate = date.format("yyyMMddHHMMSS")
					def version="${formattedDate}-${BUILD_NUMBER}"
					QCLOUD_IMAGE_VERSION="${params.imageSource}/${params.deployName}:${version}"
					PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
					QCLOUD_IMAGE_STORE="ccr.ccs.tencentyun.com/${QCLOUD_IMAGE_VERSION}"
					if(fileExists("${PROJECT_WORK_DIR}/")){
						echo "starting delete the dir ${PROJECT_WORK_DIR}"
						sh "rm -rf ${PROJECT_WORK_DIR}"
					}
				}
				echo "print the para callBack ${params.callBack}......"
				echo "print the para clusterId ${params.clusterId}......"
				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 branch ${params.branch}......"
				echo "print the para nodeEnv ${params.nodeEnv}......"
				echo "print the para userMail ${params.userMail}......"
				echo "print the para imageSource ${params.imageSource}......"
				echo "PRINT THE  PARA PROJECT_WORK_DIR ${PROJECT_WORK_DIR}......"
				echo "PRINT THE  PARA QCLOUD_IMAGE_STORE ${QCLOUD_IMAGE_STORE}......"
			}
		}

        stage('download package script') {
            steps {
				git credentialsId:CRED_ID, url:DOCKER_BUILD_GIT
				script {
					sh "chmod +x ${env.WORKSPACE}/java-new/packageDockerImage.sh ${env.WORKSPACE}/java-new/expectPackage.exp"
				}
            }
        }
		
		stage('package') {
			steps{
				echo "starting to build package"
				script {
					sh "sh ${env.WORKSPACE}/java-new/packageDockerImage.sh -g ${params.projectGitGroup} -b ${params.branch} -a ${params.projectName} -s ${params.nodeEnv} -w ${env.WORKSPACE}"
				}
			}
		}
		
		
		
		stage('buid && push images') {
			steps{
				echo "starting to buid && push images"
				script {
					sh "docker build -t ${QCLOUD_IMAGE_STORE} ${PROJECT_WORK_DIR}"
					sh "docker push ${QCLOUD_IMAGE_STORE}"
				}
			}
		}
		
		stage('deploy to qcloud') {
			steps{
				echo "starting to deploy to qcloud"
				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 notify && clean workspace"
				deleteDir() /*  clean up our workspace */
				if(params.callBack!='noNeedCallBack'){
					sh "curl '${params.callBack}&status=${currentBuild.currentResult}&jobID=${JOB_NAME},${BUILD_NUMBER}'"
				}
				mail to: QA_EMAIL,
				subject: "PineLine '${JOB_NAME}' (${BUILD_NUMBER}),构建结果: ${currentBuild.currentResult}",
				body: "构建的内容为:${params.projectName},提交人员:${params.userMail}"
			}
		}
	}
}