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

    parameters {
		string(name:'callBack', defaultValue: 'auto', description: '回调服务地址')
		choice(name:'clusterId',choices:'aws\nyoho-web-gray\nyoho-ops\nyoho-web\nyoho-media\n', description: '集群的信息')
		string(name:'projectName', defaultValue: 'yoho-api-doc', description: 'GITLAB应用名称')
		string(name:'projectGitGroup', defaultValue: 'fe', description: 'GITLAB所在的组')
		string(name:'deployName', defaultValue: 'yoho-api-doc', description: '应用名称')
		string(name:'userMail', defaultValue: 'jimi.ji@yoho.cn', 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'
    }
	
    options {
        //保持构建的最大个数
        buildDiscarder(logRotator(numToKeepStr: '50')) 
    }
	
    stages {
        stage('Declarative: code to images push') {
            steps {
				script {
					echo "PRINT THE  PARA ticketID ${params.ticketID}......"
					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 userMail ${params.userMail}......"
					echo "PRINT THE  PARA imageSource ${params.imageSource}......"	
					if(params.projectName==''){
						error "no project ${params.projectName}"
					}
					def workdir="${env.WORKSPACE}/${params.projectName}"
					sh "git clone -b ${params.branch} git@git.yoho.cn:${params.projectGitGroup}/${params.projectName}.git ${workdir}"
					if(!fileExists("${workdir}/docker.sh")){
						echo "you need docker.sh file in project ${params.projectName}"
						error  "you need docker.sh file in project ${params.projectName}"
					}
					if(!fileExists("${workdir}/Dockerfile")){
						echo "you need Dockerfile file in project ${params.projectName}"
						error  "you need Dockerfile file in project ${params.projectName}"
					}
					sh "sh ${workdir}/docker.sh ${workdir}"	
					def date = new Date()   
					def formattedDate = date.format("yyyMMddHHMMSS")
					def version="${formattedDate}-${BUILD_NUMBER}"
					sh "docker build -t ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${version} ${workdir}"
					sh "docker push ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${version}"
					sh "docker tag  ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${version} ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:latest"
					sh "docker push ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:latest"
					echo "ending build push ${params.deployName}......"
					if(params.callBack!='auto'){
						build job: 'Docker-Image-Deplopy', parameters: [string(name: 'callBack', value: "${params.callBack}"), string(name: 'clusterId', value: "${params.clusterId}"), string(name: 'serviceName', value: "${params.deployName}"), string(name: 'containerName', value: "${params.deployName}"), string(name: 'imageInfo', value: "${params.imageSource}/${params.deployName}:${version}") , string(name: 'deployEnv', value: 'qcloud')]
					}
				}
            }
        }
    }

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