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

    parameters {
		string(name:'projectName', defaultValue: 'yohoblk-wap', description: 'GITLAB应用名称')
		string(name:'projectGitGroup', defaultValue: 'fe', description: 'GITLAB所在的组')
		string(name:'projectBranch', defaultValue: 'feature/docker', description: 'git分支名称')
		string(name:'deployName', defaultValue: 'node-yohoblk-wap', description: '应用名称')
		string(name:'imageSource', defaultValue: 'yoho-online', description: '镜像存储的仓库')
		string(name:'imageVersion', defaultValue: '20180714999', description: '镜像存储的版本')
		string(name:'isNeedCdn',defaultValue:'NO', description: '是否需要上传CDN')
    }
	
	
    //常量参数,初始确定后一般不需更改
    environment{
        CRED_ID='2cb1761b-b258-4e3b-843f-8f0dab16d770'
        QA_EMAIL='jimi.ji@yoho.cn'	
    }
	
    options {
        //保持构建的最大个数
        buildDiscarder(logRotator(numToKeepStr: '50')) 
    }
	
    stages {

        stage('Define para && clean workdir'){
            steps{
                script {
                    def workdir="${env.WORKSPACE}/$params.projectName"
                    echo "print the para projectName is : ${params.projectName}......"
                    echo "print the para workdir is : ${workdir}......"
                    echo "print the para projectGitGroup is : ${params.projectGitGroup}......"
                    echo "print the para deployName is : ${params.deployName}......"
                    echo "print the para branch is : ${params.projectBranch}......"
                    echo "print the para isNeedCdn is : ${params.isNeedCdn}......"
                    echo "print the para imageSource is : ${params.imageSource}......"
                    echo "print the para imageVersion is : ${params.imageVersion}......"
                    def localdir = "${workdir}/docker.sh"
                    def qcloudimage = "ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${params.imageVersion}"
                    def awsimage = "860721417875.dkr.ecr.cn-north-1.amazonaws.com.cn/${params.imageSource}/${params.deployName}:${params.imageVersion}"
                    if(fileExists("$localdir")){
                        echo "starting delete the dir ${workdir}"
                        sh "rm -rf ${workdir}"
                    }
                }
            }
        }

        stage('git clone project'){
            steps{
                script {
					sh "git clone -b ${params.projectBranch} git@git.yoho.cn:${params.projectGitGroup}/${params.projectName}.git ${workdir}"
                }
            }
        }


        stage('build project'){
            steps{
                script {
					sh "cd ${workdir} && sh ${workdir}/docker.sh ${params.isNeedCdn} ${workdir}"
                }
            }
        }


        stage('upload to CDN and nfs'){
            steps{
                script {
                    if(params.isNeedCdn=='YES'){
                        def olddir = "/data/var-lib/jenkins/workspace/replace"
                        def repalceDir = "${workdir}/public/dist"
                        println repalceDir
                        sh "sed -i 's?${olddir}?${repalceDir}?g'  ${env.WORKSPACE}/Docker-Node-Build/LocalUploadConfig"
             //           sh "/home/soft/qshell qupload 3 ${env.WORKSPACE}/Docker-Node-Build/LocalUploadConfig"
               //         sh "cp -r ${repalceDir} /nfs-ops/Data/app/node-ci/data"
                    }else {
                        println "params.isNeedCdn is not yes,no upload"
                    }
                }
            }
        }

        stage('build and push images to qcloud') {
            steps {
				script {
					echo "begin bake image......"
					sh "docker build -t  ${qcloudimage} ${workdir}"
					sh "docker push ${qcloudimage}"
					echo "end bake qcloud image......"
				}
            }
        }

        stage('build and push images to aws') {
            steps {
                script {
                    if(check_push_aws_images(params.projectName)){
                        sh "docker tag  ${qcloudimage} ${awsimage} "
                        sh "docker push ${awsimage}"
                        echo "upload images to aws : ${params.projectName}"
                    }else {
                        println "there is no need to upload images to aws : ${params.projectName}"
                    }
                }
            }
        }
    }

	post {
		always {
		    echo "clean up our workspace"
		 /*  deleteDir() clean up our workspace */
		}
	}
}

def check_push_aws_images(project_name){
    println "begin check push aws images";
    def lst = [];
    lst.add("yohobuywap-node");
    lst.add("yohobuy-node");
    return lst.contains(project_name);
}