欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Jenkins自动化集成Pipeline

程序员文章站 2022-05-08 17:40:45
...

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']]
        }
    }
}

 

相关标签: jenkins pipeline