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

    parameters {
		string(name:'ticketID', defaultValue: 'auto', description: '工单ID')
		string(name:'projectName', defaultValue: 'yoho-api-doc', description: 'GITLAB应用名称')
		string(name:'projectGitGroup', defaultValue: 'fe', description: 'GITLAB所在的组')
		string(name:'projectBranch', defaultValue: 'master', description: 'git分支名称')
		//deployName 对应了镜像名称,服务名称,aws中任务,以及服务总称
		string(name:'deployName', defaultValue: 'yoho-api-doc', description: '应用名称,对应k8s的服务名,aws中任务以及服务的名称')
		string(name:'userMail', defaultValue: 'jimi.ji@yoho.cn', description: '触发的人员邮箱')
		string(name:'imageSource', defaultValue: 'yoho-ops', description: '镜像存储的仓库')
    }
    //常量参数,初始确定后一般不需更改
    environment{
        QA_EMAIL='jimi.ji@yoho.cn'
		NODE_ENV='production'
		OPS_CALL_BACK='http://www.yohops.com/outer/updateJenkinsStatus?'	
    }
	
    options {
        //保持构建的最大个数
        buildDiscarder(logRotator(numToKeepStr: '50')) 
    }


    stages {
		stage("Define the basis para"){
			steps{
				script {

				}
			}

		}




		stage("git clone project : ${params.projectName}"){
			steps{
				script {

				}
			}

		}





        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}......"
				}
            }
        }
    }

	post {
		always {
			echo "Pipeline is done,begin send mail"
			deleteDir() /* clean up our workspace */
			println "###############################"
			sh "curl '${OPS_CALL_BACK}ticketID=${params.ticketID}&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}"
			
		}
	}
}