How can I Trigger local maven project from remote Jenkins? - java

I have created a simple Selenium+java maven project locally on my machine. I am able to execute the testing locally triggering it with Maven.
I want to trigger it from Jenkins (which is installed on a remote machine (my company's QA server))
I am using the option 'custom workspace' of Jenkins.
As the Jenkins is on the server, it's not able to understand/locate the local path
'C:\Automation\MavenProject\'
How I can achieve this?

You can do it with master-slave concepts in Jenkins. Slave machine would be your windows machine which will connect to master Jenkins with the help of some jars. You need to create a node on your Jenkins server and after adding the configuration, you need to download the corresponding slave jars on your machine. Once you execute those jars on your local machine, it will interact with Jenkins server. Your Jenkins job can then perform further activities on your slave machine.
Also in the Jenkins job, you need to refer to this node by enabling the following option.
Restrict where this project can be run

1)Create a Maven Project in Jenkins, you may have to install some plugin if its not visible
Pushing your code to some type of version Control like Github
You need to configure Jenkins to provide GitHub build information and POM Location
Since you Jenkins is hosted up in the server, you have be able to access it public, you need to download Github integration plugin and configure jenkins to provide GitHub hook trigger for GITScm polling and similar action in github itself
This should get you going, also google or youtube, lots of solution , one example
https://www.youtube.com/watch?v=PhxZamqYJws

Related

The correct way to deploy artifact from nexus

I am trying to build and CI environment and I think that it works because I can publish a jar into Nexus automatically.
Git
BitBucket
Jenkins
Nexus OSS
I am looking information about the deployment of maven projects automatically when it publish to nexus , but I am not sure about my initial idea.
My initial idea is to download the latest artifact from Nexus3 and use mvn spring-boot:run but I am not sure about my decission.
I read information about Puppet but I am not sure about this option, could anyone help to me about this process ? Or send to me another link in the StackOVerflow about this problem.
Regards !
Build a pipeline in Jenkins:
First job builds an artifacts and uploads it to the storage (in your case it's Nexus). It has to determine which version it uploaded - the easiest way to do that is to set the version by the job itself and then upload the known version (see a sample script at the bottom).
Then pass the known version to the downstream jobs (like deploying to envs). You'd need to tell Jenkins which version you just deployed - it's possible to write it into a key-value file and then use something like EnvInject plugin to read it as an env var.
To download an artifact from Nexus use wget/curl:
http://nexus.domain/nexus/service/local/artifact/maven/redirect?r={repository}&g={groupId}&a={artifactId}&v={version}&p={type}
Jenkins has several plugins for building pipelines, but you can start with a simple built-in possibility of jobs to invoke other jobs.
Couple of notes:
Do not use Maven for deployment. Maven is a build tool, it's not suitable for deployments. Write bash scripts and possibly use Docker for deployment automation.
Do not use LATEST/SNAPSHOT/RELEASE versions - you need to have a deterministic deployment (if it's repeated with the same params it deploys the same artifact).
A sample script that sets a snapshot-like version (so it's possible to upload the artifact to snapshot Maven repo):
VERSION=`mvn help:evaluate -Dexpression=project.version | grep -v "^\["| grep -v Download`
VERSION=${VERSION/%-SNAPSHOT/} #get rid of -SNAPSHOT if it's there
VERSION="$VERSION-"`date +"%Y%m%d.%H%M%S"`"-$BUILD_NUMBER"
mvn versions:set -DnewVersion=$VERSION

How to rollback on Tomcat in case of unstable build using Jenkins?

I am using Jenkins to set-up a DevOps pipeline for a Java project. And am stuck at one scenario of deployment. If somehow there has to be roll-back of build on Tomcat server and build that has to replace this one has to be last stable build.
Suppose build#20 is deployed on server which is stable and build#21 is deployed in next build cycle but after deployment its found that this new build has issues. Now i want to replace this build by the previous build that is build#20.
The plugin I am using on Jenkins doesn't provide the facility of roll-back. Please help me out.
Plugin for deployment : https://wiki.jenkins-ci.org/display/JENKINS/Deploy+Plugin
Just re-build build#20, call it build#22, and deploy it.
"Deploy" your build results to a deploy-candidate location and only (really) deploy them to Tomcat from there after it has turned out that your complete build process is stable.

How to configure Maven installation in Jenkins per individual Slave node?

Symptom
If I create a Maven job in Jenkins (New Item => Maven project, using the maven plugin) with all defaults and run it, I get this error:
Started by user anonymous
Building on master in workspace /var/lib/jenkins/jobs/job_name/workspace
ERROR: A Maven installation needs to be available for this project to be built.Either your server has no Maven installations defined, or the requested Maven version does not exist.
Finished: FAILURE
Maven runs perfectly from command line with just mvn.
Unacceptable Workaround
There is a system-wide Jenkins configuration for Maven installation: Manage Jenkins => Configure System => Maven / Maven installations. And if I add Maven installation using this web UI (by providing pre-installed path in MAVEN_HOME as /usr/share/maven), the job runs SUCCESSFULLY.
Question: Why is it global Jenkins config and not Slave-specific one?
The global Jenkins config does not make sense: Maven is run per Slave, not per Jenkins.
The zoo of Slaves where Jenkins runs jobs may contain Slaves with various platforms, OSes, environments where different versions of Maven are installed in different locations.
Question: How would I configure Maven installation per Slave node?
Setting environment variables like MAVEN_HOME and M2_HOME to the same path for entire system on Slave node didn't work.
Versions
Both Jenkins Master and Slave are Linux hosts. Jenkins version: 1.598
Assuming you have Java and Maven installed on your slave:
Go to Manage Jenkins -> Manage Nodes
Click configure icon on the node you wish to configure
Scroll down to 'Node Properties' and tick the 'Tool Locations' checkbox
Fill in the options for Java and Maven.
It should now work (even if you have configured a Maven installation on the master).
For me, the solution was to go to Manage Jenkins -> Global Tool Configuration, and set up maven there. You can access this via /configureTools (ex: http://your-ip:8080/jenkins/configureTools )
Example:
Maven installations
Name: my_maven_3.3.9
MAVEN_HOME: /usr/share/maven
Additional configurations for other common tools at /configureTools :
Git installations
Name: my_git_2.11.0
Path to Git executable: /usr/bin/git
JDK installations
Name: my_jdk_1.8.0_161
JAVA_HOME: /opt/jdk/jdk1.8.0_161
If you do NOT add Maven to the Master configuration and you just install it on every slave with their own possibly different environment variables (example with version 3.2.5)
M2_HOME=C:\apache-maven-3.2.5
M2=C:\apache-maven-3.2.5\bin
Path+=;C:\apache-maven-3.2.5\bin
then every Jenkins slave will just use Maven with local settings.
To make it simple, just use my public docker image
docker push chiraggupta95/jenkinswithmvn:v1
In case you want to customize your own image, just check out my git repo for complete instructions.
https://github.com/chiraggupta95apr/jenkins
Happy Coding :)

How can I automatically deploy a war from Nexus to Tomcat?

How can I automatically deploy a war from Nexus to Tomcat?
I have a maven web project which gets built and deployed (both SNAPSHOT and release versions) on Nexus successfully. I would like to know if there is feature/plugin in Nexus where it picks the released war and deploys on remote Tomcat automatically?
I know that you can deploy the war to remote Tomcat using maven-tomcat-plugin but would like to know if there is an alternative solution.
Please guide.
Typically you'd use a CI tool like Jenkins to run the Maven build that publishes your War file into Nexus. Nexus would then be used by whatever tool you're using to push the War onto the target tomcat environment:
There are lots and lots of options.
Jenkins post build SSH script
Run a post-build SSH task from Jenkins that does something like this on the target tomcat server:
curl "http://myrepo/nexus/service/local/artifact/maven/redirect?r=releases&g=myorg&a=myapp&v=1.1&e=war" \
-o /usr/local/share/tomcat7/webapps/myapp.war
service tomcat7 restart
Rundeck
My preference is to use Rundeck for deployments, because it has a Nexus plugin, providing convenient drop-down menus of available releases.
There is also a Rundeck plugin for Jenkins that can be used to orchestrate a CI process with Jenkins performing the build, hand-over to Rundeck for deployment, followed by a Jenkins call-back to run the integration tests.
Chef
I also use chef which can be used to automatically deploy software in a pull fashion.
The artifact cookbook has direct support for Nexus, whereas the application_java cookbook uses a more generic "pull from a URL" approach that also works well.
..
..
The list goes on, so I hope this helps.
We used UrbanCode for the deployment automation, retrieves war from Artifactory/Nexus and deploy to the target server.
I used the Nexus Rest-API, these endpoints downloads the artifact to Jenkins workspace.
In order to deploy Snapshot & Release to Tomcat we can create a Jenkins parameterized job and pass the parameters to the REST endpoint, also to deploy to a server like Tomact "Deploy WAR/EAR" Jenkins plugin will help.
We can parameterize the endpoint and use as part of "Build" step along with "Execute Shell script" option for the build.
wget --user=${UserName} --password=${Password} "http://192.168.49.131:8080/nexus/service/local/artifact/maven/redirect?r=releases&g=${GroupId}&a=${ArtifactId}&v=${Version}&e=${TypeOfArtifact}" --content-disposition
Actual endpoints to Nexus looks something like below.
wget --user=admin --password=admin123 "http://localhost:8080/nexus/service/local/artifact/maven/redirect?r=snapshots&g=org.codezarvis.artifactory&a=hushly&v=0.0.1-SNAPSHOT&e=jar" --content-disposition
wget --user=admin --password=admin123 "http://localhost:8080/nexus/service/local/artifact/maven/redirect?r=releases&g=org.codezarvis.artifactory&a=hushly&v=0.0.5&e=jar" --content-disposition
Thanks
-Sudarshan

Maven/Jenkins automatic download/copy

I'm in a team to develop plugins in java.
We got Maven, a repository and Jenkins,
And I got myself a debian-server to test my applications.
When I push my commits, this happens:
Push in repository, upload and build in Jenkins.
Users download these .jar files and upload these to their server
What I want to be happen:
There are two ways,
First: After building with Jenkins: Download these files,
Second: When pressing "maven build", maven builds my applications and copy these to my server
How can I do this?
(Sorry for my bad English: I'm German)
If I understand you correctly, following scenario should help.
create two jobs
first job builds your artifact and archives it: user will be able to download it, job is triggered by source modification
second job is only executed manually (and requires two plugins, listed below), copies artifacts from first job and then uploads them to the server
Following plugins you would need to install:
copy artifact plugin to enable jenkins to copy artifacts from another job
publish over ssh plugin to enable jenkins to upload binaries and run commands on remote servers
You want when you push your SCM repository, to get Jenkins build into your local projet (update your jars), then manipulate send your final build to your server. Is that you want ?
If yes, I think that plugin would be interesting :
http://evgeny-goldin.com/wiki/Jenkins-maven-plugin#Supported_Plugins
you can manage jenkins goals from your pom.xml but I am not sure if you can get jenkins build.

Categories