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”
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”
- 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”
- 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).
- Finally enable GitHub triggering in Jenkins job
That’s it. Github commit should cause deploy into Tomcat now.