Jenkins: deploying war files from artifactory - maven

We are using Jenkins to build (maven) & deploy artifacts (JARs & *WAR*s) to an in-house artifactory server (both snapshots and releases).
For deployment, currently, we got Jenkins jobs that package the war file (from a release scm tag) and deploy to different environments/servers. We want to skip the package phase as it seems unnecessary to package it again & again for a released version because it's not possible to get a different copy of war file even after trying 1000 times.
We are looking for a way in Jenkins to get the artifact (war) from Artifactory and deploy it to a container. I am sure other people would have faced this situation too but I am not able to find any online material regarding this.
Is there any Jenkins plugin that takes a war file from Artifactory (based on a version) and deploy it to a remote container?
If this is not the right way of doing it then what are the recommendations for any other approach?
Thanks

I don't know about a plugin which takes a version # and deploys that, but you can build a Jenkins job to deploy the last successful release to a previous environment (thus copying from DEV-->QA for example.)
To do this, you would use the copy-artifact-plugin.
Here's an easy to follow run-through of this kind of setup:
http://www.lordofthejars.com/2012/09/deploying-jee-artifacts-with-jenkins.html

Every artifact stored in Artifactory will have a unique URL that includes the version number. It will take the format
http://artifactory-server/repository-name/path-to-artifact/version/filename
e.g.
http://artifactory/apps-releases-local/com/yourorg/yourapp/1.5.67/webapp.war
(depending on how you do your packaging, the WAR file name may include the version number as well).
So your deployment job can construct the Artifactory URL and download the file. Depending on how you have security set up in Artifactory, you may need to authenticate the request.

Related

Can I avoid Maven deployed war version with timestamp in Archiva?

In out project we are using using deployed war file in /archiva-2.2.0/repositories/internal/com/xyz/1-SNAPSHOT/proj-1-20160204.122021-15.war path using shell script to copy to jboss. However finding the latest build timestamp and build number is very difficult. Is there a way to avoid using timestamp while storing it in archiva like proj-1-SNAPSHOT.war?
I have already read about <uniqueVersion>false</uniqueVersion> whcih is not supported in maven 3.
I am using archiva-2.2.0, maven-3.3.9.
Maven repositories wasn't designed to distribute deployable components, your continuous integration environment should take care of this. Jenkins has eg Archive Artifacts (native) and Copy Artifact Plugin which you can use to store and recover binaries in your pipelines.
But you can workaround this behavior changing the version of the war to a stable one - removing the -SNAPSHOT suffix. This will lead you with a predictable URL.
I am posting this as it is a new finding, if I wget http://xx.xx.xx.xx:8083/repository/internal/com/xyz/1-SNAPSHOT/proj-1-SNAPSHOT.war. I get the latest deployed artifact in the archiva repository. So archiva does a 302 redirect to point to url of http://xx.xx.xx.xx:8083/repository/internal/com/xyz/1-SNAPSHOT/proj-1-20160204.122021-15.war location and downloads the latest build. I am not sure if this works for other repositories like nexus or artifactory etc.
Ofcourse I will need a cron job to periodically clear of the stale snapshot artifacts.

Deploy latest build from repository to tomcat

What I want to achieve is two step build automation
Step A - Build & Upload to artifact repo
Create build job in Jenkins which will create build after every check-in
Upload every successful build on Archiva server
Step B - Get latest artifact & deploy on required servers
After every desired interval, get latest build from Archiva
Deploy build uploaded in Step A.2 to dev/qa/stg server by unzipping its content in web server directory.
I was able to do achieve Step A by using maven goals in my project pom.xml but any idea/suggestions/best practices for Step B.
I understand/agree, I would need two different jobs having different pom.xml, question remains how will we get latest war from repo in pom and how will be deploy that latest war on remote server by unzipping it, as tomcat there does not have admin module.
I would deploy not a jar/war artifact on step 2, but would create a RPM that contains a needed files structure.
With maven it's quite easy to do with a maven rpm plugin
3-4. Nexus has built in YUM repository support, so you can use yum to install the latest rpm version
So I've always found it better to separate the builds and the deploys.
The schedule for those can be independently managed then.
Assuming you are using linux on the servers you could use the ssh-plugins in jenkins to download the artifact in archiva
wget http://server/repository/internal/group/artifact/version/artifact-version.jar
As for the deploy, you could sftp them over to the deploy server also using jenkins SFTP plugin.

Jenkins - How to pass values from pom.xml to downstream job (free style)

I've set up 2 free style jobs, build-app and deploy-app. build-app poll the scm and builds the app, which is Maven based, and install the artifact in a web server (internal repository server), then it calls deploy-app. I would like to pass the version of the pom file () to the downstream job so it can download the correct artifact and install it on the machine. I found some answers suggesting put the version string in a properties file and use InjectEnv plugin, but I prefer read it from the pom itself. Any ideas?
Thanks!
When you build inside Maven, you have access to the pom file version as ${program.version} and can do anything you wish with it.
The downstream freestyle job can also run Maven using the same pom but a different target. The version should be the same if you take care to keep it from changing in the interim. (This suggests a procedure that should always be followed.)
So, for example, that Maven target can run a Groovy script or an Ant script that will pick up the correct file from the repo and deploy it.

Maven deploy multi module project only if all modules build successfully

I have a maven multi module project with several modules. I want to deploy them (mvn deploy) only if they all pass a full mvn install (which includes the tests).
Currently, I run a mvn install on the project. If all modules pass, I run mvn deploy to do the deployment. The problem I see is the waste of time calling mvn twice (even if I skip tests on the second run).
Does anyone have an idea on this?
EDIT: I have learned that using Artifactory as a repository manager and the maven-artifactory-plugin with your maven setup will add the atomic deploy behaviour to the mvn deploy command. See the Build Integration section in the Artifactory documentation.
[DISCLOSURE - I'm associated with JFrog. Artifactory creator.]
Take a look at the deployAtEnd parameter of Maven Deployment plugin: http://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html
This is a bit tricky. Maven is not atomic when it executes the build life-cycle. So a broken set of artifacts may end up in a repository.
One solution I know is Nexus Pro: http://www.sonatype.com/Products/Nexus-Professional/Features - it allows you to promote builds or define certain repos as staging. So only verified versions get promoted to be used. Maybe artifactory has something similar - I just don't know.
If that solution is too expensive you probably need to create a cleanup build or profile to remove artifacts that where already uploaded. My first guess would be to write a Maven plugin to use the the proxy remote API or maybe the maven features are already sufficient. But since deploy means update the meta-data xml files too I dont think there is a delete - not sure on this either.

Configure Maven or Nexus to link trunk artifact at static URL

My current Jenkins deployment job retrieves war file generated from maven build process from Nexus repository. The deployment is done this way since I can not use hot deployment for my environments. Currently I used parameterized build with Jenkins so I can manually enter the version number for my artifact. Is there a way to configure Maven or Nexus so the artifact generate from the latest trunk build can be accessed from an static URL? For example:
http://mynexus:8081/nexus/content/repository/snapshots/com/somepackage/my-app/trunk/my-app-trunk.war
I don't know a way to do this in Nexus. But you can access the latest successful build from Jenkins, with a URL like this: http://localhost:8080/jenkins/job/jobname/lastSuccessfulBuild/my-app-trunk.war
You have to enable artifact archiving for your war file, then you can access it.
Same issue here, we discovered about :
https://wiki.jenkins-ci.org/display/JENKINS/Maven+Deployment+Linker
Which does the job.
Hope that helps.

Resources