Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

continuous deployment with jenkins

I want to deploy with jenkins to the test environment and to the production environment. To do so I need to connect to the server of the wanted environment, something like ssh/scp.

I would like to know what the best way is.

I found some plugins to do this, like the Jenkins-Deploy-Plug-in or Jenkins Publish over SSH Plugin. The first has lots of issues, which is not really trustworthy to deploy to production and for the second you need to change the global configuration, which is manual work for every deploy.

Any ideas how to solve this? Maybe with some scripts or plugins?

The only current idea I have is: to connect with jenkins to a server (maybe with the SSH Plugin) and to execute there a script that connects to the wished environment. But that are two connections. Is that really neccessary? I hope for a more straightforward way for this.

thanks for any hint.

like image 706
user1338413 Avatar asked Dec 20 '12 16:12

user1338413


People also ask

Can Jenkins do continuous deployment?

Jenkins provides good support for providing continuous deployment and delivery. If you look at the flow of any software development through deployment, it will be as shown below. The main part of Continuous deployment is to ensure that the entire process which is shown above is automated.

What is continuous delivery in Jenkins?

Continuous Delivery is a process, where code changes are automatically built, tested, and prepared for a release to production.

Is Jenkins a CI or CD?

Jenkins is a platform for creating a Continuous Integration/Continuous Delivery (CI/CD) environment. The system offers many different tools, languages, and automation tasks to aid in pipeline creation when developing and deploying programs.


2 Answers

I suggest the following procedure:

one single shell script (stored somewhere on the jenkins server) does everything. Basically, the script does scp of the build artifact and then connects to the server (ssh) and does all the necessary tasks to deploy (setup maintenance page, backup the current app, deploy the new app, ...).

On the jenkins server, there are at least 2 jobs:

  • the first one simply does the build (using maven, or any other build script)
  • the second job does the deploy : so this job only runs the shell script. (I suggest one deploy job for each target environment : testing, production, ...)

It does not require any "special" jenkins plugin to achieve this "one click deployment". It only requires that the jenkins user has ssh access to the target server.

EDIT

Here is a sample shell script to illustrate my post

#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     [email protected]:/tmp/  #connect to the server and execute the deployment script ssh -i <HOME_DIR>/.ssh/id_dsa [email protected]  #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" 
like image 77
ben75 Avatar answered Oct 14 '22 08:10

ben75


Using SSH compromises security on your environment
and is quite hard to troubleshoot.

It is better to install a Jenkins-Slave on the remote machine
and run the tests there by executing a Job on the Slave.

The Slave is monitored by the Server, which saves you a lot of trouble
managing the connection.

You can trigger the remote Job at the end of a successful build
and pass it the artifact of that build.
(can also have the first Job store the artifacts on a shared drive
and pass the location of those artifacts to the next Job).

See here:

  • Jenkins - Distributed builds
  • Jenkins - Parameterized Trigger Plugin
  • Jenkins - Copy Artifact Plugin
like image 43
Gonen Avatar answered Oct 14 '22 08:10

Gonen