eagle 发表于 2020-4-15 11:42:32

jenkins Pipline 庖丁解牛 (二.项目实战)

本帖最后由 eagle 于 2020-4-15 11:51 编辑

一.项目实战1.Pipeline Maven Integration Plugin 构建java 项目

1.安装插件2.设置java 环境变量全局工具设置设置maven
3.设置系统变量系统设置## jdk 环境变量 ###set java JDK export JAVA_HOME=/usr/local/jdk1.8.0_221/export JRE_HOME=/usr/local/jdk1.8.0_221/jreexport PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATHexport CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

4. 创建pipline jobpipeline {      agent any       tools {                              maven 'maven-3'    //这里是全局工具配置的名字,要对应      }    stages {      stage('拉取代码') {             steps {                git url: 'https://gitee.com/tridents_1/blog.git'            }      }      stage('maven 构建') {             steps {                                          sh "mvnclean package-DskipTests"
            }      }
    }}

#这里有个要注意的地方,如果使用自己的私库,要指定一下settings.xml 文件 sh "mvnclean package-DskipTests"替换为:                                    withMaven(                                          maven: 'maven-3',                                           globalMavenSettingsFilePath: '/data/services/maven/conf/settings.xml',                                           mavenSettingsFilePath: '/data/services/maven/conf/settings.xml' ) {                                              sh "mvnclean package-DskipTests"                                    }
2.NodeJS Plugin 构建NodeJS项目1.安装插件插件名称:         NodeJS Plugin2.设置Nodejs 版本全局工具设置3.创建pipline jobpipeline {    agent any     tools {                nodejs "nodejs"   //这里是全局工具配置的名字,要对应    }    stages {      stage('NodeJS 构建') {             steps {                                        sh 'node --version'                                        sh 'npm --version'            }      }
    }}
第一次运行会,自动安装nodejs 下一次运行,会打印出node 版本,npm 版本


4.项目构建pipeline {    agent any     tools {                nodejs "nodejs"   //这里是全局工具配置的名字,要对应    }    stages {      stage('拉取代码') {             steps {                  git url: 'https://gitee.com/tridents_1/vueAdmin-template.git'            }      }      stage('NodeJS 构建') {             steps {                                        sh 'node --version'                                        sh 'npm --version'                                        sh 'npm install --registry=https://registry.npm.taobao.org'                                        sh 'npm run dev'            }      }
    }}构建结果


3.分享一个多语言构建的Pipeline 小栗子pipeline {    agentany    tools {            maven 'maven-3'                  nodejs 'nodejs'         }   stages {      stage('克隆代码') {             steps {                   sh 'echo "service_name": $service_name , "Git_Address:" $git_address ,"Branche_name:" $branche"namespace:" $namespace ,"build_package_type:" $build_package_type, "build_uuid:" $build_uuid"ID:" $ID'                                        dir("${env.WORKSPACE}/$build_uuid") {                      gitbranch: '$branche',credentialsId: 'c7dcd831-8e1c-474f-852b-ee5f04e7ec3f', url: '$git_address'                }            }      }
      stage('JAVA镜像构建') {                   when {                 environment name: 'build_package_type', value: '1'
            }                        steps {                dir("${env.WORKSPACE}/$build_uuid") {                   sh'echo JAVA镜像构建'                                        sh'echo JAVA拉取配置'                                                          sh'/data/scripts/pipeline_scripts/curl_configure.sh "${WORKSPACE}/${build_uuid}"$service_name $branche   $ID $namespace   $build_package_type ${build_uuid}'                                                                                sh'echo JAVA maven构建'                                    withMaven(                                          maven: 'maven-3',                                           globalMavenSettingsFilePath: '/data/services/maven/conf/settings.xml',                                           mavenSettingsFilePath: '/data/services/maven/conf/settings.xml' ) {                                              sh "mvnclean package-DskipTests"                                       }                                         sh'echo JAVA 镜像构建'                                        sh'/data/scripts/pipeline_scripts/java_image_build.sh "${WORKSPACE}/${build_uuid}"$service_name $branche   $ID $namespace   $build_package_type ${build_uuid}'                                       
                }                }      }
      stage('NodeJS镜像构建') {                   when {                 environment name: 'build_package_type', value: '2'
            }            steps {                dir("${env.WORKSPACE}/$build_uuid") {                   sh'echo Node镜像构建'                                        sh'echo Node拉取配置'                            sh'/data/scripts/pipeline_scripts/curl_configure.sh "${WORKSPACE}/${build_uuid}"$service_name $branche   $ID $namespace   $build_package_type ${build_uuid}'                                                                                sh'echo NodeJS构建'                                        sh 'npm install --registry=--registry=https://registry.npm.taobao.org'                                        sh'npm run build:dev'                                        sh'echo NodeJS镜像构建'                                        sh'/data/scripts/pipeline_scripts/node_image_build.sh "${WORKSPACE}/${build_uuid}"$service_name $branche   $ID $namespace   $build_package_type ${build_uuid}'                                                        }                }      }               
      stage('PHP镜像构建') {                   when {                 environment name: 'build_package_type', value: '3'
            }            steps {                dir("${env.WORKSPACE}/$build_uuid") {                   sh'echo PHP镜像构建'                                        sh'echo PHP拉取配置'                            sh'/data/scripts/pipeline_scripts/curl_configure.sh "${WORKSPACE}/${build_uuid}"$service_name $branche   $ID $namespace   $build_package_type ${build_uuid}'                                                                                sh'echo PHP镜像构建'                                        sh'/data/scripts/pipeline_scripts/php_image_build.sh "${WORKSPACE}/${build_uuid}"$service_name $branche   $ID $namespace   $build_package_type ${build_uuid}'                                                          }            
            }            
         }
                stage("删除工作目录") {                        steps{                              script{                                        deleteDir()// 删除工作目录                              }                        }                }


    }}

4.jenkins 钉钉告警1.安装插件2. 创建添加post 部分
      post {                success {                        dingTalk accessToken:'你的钉钉webhook,完整的webhook即可',                         imageUrl:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1568176996966&di=23d1b0bbec5e74070c4a0cff021a7ff4&imgtype=0&src=http%3A%2F%2Fimglf2.ph.126.net%2Fo-A4B-jUEv4florGNvU0jA%3D%3D%2F2650931330678950034.png',                         jenkinsUrl:'jenkins地址/',                         message: "\n服务名称: $service_name \n分支: $branche \n构建类型: $build_package_type \n随机ID: $build_uuid \n环境ID: $ID \n构建结果: 构建成功",                         notifyPeople:''                        }                failure {                        dingTalk accessToken:'你的钉钉webhook,完整的webhook即可',                         imageUrl:'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2523915507,2341968742&fm=26&gp=0.jpg',                         jenkinsUrl:'jenkins地址/',                         message: "\n服务名称: $service_name \n分支: $branche \n构建类型: $build_package_type \n随机ID: $build_uuid \n环境ID: $ID \n构建结果: 构建失败",                         notifyPeople:''                        }                        }
3.告警结果
页: [1]
查看完整版本: jenkins Pipline 庖丁解牛 (二.项目实战)