2.2 Jenkins部署-pipline方式

 

  • 项目摘要:
  1. 本文档基于文档https://blog.csdn.net/qq_25337221/article/details/106160024编写。

但是还是以新环境为原则来编写。仅仅是部分内容有省略。

  1. 本文档依然以入门级为准。
  • 项目具体实施:
  1. 按上一篇文档安装jdk、git、maven,并且在“系统配置”里配置。
  2. 创建pipline任务

2.2 Jenkins部署-pipline方式_第1张图片
选择新建任务,选择流水线,填写任务名称为:springboot-demo-pipline

  1. 流水线

   这一块是最为重要的内容,这一块将按循序渐进的方式进行。这里采用软件VSCode编写Jenkinsfile的代码。VSCode软件中可以安装Jenkinsfile支持插件。由于这是入门案例,所以代码是直接黏贴到Jenkins页面的。

  1. 配置几个环境变量

选择“Pipline script”

添加如下代码:

pipeline{

    agent any

     tools {

        maven 'maven3.6.3'

        jdk 'jdk1.8'

    }

     environment{

        TASK_NAME="springboot-demo-pipline"

        ROOT_PROJECT_PATH="user-system"

        USER_SERVICE_PROJECT="user-service-0.0.1-SNAPSHOT.jar"

        USER_SERVICE_PROJECT_NAME="user-service"  

    }

    options{

        buildDiscarder(logRotator(numToKeepStr: '10'))

        timestamps()

    }

      triggers{

         pollSCM('H/1 * * * *')

    }

    /*后续其他代码*/

}

  1. 从git中拉取代码

2.2 Jenkins部署-pipline方式_第2张图片
选择“流水线语法”中,生成对应的git代码。

该脚本完整代码如下:

pipeline{

     /*第一部分代码*/

    stages{

        stage('Preparation'){

            steps{

                echo 'pulling code'

                /*生成的git代码*/

            }

        }

  }

}

这一步可以进行测试构建。

  1. build项目

完整代码如下:

stage('Build'){

         steps{

           script{

           echo 'Stopping SpringBoot Application'

           pid= sh (script:"ps -ef | grep $USER_SERVICE_PROJECT | grep -v grep | awk '{print \$2}'",returnStdout: true).trim()

           if (pid!=""){

            echo "Kill process: $pid"

            sh "kill -9 $pid"

            }

            echo 'starting build'

            sh 'mvn -f $ROOT_PROJECT_PATH  clean install -Dmaven.test.skip=true'

            echo 'deleteing old project'      

            sh 'rm -rf /usr/local/src/java/$USER_SERVICE_PROJECT'

            echo 'copy new project'

            sh 'cp /var/lib/jenkins/workspace/$TASK_NAME/$USER_SERVICE_PROJECT_NAME/target/$USER_SERVICE_PROJECT /usr/local/src/java/'

            echo "starting project" 

            sh 'JENKINS_NODE_COOKIE=dontKillMe nohup java -jar /usr/local/src/java/$USER_SERVICE_PROJECT >log.txt  2>&1 &'

            echo "startup success!"

           }             

         }     

      }

此时,再次测试构建项目。

  1. 添加QQ邮件通知

在上一篇配置 "系统配置"的基础上,在pipeline script中加入如下代码:

pipeline{

      /*其他代码*/

      post {

        success {

            emailext (

                subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",

                body: """

SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':

                   

Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"

""",

                to: "[email protected],[email protected]",

                from: "[email protected]"

            )

        }

        failure {

            emailext (

                subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",

                body: """

FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':

                   

Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"

""",

                to: "[email protected],[email protected]",

                from: "[email protected]"

            )

        }

    }

}

  1. 添加slack通知

在上一篇的“系统配置“基础上,在pipeline script中添加如下代码:

    post {

        success {

/*省略email代码*/

              slackSend channel: "#test-jenkins", message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'           

        }

        failure {

            /*省略email代码*/

            slackSend channel: "#test-jenkins", message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'

        }

    }

再次构建项目,slack和邮箱均收到信息。

  1. 完整代码如下(git和邮箱地址需要补齐):

pipeline{

    agent any

     tools {

        maven 'maven3.6.3'

        jdk 'jdk1.8'

    }

     environment{

        TASK_NAME="springboot-demo-pipline"

        ROOT_PROJECT_PATH="user-system"

        USER_SERVICE_PROJECT="user-service-0.0.1-SNAPSHOT.jar"

        USER_SERVICE_PROJECT_NAME="user-service"  

    }

    options{

        buildDiscarder(logRotator(numToKeepStr: '10'))

        timestamps()

    }

      triggers{

         pollSCM('H/1 * * * *')

    }

   

    stages{

        stage('Preparation'){

            steps{

                echo 'pulling code'

                git credentialsId:' ', url: ' '  /*此处补齐*/

            }

        }

        stage('Build'){

            steps{

           script{

           echo 'Stopping SpringBoot Application'

           pid= sh (script:"ps -ef | grep $USER_SERVICE_PROJECT | grep -v grep | awk '{print \$2}'",returnStdout: true).trim()

           if (pid!=""){

            echo "Kill process: $pid"

            sh "kill -9 $pid"

            }

            echo 'starting build'

            sh 'mvn -f $ROOT_PROJECT_PATH  clean install -Dmaven.test.skip=true'

            echo 'deleteing old project'      

            sh 'rm -rf /usr/local/src/java/$USER_SERVICE_PROJECT'

            echo 'copy new project'

            sh 'cp /var/lib/jenkins/workspace/$TASK_NAME/$USER_SERVICE_PROJECT_NAME/target/$USER_SERVICE_PROJECT /usr/local/src/java/'

            echo "starting project" 

            sh 'JENKINS_NODE_COOKIE=dontKillMe nohup java -jar /usr/local/src/java/$USER_SERVICE_PROJECT >log.txt  2>&1 &'

            echo "startup success!"

           }             

         }     

      }

  }

      post {

        success {

            emailext (

                subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",

                body: """

SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':

                   

Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"

""",

                to: "[email protected],[email protected]",

                from: "[email protected]"

            )

          slackSend channel: "#test-jenkins", message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'           

        }

        failure {

            emailext (

                subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",

                body: """

FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':

                   

Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"

""",

                to: "[email protected],[email protected]",

                from: "[email protected]"

            )

            slackSend channel: "#test-jenkins", message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'

        }

    }

}

     源码及其文档下载地址:https://download.csdn.net/download/qq_25337221/12436082

  • 参考链接:
  1. 参考书:《Jenkins 2.x 实践指南》,当前暂未搜到相关pdf文件。
  2. Jenkins pipeline 入门: https://www.jianshu.com/p/0ce155d9d893
  3. Jenkins中springboot项目pipline:https://www.cnblogs.com/sidesky/p/10679490.html
  4. pipeline基础知识:https://blog.csdn.net/weixin_41988331/article/details/81672128
  5. pipeline邮件通知:https://www.jianshu.com/p/c8fab60ed58e
  6. pipeline中slack通知:https://cloud.tencent.com/developer/article/1469020

 

 

本内容由安康学院“雨季”原创。

你可能感兴趣的:(项目实战)