最近做了个Jenkins配合Gitlab自动部署Java项目到Windows Server服务器。
Jenkins和Gitlab在Linux下,好一顿折腾,先记录下脚本,其余后续补充吧。
把Java项目作为服务
https://github.com/winsw/winsw
后端
@echo off
set ymd=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%
set ymd=%ymd: =0%
:: 定义常用文件夹路径
set jarName=jelly-web.jar
SET sourceFolder=E:\jenkins\source\jelly-back\
SET jarFolder=E:\jenkins\source\jelly-back\jelly-web\target\
SET targetFolder=E:\app\jelly\backend\
SET backupFolder=E:\jenkins\backup\
echo 接收到构建任务
E:
echo 进入代码文件夹
cd %sourceFolder%
echo 切换到 master 分支
git checkout master
echo 拉取最新代码
git pull
echo 打包代码
call mvn clean -U install -Dmaven.test.skip=true
echo 打包完成 拷贝jar包
if exist %jarFolder%%jarName% (
rem 停止服务
net stop jelly-back
if exist %targetFolder%%jarName% (
echo 存在旧的构建【%targetFolder%%jarName%】,备份
::把原来发布的版本 备份
copy /y %targetFolder%%jarName% %backupFolder%\jelly-web-%ymd%.jar
echo 删除旧的构建【%targetFolder%%jarName%】
::把发布文件夹的jar包删除
del %targetFolder%%jarName%
)
::把当前gitlab文件夹下的jar包移动到发布文件夹
move %jarFolder%%jarName% %targetFolder%
cd %targetFolder%
net start jelly-back
echo 启动项目【%jarName%】成功
) else (
echo 文件【%jarFolder%%jarName%】不存在,打包失败,请检查!
)
前端
@echo off
set ymd=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%
set ymd=%ymd: =0%
:: 定义常用文件夹路径
set distName=dist
SET sourceFolder=E:\jenkins\source\jelly-ui\
SET distFolder=E:\jenkins\source\jelly-ui\dist\
SET targetFolder=E:\app\jelly\fontend\
SET backupFolder=E:\jenkins\backup\
echo 接收到构建任务
E:
echo 进入代码文件夹
cd %sourceFolder%
echo 切换到 master 分支
git checkout master
echo 拉取最新代码
git pull
echo 打包代码
call npm install
call npm run build:prod
echo 打包完成 拷贝打包后文件
if exist %sourceFolder% (
if exist %targetFolder%%distName%\ (
echo 存在旧的构建【%targetFolder%%distName%】,备份
::把原来发布的版本 备份
xcopy %targetFolder%%distName% %backupFolder%%distName%-%ymd%\ /q /e /r /S /Y
)
::把当前文件夹下的dist移动到发布文件夹
echo 把当前文件夹下的dist移动到发布文件夹
xcopy %distFolder% %targetFolder%%distName%\ /q /e /r /S /Y
) else (
echo 文件【%sourceFolder%】不存在,打包失败,请检查!
)
标签:bat,jarName%,2%,批处理,jelly,echo,targetFolder%,文件夹,打包
From: https://www.cnblogs.com/jellydong/p/16914036.html