惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
P
Palo Alto Networks Blog
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AI
AI
G
GRAHAM CLULEY
O
OpenAI News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
L
LINUX DO - 热门话题
S
Schneier on Security
P
Privacy International News Feed
L
Lohrmann on Cybersecurity
SecWiki News
SecWiki News
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
A
Arctic Wolf
C
Cisco Blogs
V2EX - 技术
V2EX - 技术
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
Latest news
Latest news
人人都是产品经理
人人都是产品经理
Schneier on Security
Schneier on Security
Last Week in AI
Last Week in AI
Webroot Blog
Webroot Blog
美团技术团队
N
News and Events Feed by Topic
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
Project Zero
Project Zero
博客园_首页
Cloudbric
Cloudbric
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
The Last Watchdog
The Last Watchdog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tenable Blog
Scott Helme
Scott Helme

博客园 - 正怒月神

windows 安装 openclaw docker-compose 启动 elk linux mysql 备份 maven 打包时优先选择本地仓库 nginx 部署2个相同的vue minio 设置IP MapStruct-plus cannot find converter from MapperStruct 嵌套模型中 List<> 转 List<String> Easy es问题总结 Logstash docker发布 Docker 无法拉取 springmvc 多事务提交和回滚 springMvc 配置 UReport2 java 通过 microsoft graph 调用outlook(三) JPA Example 默认 join jackson.dataformat.xml 反序列化 对象中包含泛型 OR-TOOL 背包算法 解决JIRA、Confluence自动注销登录的问题 java 通过 microsoft graph 调用outlook(二)
jenkins pipeline 发布 jar并运行
正怒月神 · 2024-07-31 · via 博客园 - 正怒月神

废话不多说,上教程

一,配置publish ssh

二,配置 ssh server

记住Name

三 设置发布选项

1 设置模块多选(请安装多选插件:Active Choices Plug-in)

四 编写pipeline

说明:

1 modules.split(',')中的modules就是上面多选框的Name

2 详细解说:

execCommand: 'cd /java ; ps -ef | grep '+module_list[i].trim()+' | grep -v grep | awk \'{print $2}\' | xargs --no-run-if-empty kill -9 ; source /etc/profile ; nohup java -jar '+module_list[i].trim()+'.jar > '+module_list[i].trim()+'.log 2>&1 & '

// 转到目录

cd /java

//通过进程名查找PID

ps -ef | grep '+module_list[i].trim()+' | grep -v grep | awk \'{print $2}\'

//删除此进程(如果为空则不运行kill)

xargs --no-run-if-empty kill -9

//此命令配合nohup,不然Jenkins中无法通过nohup运行Jar

source /etc/profile ;

//后台运行nohup

nohup java -jar '+module_list[i].trim()+'.jar > '+module_list[i].trim()+'.log 2>&1 &

完整代码:

pipeline {
    agent any
        // 设置工具
    tools {
        maven "Maven3"
        jdk "JDK17"
    }
    
    //动态参数
    environment{
         source_dir="${WORKSPACE}"
    }
    
    options {
        timestamps() // 设置在项目打印日志时带上时间戳
        disableConcurrentBuilds() // 不允许同时执行流水线,被用来防止同时访问公共资源等
        timeout(time: 40, unit: 'MINUTES') // 设置流水线运行超时时间,Jenkins自动终止流水线
        buildDiscarder(logRotator(numToKeepStr: '10')) // 保留n次构建历史
    }
    
    // 存放所有任务的合集
    stages {
        stage('clone') {
            steps {
                // 拉取代码
                git credentialsId: 'xxxx-xxxx-xxxx-xxx-xxxxxxxx', 
                url: 'http://localhost:xxxx/xxxxx/kintech-cloud',
                branch: 'dev'
            }
            
        }
        // 打包
        stage('install') {
            steps {
              script {
                //服务名称数组
                module_list = modules.split(',')
                
                for (int i = 0; i < module_list.size(); i++) {
                        echo "=================package ${module_list[i]} "
                        // 打对应的项目以及依赖包
                        bat 'mvn -Dmaven.test.failure.ignore=true -pl kintech-modules/' + module_list[i].trim() + ' -am clean package '
                    }
                echo 'Install success'
               }
            }
    }
        
        // 部署
        stage('deploy') {
            steps {
                script {
                    for (int i = 0; i < module_list.size(); i++) {
                        echo "=================sshPublisher ${module_list[i]} "
                        // scp 免密传输
                        sshPublisher(publishers: [sshPublisherDesc(configName: 'kintech-cloud测试服务', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'ls', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'kintech-modules/'+module_list[i].trim()+'/target/', sourceFiles:  'kintech-modules/'+module_list[i].trim()+ '/target/' +module_list[i].trim()+'.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])    
            
                    }
                    echo 'Deploy success'
                }
            }
        }
 
        // 启动
        stage('start') {
            steps {
                script {
                    for (int i = 0; i < module_list.size(); i++) {
                        //启动 (cd到java目录,杀掉进程,启动jar)
                        sshPublisher(publishers: [sshPublisherDesc(configName: 'kintech-cloud测试服务', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'cd /java ; ps -ef | grep '+module_list[i].trim()+' | grep -v grep | awk \'{print $2}\' | xargs --no-run-if-empty kill -9 ; source /etc/profile ; nohup java -jar '+module_list[i].trim()+'.jar > '+module_list[i].trim()+'.log 2>&1 & ', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '/java', sourceFiles: '/java')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
 
                    }
                    echo 'Start success'
                }
            }
        }
    }
}

五 效果

六 题外话

我的模块层级,略有不同,所以打包路径也不一样。

如下图:

gateway就是一个模块,而kintech-bo模块在总的kintech-modules模块下。

所以发布jar时,打包路径会有所不同。

而我的解决方案是,增加一个选项框,用Referenced parameters 来记录。

最后在pipeline中拼接:modulesPath.trim()+module_list[i]

我的选项

注意,modulesPath带上/

kintech-modules:

 

kintech-gateway: