文档

https://www.jenkins.io/zh/doc/book/pipeline/

Pipeline 插件

jenkins + gitee + pipline 部署 spring boot_maven

blue ocean

一定要先安装 pipeline , 再安装 blue ocean, 不然会出问题

jenkins + gitee + pipline 部署 spring boot_java_02

进入 blue ocean

jenkins + gitee + pipline 部署 spring boot_maven_03

jenkins + gitee + pipline 部署 spring boot_jenkins_04

hello world

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

下拉框消失的问题: 重启即可

jenkins + gitee + pipline 部署 spring boot_java_05

blue ocean 查看运行结果

jenkins + gitee + pipline 部署 spring boot_jenkins_06

构建一个 maven 项目

拉取代码

生成脚本

点击 流水线语法 

jenkins + gitee + pipline 部署 spring boot_maven_07

选择 片段生成器, 示例步骤选择 checkout: Check out form version control

填写地址

jenkins + gitee + pipline 部署 spring boot_java_08


填写地址,可以增加一些其他配置, 比如分支

jenkins + gitee + pipline 部署 spring boot_jenkins_09

然后复制生成的脚本

jenkins + gitee + pipline 部署 spring boot_java_10

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'git@gitee.com:xxx']])
            }
        }
        
        stage('build project') {
            steps {
                echo 'build project'
            }
        }
        
        stage('publish project') {
            steps {
                echo 'publish project'
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

运行效果

jenkins + gitee + pipline 部署 spring boot_git_11

工作空间也可以看到代码拉取完成

jenkins + gitee + pipline 部署 spring boot_jenkins_12

构建

生成脚本

继续生成脚本. 由于我本地的 JAVA_HOME 指向了 jdk17 , 所以这里临时修改了 JAVA_HOME

jenkins + gitee + pipline 部署 spring boot_git_13

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'git@gitee.com:xxx']])
            }
        }
        
        stage('build project') {
            steps {
                bat '''set JAVA_HOME=D:\\program\\java\\jdk8\\jdk
                        mvn clean package -Dmaven.test.skip=true'''
            }
        }
        
        stage('publish project') {
            steps {
                echo 'publish project'
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

效果

jenkins + gitee + pipline 部署 spring boot_maven_14



重启服务

配置

接下来的三个步骤和 maven 构建一样, 生成脚本选择 sshPublisher: Send build artifacts over SSH , 配置和 maven 构建一样即可

jenkins + gitee + pipline 部署 spring boot_maven_15


最终脚本

pipeline {
    agent any

    stages {
        // 拉取代码
        stage('pull code') {
            steps {
                checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'git@gitee.com:xxxt']])
            }
        }
        
        // 构建项目
        stage('build project') {
            steps {
                bat '''set JAVA_HOME=D:\\program\\java\\jdk8\\jdk
                        mvn clean package -Dmaven.test.skip=true'''
            }
        }
        
        // 停止服务
        stage('stop server') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(
                    configName: 'centos_javaboot', transfers: [sshTransfer(
                        cleanRemote: false, 
                        excludes: '', 
                        execCommand: 'bash /home/laolang/', 
                        flatten: false, 
                        makeEmptyDirs: false, 
                        noDefaultExcludes: false, 
                        patternSeparator: '[, ]+', 
                        remoteDirectory: '', 
                        remoteDirectorySDF: false, 
                        removePrefix: '', 
                        sourceFiles: ''
                    )], 
                    usePromotionTimestamp: false, 
                    useWorkspaceInPromotion: false, 
                    verbose: true
                )])
            }
        }
        
        // 上传 jar 包
        stage('upload jar') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(
                    configName: 'centos_javaboot', 
                        transfers: [sshTransfer(
                            cleanRemote: false, 
                            excludes: '', 
                            execCommand: '', 
                            flatten: false, 
                            makeEmptyDirs: false, 
                            noDefaultExcludes: false, 
                            patternSeparator: '[, ]+', 
                            remoteDirectory: '/app', 
                            remoteDirectorySDF: false, 
                            removePrefix: 'target', 
                            sourceFiles: '**/boot-shop.jar'
                        )], 
                        usePromotionTimestamp: false, 
                        useWorkspaceInPromotion: false, 
                        verbose: true
                    )]
                )
            }
        }
        
        // 启动服务
        stage('start server') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(
                    configName: 'centos_javaboot', transfers: [sshTransfer(
                        cleanRemote: false, 
                        excludes: '', 
                        execCommand: 'cd /home/laolang/app/sbin && ./ start', 
                        flatten: false, 
                        makeEmptyDirs: false, 
                        noDefaultExcludes: false, 
                        patternSeparator: '[, ]+', 
                        remoteDirectory: '', 
                        remoteDirectorySDF: false, 
                        removePrefix: '', 
                        sourceFiles: ''
                    )], 
                    usePromotionTimestamp: false, 
                    useWorkspaceInPromotion: false, 
                    verbose: true
                )])
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}


最终效果

jenkins + gitee + pipline 部署 spring boot_maven_16

把 pipeline 脚本加入到版本控制

pipeline 脚本添加到项目根目录的 Jenkinsfile 文件中, 然后配置如下

jenkins + gitee + pipline 部署 spring boot_java_17

文件名最好不要改

jenkins + gitee + pipline 部署 spring boot_jenkins_18

效果与之前相同

Http Request 插件 集合 sleep

服务启动后除了手动查看之后, 还可以使用脚本判断, pipeline 就是个 groovy 脚本文件, 所以 groovy 的轮子都可以使用

插件 github: https:///jenkinsci/http-request-plugin

步骤手册: https://www.jenkins.io/doc/pipeline/steps/http_request/

参考资料: https://www.cnblogs.com/k4nz/p/14691174.html

如何用 Groovy 做 Json串的生成(序列化)和解析(反序列化)工作

https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#sleep-sleep

关于groovy:Jenkins管道作业:从字符串参数设置睡眠时间?

Groovy 使用 Garpe 依赖管理器

注意: 服务刚刚启动之后接口不能马上访问, 可以使用 sleep time: 10, unit: 'SECONDS'等待10秒钟, 具体数值由实际情况决定 


jenkins + gitee + pipline 部署 spring boot_git_19

例如有如下接口

GET http://192.168.56.105:8094/check/pipeline HTTP/1.1

{
  "code": "200",
  "success": true,
  "msg": "操作成功",
  "body": null,
  "extra": null
}

可以添加如下 pipelien 脚本

pipeline {
    agent any

    stages {
        stage('check server') {
            steps{
                script {
					// 因为服务启动成功后接口不能马上访问, 
                    // 此处为等待 10 秒钟的示例, 具体数值由实际情况决定
                    sleep time: 10, unit: 'SECONDS' 
                    def response = httpRequest  contentType: 'APPLICATION_JSON_UTF8', 
                                                responseHandle: 'LEAVE_OPEN', 
                                                url: 'http://192.168.56.105:8094/check/pipeline', 
                                                validResponseCodes: '200', 
                                                wrapAsMultipart: false
                    println("Response: ${response.content}") 
                    def json = new groovy.json.JsonSlurper().parseText(response.content)
                    println(json.code)
                    assert '200' == json.code : "校验接口业务状态码不是 200"
                    response.close()
                }
            }
        }
    }
}

效果如下

jenkins + gitee + pipline 部署 spring boot_jenkins_20

通过时

jenkins + gitee + pipline 部署 spring boot_maven_21

未通过

jenkins + gitee + pipline 部署 spring boot_maven_22

整体效果如下

jenkins + gitee + pipline 部署 spring boot_java_23

jenkins + gitee + pipline 部署 spring boot_git_24