首页 > 编程语言 >61-CICD持续集成工具-Jenkins自动化部署JAVA程序

61-CICD持续集成工具-Jenkins自动化部署JAVA程序

时间:2023-02-19 15:02:18浏览次数:47  
标签:git JAVA tomcat CICD hello 61 world root war

Blue Ocean 插件实现可视化

注意: 安装完插件,需要重启Jenkins才能生效

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_02



参数化构建

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_03

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_04

执行命令脚本

[root@jenkins script]#cat wheel-deploy-rollback.sh 
#!/bin/bash
#
#********************************************************************
#Description: The test script
#Copyright (C): 2023 All rights reserved
#********************************************************************
HOST_LIST="
10.0.0.203
10.0.0.204
"
APP=wheel
APP_PATH=/var/www/html
DATA_PATH=/opt
DATE=`date +%F_%H-%M-%S`


deploy () {
for i in ${HOST_LIST};do
ssh $i "rm -rf ${APP_PATH} && mkdir -pv ${DATA_PATH}/${APP}-${DATE}"
scp -r * $i:${DATA_PATH}/${APP}-${DATE}
ssh $i "ln -sv ${DATA_PATH}/${APP}-${DATE} ${APP_PATH}"
done
}

rollback() {
for i in ${HOST_LIST};do
CURRENT_VERISION=$(ssh $i "readlink $APP_PATH")
CURRENT_VERISION=$(basename ${CURRENT_VERISION})
echo ${CURRENT_VERISION}
PRE_VERSION=$(ssh $i "ls -1 ${DATA_PATH} | grep -B1 ${CURRENT_VERISION}|head -n1 ")
echo $PRE_VERSION
ssh $i "rm -f ${APP_PATH}&& ln -sv ${DATA_PATH}/${PRE_VERSION} ${APP_PATH}"
done
}


case $1 in
deploy)
deploy
;;
rollback)
rollback
;;
*)
exit
;;
esac

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_gitlab_05



Jenkins利用git-parameter插件实现JAVA项目指定Tag版本部署

  • 更新镜像源到国内网站
[root@jenkins ~]#sed -i.bak 's#updates.jenkins.io/download#mirror.tuna.tsinghua.edu.cn/jenkins#g' /var/lib/jenkins/updates/default.json
[root@jenkins ~]#sed -i 's#www.google.com#www.baidu.com#g' /var/lib/jenkins/updates/default.json
  • 安装git-parameter插件

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_CICD_06

  • Gitlab准备JAVA项目

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_tomcat_07

  • 给项目不同版本版本追加Tag

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_08


[root@gitlab hello-world-war]#vim src/main/webapp/index.jsp 
[root@gitlab hello-world-war]#git commit -am 'v1.0'
[main 9bc3d36] v1.0
Committer: root <[email protected]>
您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确
与否。您可以对其进行设置以免再出现本提示信息。运行如下命令在编辑器
中编辑您的配置文件:

git config --global --edit

设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份:

git commit --amend --reset-author

1 file changed, 2 insertions(+), 2 deletions(-)
[root@gitlab hello-world-war]#git tag v1.0
[root@gitlab hello-world-war]#vim src/main/webapp/index.jsp
[root@gitlab hello-world-war]#git commit -am 'v2.0'
[main 5bbf552] v2.0
Committer: root <[email protected]>
您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确
与否。您可以对其进行设置以免再出现本提示信息。运行如下命令在编辑器
中编辑您的配置文件:

git config --global --edit

设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份:

git commit --amend --reset-author

1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab hello-world-war]#git tag v2.0
[root@gitlab hello-world-war]#vim src/main/webapp/index.jsp
[root@gitlab hello-world-war]#git commit -am 'v3.0'
[main 3a17108] v3.0
Committer: root <[email protected]>
您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确
与否。您可以对其进行设置以免再出现本提示信息。运行如下命令在编辑器
中编辑您的配置文件:

git config --global --edit

设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份:

git commit --amend --reset-author

1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab hello-world-war]#git tag v3.0
[root@gitlab hello-world-war]#git push --tags
枚举对象中: 23, 完成.
对象计数中: 100% (23/23), 完成.
使用 8 个线程进行压缩
压缩对象中: 100% (12/12), 完成.
写入对象中: 100% (18/18), 1.25 KiB | 319.00 KiB/s, 完成.
总共 18(差异 6),复用 0(差异 0),包复用 0
To gitlab.mooreyxia.org:dev/hello-world-war.git
* [new tag] v1.0 -> v1.0
* [new tag] v2.0 -> v2.0
* [new tag] v3.0 -> v3.0
[root@gitlab hello-world-war]#git log
commit 3a17108df5499e33b164c6a5d3c03c1cd905a055 (HEAD -> main, tag: v3.0)
Author: root <[email protected]>
Date: Sat Feb 11 18:12:22 2023 +0800

v3.0

commit 5bbf552dbc61b4e8744b133eba65b4cc657a760c (tag: v2.0)
Author: root <[email protected]>
Date: Sat Feb 11 18:11:57 2023 +0800

v2.0

commit 9bc3d3698b643caaddbe88ab180269b978ad4580 (tag: v1.0)
Author: root <[email protected]>
Date: Sat Feb 11 18:11:24 2023 +0800

v1.0

commit 157ee76ce127d16ae7e5b4d811b08f3df9871a37 (origin/master, origin/main, origin/HEAD)
Merge: 71766c4 fd12765
Author: Eric Savage <[email protected]>
Date: Fri May 23 02:00:17 2014 -0400

Merge branch 'master' of github.com:efsavage/hello-world-war

commit 71766c4c16f1d23a09fdc92cc58358f3db8ad9d5
Author: Eric Savage <[email protected]>
Date: Fri May 23 01:57:25 2014 -0400

Initial commit

commit fd127650656b7fbe6dddca5d2b3b831f4de8b3bc
Author: Eric Savage <[email protected]>
Date: Thu May 22 22:33:46 2014 -0700

Initial commit

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_09

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_10

  • 准备TomCat展示JAVA用
[root@ubuntu2204 ~]#apt install tomcat9  tomcat9-admin tomcat9-common tomcat9-docs tomcat9-examples tomcat9-user
[root@ubuntu2204 ~]#ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 100 *:8080 *:* users:(("java",pid=2992,fd=37))

[root@ubuntu2204 ~]#ls /var/lib/tomcat9/webapps/
ROOT

测试一下

[root@gitlab wheel]#mkdir javaAPP
[root@gitlab wheel]#cd javaAPP/
[root@gitlab javaAPP]#git clone [email protected]:dev/hello-world-war.git
正克隆到 'hello-world-war'...
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 18 (delta 1), reused 18 (delta 1), pack-reused 0
接收对象中: 100% (18/18), 完成.
处理 delta 中: 100% (1/1), 完成.
[root@gitlab javaAPP]#ls
hello-world-war
[root@gitlab javaAPP]#tree hello-world-war/
hello-world-war/
├── dist
│ └── hello-world.war
├── pom.xml
├── README.md
└── src
└── main
└── webapp
├── index.jsp
└── WEB-INF
└── web.xml

5 directories, 5 files
[root@gitlab javaAPP]#scp -r hello-world-war/src/main/webapp/ 10.0.0.203:/var/lib/tomcat9/webapps/hello
The authenticity of host '10.0.0.203 (10.0.0.203)' can't be established.
ED25519 key fingerprint is SHA256:D5IJqoQ+6HEwmjaWfYs8tPfgGdJbWsyMJ/fk/L83np4.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:1: [hashed name]
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.203' (ED25519) to the list of known hosts.
[email protected]'s password:
web.xml 100% 345 284.5KB/s 00:00
index.jsp 100% 204 162.7KB/s 00:00
#203
[root@ubuntu2204 ~]#ls /var/lib/tomcat9/webapps/
hello ROOT
[root@ubuntu2204 ~]#tree /var/lib/tomcat9/webapps/hello/
/var/lib/tomcat9/webapps/hello/
├── index.jsp
└── WEB-INF
└── web.xml

1 directory, 2 files

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_tomcat_11

  • 利用git-parameter插件实现JAVA项目指定Tag版本部署

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_12

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_13

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_CICD_14

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_gitlab_15

准备部署脚本

#执行脚本的前提需要打通机器之间的ssh认证
[root@jenkins script]#cat hello_world_war.sh
#!/bin/bash
#
#********************************************************************
#Description: The test script
#Copyright (C): 2023 All rights reserved
#********************************************************************

APP_TAR_PATH=/data/tomcat/appdir
APP_PATH=/data/tomcat/webdir
APP_DEPLOY_PATH=/var/lib/tomcat9/webapps/hello

DATE=`date +%F_%H-%M-%S`
HOST_LIST="
10.0.0.203
10.0.0.204
"
tar -C $WORKSPACE/src/main/webapp/ -cf hello.tar .
for host in $HOST_LIST;do
scp hello.tar $host:${APP_TAR_PATH}/hello-${DATE}.tar
#在反向代理上先下线要部署的服务器
ssh $host "systemctl stop tomcat9 && \
mkdir ${APP_PATH}/hello-${DATE} && \
tar xf ${APP_TAR_PATH}/hello-${DATE}.tar -C ${APP_PATH}/hello-${DATE} && \
rm -f $APP_DEPLOY_PATH && \
ln -s ${APP_PATH}/hello-${DATE} $APP_DEPLOY_PATH && \
systemctl start tomcat9"
#在反向代理上上线服务器
done

#203 和 204
[root@ubuntu2204 ~]#mkdir -p /data/tomcat/appdir

#测试
[root@jenkins ~]#mkdir /test
[root@jenkins ~]#cd /test/
[root@jenkins test]#git clone [email protected]:dev/hello-world-war.git
Cloning into 'hello-world-war'...
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 18 (delta 1), reused 18 (delta 1), pack-reused 0
Receiving objects: 100% (18/18), done.
Resolving deltas: 100% (1/1), done.
[root@jenkins test]#ls
hello-world-war
[root@jenkins test]#cd hello-world-war/
[root@jenkins hello-world-war]#export WORKSPACE=/test/hello-world-war/
[root@jenkins hello-world-war]#bash /data/script/hello_world_war.sh
hello.tar 100% 10KB 5.3MB/s 00:00
hello.tar
#204 和 204
[root@ubuntu2204 webapps]#tree /data/tomcat/
/data/tomcat/
├── appdir
│ ├── hello-2023-02-11_17-51-15.tar
│ ├── hello-2023-02-11_17-54-59.tar
│ ├── hello-2023-02-11_17-56-05.tar
│ ├── hello-2023-02-11_17-57-40.tar
│ └── hello-2023-02-11_17-59-34.tar
└── webdir
├── hello-2023-02-11_17-57-40
│ ├── index.jsp
│ └── WEB-INF
│ └── web.xml
└── hello-2023-02-11_17-59-34
├── index.jsp
└── WEB-INF
└── web.xml

6 directories, 9 files

[root@ubuntu2204 webapps]#tree /var/lib/tomcat9/
/var/lib/tomcat9/
├── conf -> /etc/tomcat9
├── lib
├── logs -> ../../log/tomcat9
├── policy
│ └── catalina.policy
├── webapps
│ ├── hello -> /data/tomcat/webdir/hello-2023-02-11_17-59-34
│ └── ROOT
│ ├── index.html
│ └── META-INF
│ └── context.xml
└── work -> ../../cache/tomcat9

9 directories, 3 files

#记得测试清理变量
[root@jenkins hello-world-war]#unset WORKSPACE

测试构建部署是否正确

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_CICD_16

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_tomcat_17

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_18



Jenkins利用git-parameter插件实现JAVA项目根据修订版本部署

与上述TAG部署有一点区别

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_19

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_20



Jenkins通过maven和container插件连接tomcat部署基于java的war包

  • 编译安装 tomcat 服务器和配置
#主备文件
[root@ubuntu2204 ~]#ls
apache-tomcat-9.0.71.tar.gz install_tomcat.sh jdk-8u351-linux-x64.tar.gz

#编译安装
TOMCAT_VERSION=9.0.71
JDK_VERSION=8u351

TOMCAT_FILE="apache-tomcat-${TOMCAT_VERSION}.tar.gz"

JDK_FILE="jdk-${JDK_VERSION}-linux-x64.tar.gz"

JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"
DIR=`pwd`

color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$2" && $MOVE_TO_COL
echo -n "["
if [ $1 = "success" -o $1 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $1 = "failure" -o $1 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}



install_jdk(){
if [ ! -f "$DIR/$JDK_FILE" ];then
color 1 "$JDK_FILE 文件不存在"
exit;
elif [ -d $JDK_DIR/jdk ];then
color 1 "JDK 已经安装"
exit
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk* jdk

cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export PATH=\$PATH:\$JAVA_HOME/bin
#export JRE_HOME=\$JAVA_HOME/jre
#export CLASSPATH=.:\$JAVA_HOME/lib/:\$JRE_HOME/lib/
EOF
. /etc/profile.d/jdk.sh
java -version && color 0 "JDK 安装完成" || { color 1 "JDK 安装失败" ; exit; }

}

install_tomcat(){
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
color 1 "$TOMCAT_FILE 文件不存在"
exit;
elif [ -d $TOMCAT_DIR/tomcat ];then
color 1 "TOMCAT 已经安装"
exit
else
[ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd $TOMCAT_DIR && ln -s apache-tomcat-*/ tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH' > /etc/profile.d/tomcat.sh
id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat

cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JDK_DIR/jdk
EOF

chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/

cat > /lib/systemd/system/tomcat.service <<EOF
[Unit]
Descriptinotallow=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target

[Service]
Type=forking
Envirnotallow=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service &> /dev/null
systemctl is-active tomcat.service &> /dev/null && color 0 "TOMCAT 安装完成" || { color 1 "TOMCAT 安装失败" ; exit; }

}

install_jdk

install_tomcat

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_gitlab_21

  • 创建用户和授权

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_22


[root@web1 ~]#find / -name tomcat-users.xml
/etc/tomcat9/tomcat-users.xml
/usr/share/tomcat9/etc/tomcat-users.xml
/usr/share/tomcat9/skel/conf/tomcat-users.xml

[root@web1 ~]#vim /usr/share/tomcat9/etc/tomcat-users.xml
[root@web1 ~]#cat /usr/share/tomcat9/etc/tomcat-users.xml
...
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="123" roles="manager-gui,manager-script"/>
</tomcat-users>

[root@web1 ~]#systemctl restart tomcat9
  • 配置远程连接权限
[root@web1 ~]#dpkg -L tomcat9-admin | grep context.xml
/usr/share/tomcat9-admin/host-manager/META-INF/context.xml
/usr/share/tomcat9-admin/manager/META-INF/context.xml
[root@web1 ~]#vim /usr/share/tomcat9-admin/manager/META-INF/context.xml
[root@web1 ~]#cat /usr/share/tomcat9-admin/manager/META-INF/context.xml
...
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192\.168\.11\.\d+" /> --> 授权连接网段
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

[root@web1 ~]#systemctl restart tomcat9

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_23

  • Jenkins 安装 Maven 和 Tomcat 插件

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_24

  • Jenkins 服务器上安装 maven 和配置镜像加速
[root@jenkins ~]#apt install maven -y
#镜像加速
[root@jenkins ~]#cat /etc/maven/settings.xml|grep mirror
...
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>

[root@jenkins ~]#mvn --version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-60-generic", arch: "amd64", family: "unix"
  • Jenkins 全局工具配置 JDK 和 Maven

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_25

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_CICD_26

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_27

  • 创建 Tomcat 的全局凭据

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_CICD_28

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_29

  • 创建 Maven 风格的任务

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_tomcat_30

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_31

  • 输入maven的编译选项
clean package -Dmaven.test.skip=true

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_32

  • 更改编译后的war包名

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_ubuntu_33

  • 部署构建后war包到tomcat服务器上

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_CICD_34

  • 构建

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_35

[root@jenkins ~]#cd /var/lib/jenkins/workspace/maven-hello-world-war/
[root@jenkins maven-hello-world-war]#ll
total 36
drwxr-xr-x 6 root root 4096 Feb 17 09:23 ./
drwxr-xr-x 3 root root 4096 Feb 17 09:22 ../
drwxr-xr-x 8 root root 4096 Feb 17 09:22 .git/
-rw-r--r-- 1 root root 42 Feb 17 09:22 .gitignore
-rw-r--r-- 1 root root 213 Feb 17 09:22 README.md
drwxr-xr-x 2 root root 4096 Feb 17 09:22 dist/
-rw-r--r-- 1 root root 930 Feb 17 09:22 pom.xml
drwxr-xr-x 3 root root 4096 Feb 17 09:22 src/
drwxr-xr-x 4 root root 4096 Feb 17 09:23 target/
[root@jenkins maven-hello-world-war]#tree target/
target/
├── hello-world-war-1.0.0
│ ├── META-INF
│ ├── WEB-INF
│ │ ├── classes
│ │ └── web.xml
│ └── index.jsp
├── hello-world-war-1.0.0.war
├── hello.war
└── maven-archiver
└── pom.properties

5 directories, 5 files

#确认hello.war复制到tomcat服务器上
[root@web01 ~]#ls /usr/local/tomcat/webapps/
ROOT docs examples hello hello-world-war-1.0.0 hello-world-war-1.0.0.war hello.war host-manager manager
  • 确认tomcat上部署程序是否成功

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_36

  • 尝试升级
#代码更改并提交到gitlab
[root@gitlab data]#vim hello-world-war/src/main/webapp/index.jsp
[root@gitlab data]#cd hello-world-war/
[root@gitlab hello-world-war]#git branch
* main
[root@gitlab hello-world-war]#git commit -am 'v1.0'
[main 8e12062] v1.0
Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

git config --global --edit

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

1 file changed, 2 insertions(+), 2 deletions(-)
[root@gitlab hello-world-war]#git push
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 445 bytes | 445.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.mooreyxia.org:myprj/hello-world-war.git
157ee76..8e12062 main -> main
[root@gitlab hello-world-war]#git log
commit 8e12062eafed08a26c8949be9417e00072ad7e1a (HEAD -> main, origin/main, origin/HEAD)
Author: root <[email protected]>
Date: Fri Feb 17 09:56:18 2023 +0000

v1.0

commit 157ee76ce127d16ae7e5b4d811b08f3df9871a37 (origin/master)
Merge: 71766c4 fd12765
Author: Eric Savage <[email protected]>
Date: Fri May 23 02:00:17 2014 -0400

Merge branch 'master' of github.com:efsavage/hello-world-war

commit 71766c4c16f1d23a09fdc92cc58358f3db8ad9d5
Author: Eric Savage <[email protected]>
Date: Fri May 23 01:57:25 2014 -0400

Initial commit

部署构建后确认tomcat服务更新

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_tomcat_37



jenkins实现spring-boot项目的构建和部署

  • Gitlab 导入项目

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_tomcat_38

  • Jenkins 服务器上安装 maven 和配置镜像加速
[root@jenkins ~]#apt install maven -y
#镜像加速
[root@jenkins ~]#vim /etc/maven/settings.xml
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
#查看相关变量值
[root@jenkins ~]#mvn --version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-60-generic", arch: "amd64", family: "unix"
  • Jenkins 全局工具配置 JDK 和 Maven

参考上一个实验,这里省略

  • 准备相关脚本 - 如遇到java命令未执行,需要解决SSH远程执行命令找不到环境变量的问题
[root@jenkins script]#cat spring-boot-helloworld.sh 
#!/bin/bash

APP_PATH=/data/spring-boot-helloworld/

HOST_LIST="
192.168.11.204
192.168.11.205
"

mvn clean package -Dmaven.test.skip=true

for host in $HOST_LIST;do
ssh $host killall -9 java &> /dev/null
scp target/spring-boot-helloworld-*-SNAPSHOT.jar $host:${APP_PATH}/spring-boot-helloworld.jar
ssh $host "nohup java -jar ${APP_PATH}/spring-boot-helloworld.jar --server.port=8888 &>/dev/null & "&
done


#部署对象服务器要提前建立项目存放路径
[root@web03 ~]#mkdir -p /data/spring-boot-helloworld/
[root@web04 ~]#mkdir -p /data/spring-boot-helloworld/
  • jenkins服务器与部署对象服务器打通ssh验证
[root@jenkins script]#ssh-copy-id 192.168.11.204
[root@jenkins script]#ssh-copy-id 192.168.11.205
  • 部署对象服务器安装java
#编译安装
[root@web03 ~]#ls
install_jdk.sh jdk-8u351-linux-x64.tar.gz

[root@web03 ~]#cat install_jdk.sh
#!/bin/bash

JDK_FILE="jdk-8u351-linux-x64.tar.gz"

JDK_DIR="/usr/local"

DIR=`pwd`

color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$2" && $MOVE_TO_COL
echo -n "["
if [ $1 = "success" -o $1 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $1 = "failure" -o $1 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}

install_jdk(){
if [ ! -f "$DIR/$JDK_FILE" ];then
color 1 "$JDK_FILE 文件不存在"
exit;
elif [ -d $JDK_DIR/jdk ];then
color 1 "JDK 已经安装"
exit
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk* jdk

cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export PATH=\$PATH:\$JAVA_HOME/bin
#export JRE_HOME=\$JAVA_HOME/jre
#export CLASSPATH=.:\$JAVA_HOME/lib/:\$JRE_HOME/lib/
EOF
. /etc/profile.d/jdk.sh
java -version && color 0 "JDK 安装完成" || { color 1 "JDK 安装失败" ; exit; }

}

install_jdk

#bash 安装即可
[root@web03 ~]#java -version
java version "1.8.0_351"
Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)

[root@web04 ~]#java -version
java version "1.8.0_351"
Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)
  • 在 Jenkins 创建 Jenkins任务

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_tomcat_39

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_gitlab_40

  • 构建并检查结果

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_tomcat_41

61-CICD持续集成工具-Jenkins自动化部署JAVA程序_jenkins_42

我是moore,拖更了一阵子,现在继续。大家一起加油!!!

标签:git,JAVA,tomcat,CICD,hello,61,world,root,war
From: https://blog.51cto.com/mooreyxia/6064563

相关文章

  • java的filter如何实现utf-8编码转换
    importjavax.servlet.*;importjavax.servlet.annotation.WebFilter;importjavax.servlet.http.HttpServletRequest;importjava.io.IOException;@WebFilter("/*")......
  • 深入理解Java字符串常量池
    “先从这道面试题开始吧!”Strings=newString("二哥");“这行代码创建了几个对象?”“不就一个吗?”三妹不假思索地回答。“不,两个!”我直接否定了三妹的答案,“使用ne......
  • Java如何判断两个字符串是否相等?
    “这个问题也可以引申为.equals()和‘==’操作符有什么区别。”.equals()就好像我们普通人,看见阿丽塔以为是洛丽塔,看见洛丽塔以为是阿丽塔,看起来一样就觉得她们是同......
  • Java字符串拼接
    “哥,你让我看的《Java开发手册openinnewwindow》上有这么一段内容:循环体内,拼接字符串最好使用StringBuilder的append()方法,而不是+号操作符。这是为什么呀?”三妹......
  • Java字符串分割
    “哥,我感觉字符串拆分没什么可讲的呀,直接上String类的split()方法不就可以了!”三妹毫不客气地说。“假如你真的这么觉得,那可要注意了,事情远没这么简单。”我微笑着说......
  • 漏洞分析-log4j RCE-JAVA篇
    0x00原理分析log4j的介绍:log4j是java打印输出日志的一个API,只要引入了log4j的jar包或者是在xml配置文件内配置好log4j即可输入java运行时产生的日志内容,一般用于记录网......
  • Java入门博客
    开始准备 新建一个file->new->project->emptyproject   新建一个file->new->module->java 打开projectstructure,修改这两处   修改注释的颜色 ......
  • Java基础知识点(带返回值方法的定义和调用
    一:带返回值方法的定义方法的返回值其实就是方法运行的最终结果。如果要在调用处根据方法的结果,去编写另外一段逻辑,为了在调用处拿到方法的结果,就需要定义带返回值的方法。eg......
  • LeetCode-53. 最大子数组和(Java)
    一、前言:......
  • Java:使用thumbnailator实现图片压缩处理
    thumbnailator可以实现图片的压缩、旋转、添加水印文档https://github.com/coobird/thumbnailatorhttps://github.com/coobird/thumbnailator/wiki/Examples依赖<!--......