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

    parameters {
		string(name:'projectName', defaultValue: 'yoho-app-web', description: 'GITLAB应用名称')
		string(name:'projectGitGroup', defaultValue: 'fe', description: 'GITLAB所在的组')
		string(name:'projectBranch', defaultValue: 'release/1.0', description: 'git分支名称')
		string(name:'deployName', defaultValue: 'yoho-app-web', description: '应用名称')
		string(name:'imageSource', defaultValue: 'yoho-online', description: '镜像存储的仓库')
		string(name:'imageVersion', defaultValue: 'release-1.0-20181227144324', description: '镜像存储的版本')
		choice(name:'isNeedCdn',choices:'NO\nYES\n', description: '是否需要上传CDN')
    }
	
	
    //常量参数,初始确定后一般不需更改
    environment{
        CRED_ID='2cb1761b-b258-4e3b-843f-8f0dab16d770'
        QA_EMAIL='jimi.ji@yoho.cn'
        AWS_IMAGE='860721417875.dkr.ecr.cn-north-1.amazonaws.com.cn'
        QCLOUD_IMAGE='ccr.ccs.tencentyun.com'
        PROJECT_WORK_DIR="${env.WORKSPACE}/$params.projectName"
    }
	
    options {
        //保持构建的最大个数
        buildDiscarder(logRotator(numToKeepStr: '20'))
    }
	
    stages {
        stage('Define para && clean workdir'){
            steps{
                script {
                    echo "print the para projectName is : ${params.projectName}......"
                    echo "print the para workdir is : $PROJECT_WORK_DIR......"
                    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}......"
                    if(fileExists("${PROJECT_WORK_DIR}/")){
                        echo "starting delete the dir ${PROJECT_WORK_DIR}"
                        sh "rm -rf ${PROJECT_WORK_DIR}"
                    }
                }
            }
        }

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


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


        stage('upload to CDN and nfs'){
            steps{
                script {
                    println "begin upload to cdn"
                    if(params.isNeedCdn=='YES'){
                        def olddir = "this_is_replace_for_qshell_upload_to_cdn"
                        def repalceDir = "${PROJECT_WORK_DIR}/dist/statics"
                        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......"
                    def qcloudimage = "${env.QCLOUD_IMAGE}/${params.imageSource}/${params.deployName}:${params.imageVersion}"
					sh "docker build -t  ${qcloudimage} ${PROJECT_WORK_DIR}/dist/node"
					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)){
                    	def qcloudimage = "${env.QCLOUD_IMAGE}/${params.imageSource}/${params.deployName}:${params.imageVersion}"
                        def awsimage = "${env.AWS_IMAGE}/${params.imageSource}/${params.deployName}:${params.imageVersion}"
                        //aws docker login session timeout
                        sh '$(/root/.local/bin/aws ecr get-login --no-include-email)'
                        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");
    lst.add("yoho-app-web");
    lst.add("ufo-app-web");
    return lst.contains(project_name);
}