How could I automate build deploy in jenkins?

38,268

Solution 1

One mechanism to deploy off of a build on Jenkins is to use artifacts to place the latest binary in a known location, and then kick off a new job (only on success of the compile/test phase) which uses (private key protected) ssh or scp to copy the artifacts to the test/production machine and then perform the install.

We use a similar mechanism for some automated testing that we do. The tricky part is getting the shell command to handle the ssh keys, so we do the following:

eval `ssh-agent -s`
ssh-add ~/.ssh/your_private_key_here

As long as that private key is on the Jenkins server and the public key is on the server you're trying to push to, you can then use ssh and scp commands in the rest of the script to perform functions on the server in question.

If you prefer to run the process entirely from the target server end, you can create a small script that runs on the server that checks for new files in the artifact directory of your Jenkins server build. Thanks to the latest path, you don't have to know the build number to do this. To find the specific path, you can log in to your Jenkins server (once you've saved at least one artifact), and find the project you are using and look at the Last Successful Artifacts, which will be URLs to the last successful builds of the artifacts. These URLs remain constant and always point at the most recent successful build, so you don't have to worry about them changing unless the project name or server name changes.

NOTE: there are security holes here that you can drive a truck through if you are doing this for anything other than a deployment to test. In the case of the first mechanism, your build server has an ssh key that gives it access (potentially destructive) to the target. In the case of the second mechanism, you are trusting that the Jenkins server will only serve up binaries that are good for you. However, for test environments, push to stage, etc. these techniques will work well.

Solution 2

These are the ways I know:

  • With a script:

In the Jenkins configurations, you can execute windows/shell commands after the execution of your maven goals. In my case, I have a Glassfish on a Linux, and via ssh I execute the asadmin parameters for the deployment. I have installed an instance in the server, and the process that I follow is: stop instance, undeploy app, deploy app, start instance (commands).

  • With a Maven Deploy Plugin:

This plugin takes a war/ear file and deploys that to a running remote application server at the end of a build. The list of currently supported containers include:

Tomcat 4.x/5.x/6.x/7.x JBoss 3.x/4.x Glassfish 2.x/3.x

https://wiki.jenkins-ci.org/display/JENKINS/Deploy+Plugin

  • With Cargo:

The Deploy Plugin is based on this. You must edit your pom.xml and execute the goals of deploy with maven.

http://cargo.codehaus.org/

Share:
38,268
Sujith
Author by

Sujith

Updated on September 11, 2020

Comments

  • Sujith
    Sujith over 3 years

    We are using jenkins for CI. we get late night builds. Is there any way to automate the build deploy as soon as we get a mail or intimation ? Any suggestions would be appreciated..

  • Sujith
    Sujith about 11 years
    Thanks a lot for the response... I have few clarification in this... We used to do deployment testing as well [tats the reason we use the released build number] So in this case it will copy the files and not deploy it actually.So there any way to deploy it using jenkins by the build number? Sorry if my understanding is not correct...
  • gaige
    gaige about 11 years
    @Sujith I don't think I'm following you precisely. The deployment is going to have to be done within the ssh script, since there's no predefined mechanism in Jenkins to deploy arbitrary packages to another server. But, I'm not sure if that's the question you have. If you want to manually deploy, you can set up a Project that requires manual triggering. If you wanted to carry the build number along with, you can use the build number as a variable in the script to pass that along in your ssh command. Can you clarify what you want done with the build number?