jenkins:用jenkins通过ssh部署jar包到远程linux机器(jdk 15 / jenkins 2.257)
一,远程linux机器上安装java15(如已安装,跳过此步)
说明:演示用的linux机器ip: 192.168.1.47
1,下载:
[root@blog ~]# cd /usr/local/source [root@blog source]# mkdir jdk [root@blog source]# cd jdk [root@blog jdk]# wget https://download.oracle.com/otn-pub/java/jdk/15+36/779bf45e88a44cbd9ea6621d33e33db1/jdk-15_linux-x64_bin.tar.gz?AuthParam=1600661691_2c068f0678a8396b65cf132c02446e2a --2020-09-21 12:13:38-- https://download.oracle.com/otn-pub/java/jdk/15+36/779bf45e88a44cbd9ea6621d33e33db1/jdk-15_linux-x64_bin.tar.gz?AuthParam=1600661691_2c068f0678a8396b65cf132c02446e2a Resolving download.oracle.com (download.oracle.com)... 23.211.96.11 Connecting to download.oracle.com (download.oracle.com)|23.211.96.11|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 188017890 (179M) [application/x-gzip] Saving to: ‘jdk-15_linux-x64_bin.tar.gz?AuthParam=1600661691_2c068f0678a8396b65cf132c02446e2a’ jdk-15_linux-x64_bin.tar.gz?AuthParam=16006 100%[==========================================================================================>] 179.31M 12.0MB/s in 14s 2020-09-21 12:13:52 (12.7 MB/s) - ‘jdk-15_linux-x64_bin.tar.gz?AuthParam=1600661691_2c068f0678a8396b65cf132c02446e2a’ saved [188017890/188017890] [root@blog jdk]# ls 'jdk-15_linux-x64_bin.tar.gz?AuthParam=1600661691_2c068f0678a8396b65cf132c02446e2a' [root@blog jdk]# mv jdk-15_linux-x64_bin.tar.gz\?AuthParam\=1600661691_2c068f0678a8396b65cf132c02446e2a jdk-15_linux-x64_bin.tar.gz
2,解压
[root@blog jdk]# tar -zxvf jdk-15_linux-x64_bin.tar.gz
3,移动到安装目录
[root@blog jdk]# mv jdk-15 /usr/local/soft/
4,配置环境变量:
编辑profile文件,把命令保存到文件中
[root@blog jdk]# vi /etc/profile
添加以下命令:
export JAVA_HOME=/usr/local/soft/jdk-15 export CLASSPATH=.:$JAVA_HOME/lib export PATH=$PATH:$JAVA_HOME/bin
使生效:
[root@blog jdk]# source /etc/profile
5,检查安装效果:
[root@blog jdk]# java --version java 15 2020-09-15 Java(TM) SE Runtime Environment (build 15+36-1562) Java HotSpot(TM) 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/25/jenkins-yong-jenkins-tong-guo-ssh-bu-shu-jar-bao-dao-yuan/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,远程服务器(192.168.1.47)端配置启动jar的脚本:
1,创建脚本:
[root@blog java]# mkdir /data/java/tools [root@blog java]# cd tools [root@blog tools]# vi startadmin.sh [root@blog tools]# chmod +x startadmin.sh
脚本内容:
[root@blog tools]# more startadmin.sh #!/bin/bash export BUILD_ID=dontKillme WORKSPACE=/data/java/jar JDK_PATH=/usr/local/soft/jdk-15/bin/java JAR_NAME=demo-0.0.1-SNAPSHOT.jar PID=`ps -ef | grep ${JAR_NAME} | grep -v grep | grep -v startup | awk '{print \$2}'` echo $PID; if [ ! "$PID" ] ;then echo "进程不存在" else echo "进程存在,杀死进程PID$PID" kill -9 $PID fi echo "服务启动开始" nohup ${JDK_PATH} -jar ${WORKSPACE}/${JAR_NAME} &
2,启动脚本上传一个名为demo-0.0.1-SNAPSHOT.jar的jar包到/data/java/jar目录
运行启动jar包的脚本:
[root@blog tools]# /data/java/tools/startadmin.sh 进程不存在 服务启动开始 [root@blog tools]# nohup: appending output to ‘nohup.out'
3,查看端口:检查是否生效?
[root@blog tools]# ss -lntp | grep 8080 LISTEN 0 100 0.0.0.0:8080 0.0.0.0:* users:(("java",pid=11559,fd=11))
可以看到进程已启动
4,通过浏览器测试访问:
http://192.168.1.47:8080/home/home
页面返回:
this is home/home page
说明已生效
三,远程服务器(192.168.1.47)配置sudo
1,编辑sudo文件
[root@blog ~]# visudo
在sudo的规则最后添加下面这一行
webop ALL=(ALL) NOPASSWD:/data/java/tools/startadmin.sh
2,测试sudo规则是否生效?切换到webop账号后测试
[webop@blog ~]$ ps auxfww | grep jar | grep -v grep root 11559 0.4 1.2 7728268 193372 pts/3 Sl 14:47 0:07 /usr/local/soft/jdk-15/bin/java -jar /data/java/jar/demo-0.0.1-SNAPSHOT.jar [webop@blog ~]$ sudo /data/java/tools/startadmin.sh
进程存在,杀死进程PID11559
服务启动开始
[webop@blog ~]$ ps auxfww | grep jar | grep -v grep root 12314 73.4 1.0 7726220 170800 pts/4 Sl 15:15 0:05 /usr/local/soft/jdk-15/bin/java -jar /data/java/jar/demo-0.0.1-SNAPSHOT.jar
进程id发生了变化,说明规则已生效
四,在jenkins服务器上进行配置免密登录
1,生成rsa密钥
[root@localhost ~]# ssh-keygen -m PEM -t rsa -b 4096
2,复制公钥到远程服务器
[root@localhost ~]# ssh-copy-id -p 10108 -i .ssh/id_rsa.pub webop@192.168.1.47
3,测试免密登录:
[root@localhost ~]# ssh -p '10108' webop@192.168.1.47 Welcome to Alibaba Cloud Elastic Compute Service ! Activate the web console with: systemctl enable --now cockpit.socket Last login: Mon Sep 21 15:40:06 2020 from 123.124.125.107 [webop@blog ~]$
无需输入密码即可登录,说明用密钥实现免密登录已生效
五,jenkins上安装Publish Over SSH插件
一,安装插件:
选择 Publish Over SSH
点 直接安装 按钮
六,jenkins上配置ssh服务器
1,manage jenkins->configure system
添加一台服务器:
六,在jenkins的项目中上配置通过ssh发布jar包到远程服务器:
1, 新建item->项目名称: profilemaven,选择:构建一个maven项目
然后点确定
2,对项目做配置:
在github项目中,输入项目url
构建后操作中选择Send build artifacts Over SSH
添加上要传输的jar文件
七,测试效果
构建完成后查看控制台输出:
channel stopped SSH: Connecting from host [localhost.localdomain] SSH: Connecting with configuration [47] ... SSH: EXEC: STDOUT/STDERR from command [sudo /data/java/tools/startadmin.sh] ... 进程存在,杀死进程PID12314 服务启动开始 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.4.RELEASE) 2020-09-21 16:59:27.806 INFO 12662 --- [ main] com.profile.demo.DemoApplication :
然后从服务端可以正常访问
八,查看Jenkins版本
在登录后首页的右下角,可以看到当前的版本:2.257