与詹金斯一起持续部署

时间:2012-12-20 16:23:54

标签: jenkins jenkins-plugins continuous-deployment

我想用jenkins部署到测试环境和生产环境。为此,我需要连接到所需环境的服务器,例如ssh / scp。

我想知道最好的方法是什么。

我找到了一些插件来执行此操作,例如Jenkins-Deploy-Plug-in或Jenkins Publish over SSH插件。第一个问题很多,部署到生产中并不值得信赖,第二个需要更改全局配置,这是每个部署的手动工作。

任何想法如何解决这个问题?也许有一些脚本或插件?

我目前唯一的想法是:将jenkins连接到服务器(可能使用SSH插件)并在那里执行连接到所需环境的脚本。但这是两个联系。这真的是必要的吗?我希望有一个更简单的方法。

感谢任何提示。

3 个答案:

答案 0 :(得分:21)

我建议采用以下程序:

一个shell脚本(存储在jenkins服务器上的某个地方)可以完成所有操作。 基本上,脚本执行构建工件的scp,然后连接到服务器(ssh)并执行所有必要的部署任务(设置维护页面,备份当前应用程序,部署新应用程序,......)。

在jenkins服务器上,至少有2个作业:

  • 第一个只是构建(使用maven或任何其他构建脚本)
  • 第二个作业执行部署:所以此作业只运行shell脚本。 (我建议为每个目标环境部署一个部署工作:测试,生产......)

它不需要任何“特殊”jenkins插件来实现这种“一键部署”。 它只要求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"

答案 1 :(得分:8)

使用SSH会破坏您环境的安全性 并且很难排除故障。

最好在远程机器上安装 Jenkins-Slave 并通过在Slave上执行Job来运行测试。

奴隶由服务器监控,为您省去了很多麻烦 管理连接。

您可以在成功构建结束时触发远程作业 并传递该构建的神器。
(也可以将第一个作业存储在共享驱动器上的工件上 并将这些工件的位置传递给下一个工作。)

见这里:

答案 2 :(得分:3)

理想情况下,您应该使用FabricCapistrano之类的内容进行部署,并从Jenkins中调用这些脚本。我已经将Capistrano广泛用于Ruby On Rails和非Ruby应用程序。我看到的最大优势是:

  • 内置智能以在部署时出现错误时回滚部署。
  • 它提供的钩子来运行一组脚本,例如数据库迁移,服务重启等。
  • 手动回滚,以备不时之需。