我建议以下程序:
一个单一的 shell 脚本(存储在 jenkins 服务器上的某处)可以完成所有工作。基本上,该脚本执行构建工件的 scp,然后连接到服务器 (ssh) 并执行所有必要的部署任务(设置维护页面、备份当前应用程序、部署新应用程序,...)。
在 jenkins 服务器上,至少有 2 个作业:
- 第一个只是进行构建(使用 maven 或任何其他构建脚本)
- 第二个工作是部署:所以这个工作只运行 shell 脚本。(我建议为每个目标环境执行一项部署工作:测试、生产……)
它不需要任何“特殊”的詹金斯插件来实现这种“一键部署”。它只要求 jenkins 用户对目标服务器具有 ssh 访问权限。
编辑
这是一个示例 shell 脚本来说明我的帖子
#This script will copy the last artifact build by the job "MyApp" to test.myserver.com
#and remotely execute the deployment script.
#copy the war to the server
#(the job "MyApp" is using maven, that's why the war can be found at this location)
scp -i <HOME_DIR>/.ssh/id_dsa $HUDSON_HOME/jobs/MyApp_Build/workspace/myapp/target/myapp.war deployeruser@test.myserver.com:/tmp/
#connect to the server and execute the deployment script
ssh -i <HOME_DIR>/.ssh/id_dsa deployeruser@test.myserver.com
#The following is just an example of what a deployment script can be.
#of course you must adapt it to your needs and environment
"cd <TOMCAT_DIR>;
#first copy the current war to a backup directory (additionaly, I have a cron task deleting old undeployed apps)
cp -rf myapp-apps/myapp* undeployed/myapp-apps/;
#execute a script (stored on the server) to properly stop the app
sh bin/myapp.sh stop;
#delete current app
rm -rf myapp-apps/myapp;
rm -rf myapp-apps/myapp.war;
#copy the uploaded war in tomcat app directory
cp /tmp/myapp.war myapp-apps/;
#execute a script (stored on the server) to start the app
sh bin/myapp.sh start"