Jenkins自動化集成Pipeline

jenkins自動化集成的pipeline

1.net api項目

pipeline {
    agent any
    tools {
        msbuild 'unix_msbuild'
    }
    options {
        disableConcurrentBuilds()
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '8')
        timestamps()
        timeout(30)
        
    }
    environment {
        //gitlab key唯一標識符
		git_access_key='gzcfe_gitlab_key'
        //部署應用名稱
		deploy_app="項目名稱"
        //部署服務器組
		deploy_host="csharp_onetreetest"
        //拉去分支
		deploy_branch="beta"
		//自動部署分支
		Automation_branch="shunmai_beta"
        //定義生成配置
		build_configuration="Debug"
        //項目目錄
		Primary_path="dev/${JOB_NAME.split('/')[0]}"
        //主文件目錄
		app_dir="${JOB_BASE_NAME}"
		//構建主文件
        csproj_name="${JOB_BASE_NAME}"
        //ansible文檔目錄
        Automation_dir="Automation_onetree_config"
        //ansible應用文檔目錄
        Automation_app_dir="${JOB_NAME.split('/')[0]}"
    }
    stages {
        stage('cleanWs') {
            steps{
                echo '清除環境'
                cleanWs deleteDirs: true, patterns: [[pattern: 'bin', \
                                                    type: 'INCLUDE'], \
                                            [pattern: 'deploy', \
                                                    type: 'INCLUDE'], \
                                            [pattern: 'Automation_onetree_config', \
                                                    type: 'INCLUDE']]
            }
        }
        stage('checkout') {
            options {
                retry(3)
            }
            steps {
                echo '檢查源碼'
                sh 'sleep 1'
                checkout([$class: 'GitSCM', \
                        branches: [[name: "*/${deploy_branch}"]], \
                        doGenerateSubmoduleConfigurations: false, \
                        extensions: [[$class: 'RelativeTargetDirectory', \
                                    relativeTargetDir: "${Primary_path}"]], \
                        submoduleCfg: [], \
                        userRemoteConfigs: [[credentialsId: "${git_access_key}", \
                                            url: 'gitlab項目庫']]])
                checkout([$class: 'GitSCM', \
                        branches: [[name: "*/${deploy_branch}"]], \
                        doGenerateSubmoduleConfigurations: false, \
                        extensions: [[$class: 'RelativeTargetDirectory', \
                                    relativeTargetDir: 'Shunmai-AspNet-Common/AspNet-Common']], \
                        submoduleCfg: [], \
                        userRemoteConfigs: [[credentialsId: "${git_access_key}", \
                                            url: 'http://git.one.com/dev/AspNet-Common.git']]\
                        ])
                checkout([$class: 'GitSCM', \
                        branches: [[name: "*/${Automation_branch}"]], \
                        doGenerateSubmoduleConfigurations: false, \
                        extensions: [[$class: 'RelativeTargetDirectory', \
                                    relativeTargetDir: "${Automation_dir}"]], \
                        submoduleCfg: [], \
                        userRemoteConfigs: [[credentialsId: "${git_access_key}", \
                                            url: 'http://git.one.com/yunwei/Automation_onetree_config.git']]\
                        ])
            }
        }
        stage('clean') {
            steps {
                echo '清除'
                sh "dotnet clean -c ${build_configuration} -o ${WORKSPACE}/bin/${build_configuration}/${app_dir} ${WORKSPACE}/${Primary_path}/${app_dir}/${csproj_name}.csproj"
            }
        }
        stage('restore') {
            steps {
                echo '還原'
                sh "dotnet restore ${WORKSPACE}/${Primary_path}/${app_dir}/${csproj_name}.csproj"
            }
        }
        stage('build') {
            steps {
                echo '構建'
                sh "dotnet build -c ${build_configuration} ${WORKSPACE}/${Primary_path}/${app_dir}/${csproj_name}.csproj"
            }
        }
        stage('publish') {
            steps {
                echo '發佈'
                sh "dotnet publish -c ${build_configuration} -o ${WORKSPACE}/bin/${build_configuration}/${app_dir} ${WORKSPACE}/${Primary_path}/${app_dir}/${csproj_name}.csproj"
            }
        }
        stage('archive') {
            steps {
                echo '歸檔'
                sh 'cd ${WORKSPACE}/bin/${build_configuration} && \
                    tar cf ${csproj_name}.tar ${csproj_name} && \
                    mv ${csproj_name}.tar ${WORKSPACE}'
                archiveArtifacts artifacts: '*.tar', \
                                fingerprint: true
            }
        }
        stage('deploy') {
            steps {
                echo '拷貝歸檔文件'
                copyArtifacts filter: '*.tar', \
                            fingerprintArtifacts: true, \
                            projectName: "${csproj_name}", \
                            selector: specific('${BUILD_NUMBER}'), \
                            target: 'deploy'
                ansiblePlaybook disableHostKeyChecking: true, \
                                extras: '--extra-vars "App_var=${deploy_app} Inventory_var=${deploy_host}"', \
                                installation: 'ansible_2.7', \
                                inventory: "${Automation_dir}/hosts", \
                                playbook: "${Automation_dir}/${Automation_app_dir}/deploy.yml"
            }
        }
    }
}

2. vue項目pipeline


pipeline {
    agent any
    options {
        disableConcurrentBuilds()
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '8')
        timestamps()
        timeout(30)
        
    }
    environment {
        //gitlab key唯一標識符
		git_access_key='gzcfe_gitlab_key'
        //部署應用名稱
		deploy_app="shunmai-pay-vue"
        //部署服務器組
		deploy_host="vue_onetree_beta"
        //拉去分支
		deploy_branch="beta"
		//自動部署分支
		Automation_branch="shunmai_vue_beta"
        //定義生成配置
		build_configuration="test"
        //項目目錄
		Primary_path="${JOB_NAME.split('/')[0]}"
        //主文件目錄
		app_dir="pay.qrcode"
        //ansible文檔目錄
        Automation_dir="Automation_onetree_config"
        //ansible應用文檔目錄
        Automation_app_dir="global_template"
    }
    stages {
        stage('checkout') {
            options {
                retry(3)
            }
            steps {
                echo '檢查源碼'
                sh 'sleep 1'
                checkout([$class: 'GitSCM', \
                        branches: [[name: "*/${deploy_branch}"]], \
                        doGenerateSubmoduleConfigurations: false, \
                        extensions: [[$class: 'RelativeTargetDirectory', \
                                    relativeTargetDir: "${Primary_path}"]], \
                        submoduleCfg: [], \
                        userRemoteConfigs: [[credentialsId: "${git_access_key}", \
                                            url: 'https://git.one.com/dev/shunmai.zhirongyougou.h5client.git']]])
                checkout([$class: 'GitSCM', \
                        branches: [[name: "*/${Automation_branch}"]], \
                        doGenerateSubmoduleConfigurations: false, \
                        extensions: [[$class: 'RelativeTargetDirectory', \
                                    relativeTargetDir: "${Automation_dir}"]], \
                        submoduleCfg: [], \
                        userRemoteConfigs: [[credentialsId: "${git_access_key}", \
                                            url: 'http://git.one.com/yunwei/Automation_onetree_config.git']]\
                        ])
            }
        }
        stage('install') {
            steps {
                echo 'install'
                dir("/${WORKSPACE}/${Primary_path}/${app_dir}"){
                    sh "cnpm install"
                }
            }
        }
        stage('build') {
            steps {
                echo 'build'
                dir("/${WORKSPACE}/${Primary_path}/${app_dir}"){
                    sh "npm run build"
                }
            }
        }
        stage('archive') {
            steps {
                echo '歸檔'
                dir("/${WORKSPACE}/${Primary_path}/${app_dir}/dist/"){
                    sh 'cp -pr ./qrcode/* ./   &&\
                        tar cf ${deploy_app}.tar * && \
                        mv ${deploy_app}.tar ${WORKSPACE}'
                }
                archiveArtifacts artifacts: '*.tar', \
                                fingerprint: true
            }
        }
        stage('deploy') {
            steps {
                echo '拷貝歸檔文件'
                copyArtifacts filter: '*.tar', \
                            fingerprintArtifacts: true, \
                            projectName: "${deploy_app}", \
                            selector: specific('${BUILD_NUMBER}'), \
                            target: 'deploy'
                ansiblePlaybook disableHostKeyChecking: true, \
                                extras: '--extra-vars "App_var=${deploy_app} Inventory_var=${deploy_host}"', \
                                installation: 'ansible_2.7', \
                                inventory: "${Automation_dir}/hosts", \
                                playbook: "${Automation_dir}/${Automation_app_dir}/deploy.yml"
            }
        }
    }
    post {
        always {
            cleanWs deleteDirs: true, notFailBuild: true, patterns: [[pattern: "${JOB_NAME.split('/')[0]}", type: 'INCLUDE'],[pattern: "${deploy_app}.tar", type: 'INCLUDE'] , [pattern: 'deploy', type: 'INCLUDE']]
        }
    }
}

3. java項目的pipeline


pipeline {
    agent any
    tools {
        jdk 'jdk_1.8'
        maven 'Maven_3.5.3'
    }
    options {
        disableConcurrentBuilds()
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
        timestamps()
        timeout(30)
    }
    environment {
        //gitlab key唯一標識符
		git_access_key='gzcfe_gitlab_key'
        //部署應用名稱
	    deploy_app="ace-center"
        //部署服務器組
	    deploy_host="ace_seeker_onetree_test"
	    //併發限制
	    deploy_Serial_number="1"
	    //deploy_Serial_number="[1,2,4]"
	    //deploy_Serial_number="[\"10%\",\"30%\",\"100%\"]"
        //拉去分支
		deploy_branch="testing"
		//自動部署分支
		Automation_branch="testing"
        //定義生成配置
		build_configuration="new_dev"
        //項目目錄
	    Primary_path="${JOB_NAME.split('/')[0]}"
        //主文件目錄
		app_dir="ace-center"
        //ansible文檔目錄
        Automation_dir="Automation_onetree_config"
        //ansible應用文檔目錄
        Automation_app_dir="${JOB_NAME.split('/')[0]}"
    }
    stages {
        stage('checkout') {
            options {
                retry(3)
            }
            steps {
                echo '檢查源碼'
                sh 'sleep 1'
                checkout([$class: 'GitSCM', \
                        branches: [[name: "*/${deploy_branch}"]], \
                        doGenerateSubmoduleConfigurations: false, \
                        extensions: [[$class: 'RelativeTargetDirectory', \
                                    relativeTargetDir: "${Primary_path}"]], \
                        submoduleCfg: [], \
                        userRemoteConfigs: [[credentialsId: "${git_access_key}", \
                                            url: 'http://git.two.com/dev/ace-seeker.git']]])
                checkout([$class: 'GitSCM', \
                        branches: [[name: "*/${Automation_branch}"]], \
                        doGenerateSubmoduleConfigurations: false, \
                        extensions: [[$class: 'RelativeTargetDirectory', \
                                    relativeTargetDir: "${Automation_dir}"]], \
                        submoduleCfg: [], \
                        userRemoteConfigs: [[credentialsId: "${git_access_key}", \
                                            url: 'http://git.two.com/yunwei/Automation_onetree_config.git']]\
                        ])
                
            
            }
        }
        stage('Install ace-core') {
            steps {
                echo '安裝依賴包'
                dir("/${WORKSPACE}/ace-seeker"){
                    sh "mvn --settings ${WORKSPACE}/${Automation_dir}/maven_config/settings.xml -f ace-modules/ace-core/pom.xml clean install"
                }
            }
        }
        stage('Install ace-common') {
            steps {
                echo '安裝依賴包'
                dir("/${WORKSPACE}/ace-seeker"){
                    sh "mvn --settings ${WORKSPACE}/${Automation_dir}/maven_config/settings.xml -f ace-common/pom.xml clean install"
                }
            }
        }
        stage('Install ace-auth') {
            steps {
                echo '安裝依賴包'
                dir("/${WORKSPACE}/ace-seeker"){
                    sh "mvn --settings ${WORKSPACE}/${Automation_dir}/maven_config/settings.xml -f ace-auth/pom.xml clean install"
                }
            }
        }
        stage('build') {
            steps {
                echo '構建'
                dir("/${WORKSPACE}/ace-seeker"){
                    sh "mvn --settings ${WORKSPACE}/${Automation_dir}/maven_config/settings.xml -f ${app_dir}/pom.xml clean package"
                }
            }
            post {
                success {
                    echo '歸檔'
                    dir("/${WORKSPACE}/ace-seeker/${app_dir}/target"){
                    archiveArtifacts artifacts: '*.jar', \
                                fingerprint: true
                    }
                }
            }
        }
        /***
        stage('archive') {
            steps {
                echo '歸檔'
                dir("/${WORKSPACE}/ace-seeker/${app_dir}/target"){
                    archiveArtifacts artifacts: '*.jar', \
                                fingerprint: true, \
                                onlyIfSuccessful: true
                }            
            }
        }
        ***/
        stage('deploy') {
            steps {
                echo '拷貝歸檔文件'
                copyArtifacts filter: '*.jar', \
                            fingerprintArtifacts: true, \
                            projectName: "${deploy_app}", \
                            selector: specific('${BUILD_NUMBER}'), \
                            target: 'deploy'
                ansiblePlaybook disableHostKeyChecking: true, \
                                extras: '--extra-vars "App_var=${deploy_app} Inventory_var=${deploy_host} jenkins_var_config=${build_configuration} Serial_number=${deploy_Serial_number}"', \
                                installation: 'ansible_2.7', \
                                inventory: '${Automation_dir}/hosts', \
                                playbook: "${Automation_dir}/${Automation_app_dir}/deploy.yml"
            }
        }
    }
    post {
        always {
            cleanWs deleteDirs: true, notFailBuild: true, patterns: [[pattern: 'ace-seeker', type: 'INCLUDE'], [pattern: 'deploy', type: 'INCLUDE']]
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章