About Lubos Krnac

Lubos is a Java/JavaScript developer. His religion is constantly improve his developments skills according to best practices. Strongly believes that TDD drives better design and nicely decoupled code. Past experience include C++, Assembler and C#.

Trigger Continuous Delivery every GitHub commit

Crucial piece of puzzle when developing web application is Continuous Delivery. Testers or users can by early access to alpha version contribute to development process. Design,  requirements, architecture or performance problems can be catched much sooner.

I am going to show how to set up this process with usage of Maven and Jenkins. Target environment is hosted on Tomcat7. Source code is hosted on GitHub. Because I am type of developer that tries to avoid polling as much as possible, I am going to show how to trigger this process by GitHub’s cool feature called WebHooks.
 
 

1. Create Continuous Delivery job

Creation of Jenkins job and integrating it with Maven is very easy. Will quickly cover this:

  • Create it with “New Item” -> “
  • Set up GitHub URL in section “Source Code Management” (Authentication is not needed in my case, because my GitHub repository is public)
  • Skip section “Build Triggers” for now, will come back to this later.
  • Configure “Build” section with POM path and goals you are using for building your WAR file
  • Set up “Build Settings” -> “E-Mail Notification”

Save and try to run the Jenkins job. This is very common and basic Jenkins job configuration. Now we are about to set up deployment of WAR file into Tomcat7. But here comes dilemma into play. There are two very mature ways for deployment. I will cover both and let reader pick one.

a) Continuous Delivery with usage of tomcat7-maven-plugin

  • First of all we need to enable access into Tomcat7. Edit $CATALINA_HOME/conf/tomcat-users.xml (CATALINA_HOME is Tomcat’s home directory) and configure role and user as follows
<role rolename="manager-script"/>
<user username="deployer" password="===PASSWORD===" roles="manager-script"/>
  • Configure Tomcat7 credentials for Maven in configuration file settings.xml. This file is usually located in <user_home>/.m2.
<server>
	<id>tomcat-server-alpha</id>
	<username>deployer</username>
	<password>===PASSWORD===</password>
</server>
  • Set up tomcat7-maven-plugin into pom.xml
<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.2</version>
	<configuration>
		<url>http://[JENKINS-URL]/manager/text</url>
		<server>tomcat-server-alpha</server>
		<path>/[TOMCAT-DEPLOY-PATH]</path>
	</configuration>
</plugin>
  • Lastly add additional Maven goal “tomcat7:redeploy” into Jenkins job

b) Continuous Delivery with usage of Jenkins Deploy plugin

  • Install Jenkins Deploy plugin
  • In Jenkins job that builds WAR, configure “Add post-build action” -> “Deploy ear/war to container”

jenkins-tomcat-deploy

2. Jenkins – GitHub integration

  • Blocking requirement here is to have Jenkins server accessible from web. If you can’t for whatever reason, you must stick with polling the source control in Jenkins.
  • Install GitHub plugin into Jenkins
  • Generate Personal access token in GitHub for Jenkins. This can be found under “Edit Your Profile” -> “Applications”

github-generate-new-token

  • Set up GitHub plugin to use generated token in Jenkins. You can find this section in “Manage Jenkins” -> “Configure System” -> “GitHub Web Hook”. Note that you don’t need to use password. API URL is “https://api.github.com”

jenkins-github-web-hook

  • Create WebHook in Github. Open repository -> “Settings” -> “Webhooks & Services” -> “Create Webhook”
  • Use Jenkins URL with suffix “/github-webhook”. Jenkins will replace automatically as you configure jobs, so that it’s not needed to create GitHub hook for each Jenkins job
  • After creation you can test webhook via three dots in “Recent Deliveries”. HTML error code “302 Found” means that it’s working fine (even when GitHub highlights it with exclamation mark).

github-webhook-creation

  • Finally enable GitHub triggering in Jenkins job

jenkins-job-triggers

That’s it. Github commit should cause deploy into Tomcat now.

Resources

  1. Jenkins GitHub plugin documentation
  2. Codehous Tomcat Maven plugin documentation
Related Whitepaper:

Software Architecture

This guide will introduce you to the world of Software Architecture!

This 162 page guide will cover topics within the field of software architecture including: software architecture as a solution balancing the concerns of different stakeholders, quality assurance, methods to describe and evaluate architectures, the influence of architecture on reuse, and the life cycle of a system and its architecture. This guide concludes with a comparison between the professions of software architect and software engineer.

Get it Now!  

Leave a Reply


one + = 8



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books