Jenkins to Nexus with Git Polling

Author, Arun Gupta

Author, Arun Gupta

by Arun Gupta (originally on Miles to Go 2.0)

Build Binaries Only Once is a very important principle of Continuous Deployment (CD). However that blog guides you to build and deploy binaries to Nexus from your development machine. This is fine as a starting step where everything is locally contained on your laptop and you are just testing setup to figure out how things work. But everybody in the team having a local Nexus repository defies the purpose of a “shared repository”.  This is also against Continuous Integration (CI) where the source code committed by different team members checked out and build on a CI server. And CI is a fundamental requirement for Continuous Deployment. How do you set this up then?

You use a CI server to push binaries to Nexus.

There are a varety of CI servers in both open source and commercial range. Jenkins, Travis,  CruiseControl and Go are some of the popular ones in the open source land. They all have a commercial edition as well. Bamboo and AnthillPro are a couple of popular commercial-only offerings. This blog will use the simplest, most popular, and easiest to useJenkins CI server.

The overall flow is shown in the diagram and explained after that.

Jenkins, GitHub, Nexus setup

 

The flow is:

  • Developers push code from inside firewall to GitHub
  • Jenkins is polling GitHub for code updates
  • Build the binaries and push the artifacts to Nexus (inside firewall)

This tech tip will show how to get started with Jenkins and push binaries to Nexus by polling the GitHub workspace. While polling is inefficient, it may be the only, and probably a simplified, choice.

In this setup, Jenkins and Nexus are both setup inside your firewall. This is a more common scenario as at least Nexus would be typically inside firewall. However Jenkins may be configured outside firewall in which case it will be able to archive artifacts but not directly push to Nexus. A proxy needs to be configured for Jenkins and Nexus to communicate in this case.

Lets get started!

Download and Start Jenkins Server

All information about Jenkins can be found at jenkins-ci.org.

1) Download the latest WAR file:

tools> curl -O -L http://mirrors.jenkins-ci.org/war/latest/jenkins.war
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0   328    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 65.3M  100 65.3M    0     0   298k      0  0:03:44  0:03:44 --:--:--  534k

The total time to download will differ based upon your network speed.

2) Start Jenkins as:

java -jar jenkins.war
  1. Starting and Accessing Jenkins provide more details about starting Jenkins and different configuration options.
  2. Once Jenkins is started, it can be accessed at localhost:8080 and shown as:Default Jenkins Output

Install Git Plugin

By default, Jenkins does not have the ability to handle Git workspace. Hopefully this will be fixed in a subsequent release because of INFRA-253. In the meanwhile, lets install Git plugin.

  1. Click on “Manage Jenkins”, “Manage Plugins”
  2. Click on “Available” tab, select “GIT Plugin” and click on “Install without restart”Git plugin installation in Jenkins
  3. Click on “Restart Jenkins …” to restart Jenkins.

Create a Jenkins Job

  1. Configure Maven at Configure System as explained here
  2. Create a new Jenkins job by going to localhost:8080/newJob
  3. Choose “Maven project” and give the name as shown:Jenkins New JobClick on “OK”.
  4. In “Source Code Management”, choose “Git” and specify the repository “https://github.com/javaee-samples/javaee7-simple-sample” as shown:
    Java EE 7 Simple Sample GitHub Repo in Jenkins
  5. In “Build Triggers”, choose “Poll SCM” and specify the schedule to poll the repo every 5 minutes as “H/5 * * * *”:techtip75-polling-schedule
  6. In “Build”, specify “deploy” target as shown:Maven target in Jenkins

Deploy SNAPSHOT to Nexus

Once the setup is done, deploying to Nexus is just a click or a poll away.

  1. Click on Build Now to build the jobJenkins Build Job
  2. Console output for the first job will show something like:
[JENKINS] Archiving /Users/arungupta/.jenkins/jobs/Java EE 7 Simple Sample/workspace/pom.xml to org.javaee7.sample/javaee7-simple-sample/1.10-SNAPSHOT/javaee7-simple-sample-1.10-SNAPSHOT.pom
[JENKINS] Archiving /Users/arungupta/.jenkins/jobs/Java EE 7 Simple Sample/workspace/target/javaee7-simple-sample-1.10-SNAPSHOT.war to org.javaee7.sample/javaee7-simple-sample/1.10-20150305.202625-8/javaee7-simple-sample-1.10-20150305.202625-8.war
channel stopped
Finished: SUCCESS

3) Git Polling Log will show the last poll to your workspace repo. If there are any commits to the workspace after last job then a new job will be started.

This blog showed how to push binaries from Jenkins to Nexus using Git Polling.

Enjoy!

The following two tabs change content below.
Arun Gupta is Director of Developer Advocacy at Red Hat and focuses on JBoss Middleware. As a founding member of the Java EE team at Sun Microsystems, he spread the love for technology all around the world. At Oracle, he led a cross-functional team to drive the global launch of the Java EE 7 platform through strategy, planning, and execution of content, marketing campaigns, and program. After authoring ~1400 blogs at blogs.oracle.com/arungupta on different Java technologies, he continues to promote Red Hat technologies and products at blog.arungupta.me. Arun has extensive speaking experience in 37 countries on myriad topics and is a JavaOne Rockstar. An author of a best-selling book, an avid runner, a globe trotter, a Java Champion, JUG leader, he is easily accessible at @arungupta.
Authors

Related posts

9 Comments

  1. Akshay Kapoor said:

    How about using hooks in Github to trigger Jenkins build job once code is pushed on the repository. This helps get away with regular polling by Jenkins. Flow is something like:

    Developer commits code on Github -> Github post-commit hook triggers Jenkins -> Jenkins pulls code and generates a build

    • Arun Gupta said:

      Sure, that option would work when Jenkins and Nexus are outside firewall. Alternatively there needs to be a mechanism to create a bridge between GitHub (outside firewall) and Jenkins (inside firewall), say using a proxy.

  2. nirish said:

    where was the nexus repository details specified. in the above steps? I thought the nexus repository details are specified in the POM files of the project?

  3. cheqrouni said:

    Thanks for this tutorial. I have a question about how can I deploy a project to nexus in Jenkins without updating the pom.xml and also how can i update the artifcatId and groupId without update the pom.xml
    In advance Thank’s

    • Arun Gupta said:

      Pushing to Nexus is done using mvn install. This can be issued as a command in Jenkins shell.

      artifactId and groupId cannot be overridden on CLI.

  4. Arun Gupta said:

    @cheqrouni mvn install will install to Nexus, you can use this command in the shell script of Jenkins.

    artifactId and groupID cannot be overridden on CLI.

  5. Robert said:

    How do you change the default Nexus URL?

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project javaee7-simple-sample: Failed to retrieve remote metadata org.javaee7.sample:javaee7-simple-sample:1.11-SNAPSHOT/maven-metadata.xml: Could not transfer metadata org.javaee7.sample:javaee7-simple-sample:1.11-SNAPSHOT/maven-metadata.xml from/to deployment (http://localhost:8081/nexus/content/repositories/snapshots/): Connect to localhost:8081 [localhost/127.0.0.1] failed: Connection refused -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

*

Top