1、Jenkins环境安装
下载:官网自行下载
环境:Java11
启动命令:java -jar jenkins.war
权限赋予:chmod +x auth-build-by-pom.sh
后台启动命令:
nohup /home/devops/jenkins/jdk-11.0.20/bin/java -jar /home/devops/jenkins/jenkins.war --httpPort=443 > jenkins.log 2>&1 &
gitlab项目:
2、项目打包发布
- 环境
a、maven
b、play 1.5.3
c、java8以上
---------------play启动项目pom.xml增加插件-----------------
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
-----------------根目录增加assembly.xml文件-----------------------
<assembly>
<id>build-assembly</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}/app</directory>
<outputDirectory>/app</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/components</directory>
<outputDirectory>/components</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/conf</directory>
<outputDirectory>/conf</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/lib</directory>
<outputDirectory>/lib</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/assets</directory>
<outputDirectory>/assets</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/modules</directory>
<outputDirectory>/modules</outputDirectory>
</fileSet>
</fileSets>
</assembly>
2)Jenkins全局配置:
Tools
JDK installations
Git installations
Maven installations
---------------------------------------------------------
3)Jenkins构建项目配置参数:
play启动应用:
1、git
2、触发构建条件:Poll SCM (每分钟执行检查一遍代码是否有变动,有则重构建)
* * * * *
3、构建环境:
Add timestamps to the Console Output
Terminate a build if it's stuck
With Ant-java8
4、构建脚本
/home/joke/Documents/auth-build-by-pom.sh "/home/joke/Documents/outs" "dev"
/home/devops/jenkins/shell/play-build.sh "/home/devops/run-project/dev/mall-platform" "dev"
(shell路径 项目部署路径 conf配置环境)
4)jar包应用:
1、git
2、触发构建条件:Poll SCM (每分钟执行检查一遍代码是否有变动,有则重构建)
* * * * *
3、构建环境:
Add timestamps to the Console Output
Terminate a build if it's stuck
With Ant-java8
4、构建脚本
mvn clean install
mvn clean deploy
(发布到在线maven仓库,供所有引用过的play应用重构建应用)
5、引用项目冲构建
Post-build Actions - Build other projects
所有引用到这个jar包的应该都要执行一遍重构建
play应用打包脚本1:
#!/bin/bash
# Author: joke
# Date: 2023-11-01 09:56:33
# Last Modified by: joke
# Description: 通过 maven 管理打包并发布项目
# Example: /home/joke/Documents/play-build.sh "/home/out" "dev"
# 重置 BUILD_ID 环境变量,防止Jenkins build结束后kill掉衍生进程
export BUILD_ID=anyWordIsOk
# 定义部署目录变量
destination_directory="$1"
if [ ! -d "$destination_directory" ]; then
mkdir -p "$destination_directory"
fi
mvn clean install -DskipTests -Dactive.resource="$2"
mvn package -DskipTests
project_path=$(pwd)
cd "$project_path"/target
assembly_zip=$(find . -name "*assembly*.zip" -print -quit)
if [ -n "$assembly_zip" ]; then
mv -f "$assembly_zip" "$destination_directory"
# 进入部署目录,停止服务
cd "$destination_directory"
cd "$destination_directory"/"$(find . -type d -name "*SNAPSHOT*" -print -quit)"
play stop
# 进入部署目录,删除旧版本
cd "$destination_directory"
rm -rf "$(find . -type d -name "*SNAPSHOT*" -print -quit)"
# 解压新版本
unzip -o "$assembly_zip"
# 进入新版本目录,启动服务
cd "$destination_directory"/"$(find . -type d -name "*SNAPSHOT*" -print -quit)"
nohup play start > ./run.out 2>&1 &
max_wait_time=30
current_time=0
while [ "$current_time" -lt "$max_wait_time" ]; do
if [[ $(cat ./logs/system.out) == *"Server is up and running"* ]] && [ "$current_time" -ge 5 ]; then
cat ./logs/system.out
echo "服务已成功启动"
break
fi
sleep 1
current_time=$((current_time + 1))
done
if [ "$current_time" -ge "$max_wait_time" ] || ! [[ $(cat ./logs/system.out) == *"Server is up and running"* ]]; then
echo "启动日志:./logs/system.out"
cat ./logs/system.out
echo "运行日志:./run.out"
cat ./run.out
echo "服务启动失败或等待超时"
exit 1
fi
else
echo "未找到带 'assembly' 字符的 ZIP 文件"
exit 1
fi
jar模块打包脚本2:
#!/bin/bash
# Author: joke
# Date: 2023-11-01 09:56:33
# Last Modified by: joke
# Description: jar项目通过 maven 管理更新并发布项目
# Example: /home/joke/Documents/jar-build.sh
mvn clean install -DskipTests
mvn clean deploy
以上为Jenkins构建的大概思路流程,仅仅满足个人在业务中所设计到的需求,如果需要更加自定义的需求,需要多看Jenkins的配置选项,以及多了解容器化部署的过程,毕竟容器化才是市场主流的方向,如果有需求的话,再把容器化部署的过程简单梳理一下。