How to create a jenkins job that builds a liferay plugin and deploys it to a remote liferay portal
Have a look also at this post.
First of all we need:
- a running jerkins server
- the liferay SDK
- the liferay portal
- the plugin code committed in SVN
- the remote portal (optionally)
For the example we theoretically have
- a SVN server with URL https://svn.server.com/svn/project/
- a plugin of type portlet with the name MyPortletPlugin
- the plugin is committed in the SVN as a portlet project
Description
For this example we will create a new user named liferay. Under this username we will install the liferay SDK and portal. The portal installation will be used only for the build phase by the SDK. We won’t start this portal. The deployment takes place in a remote portal installation.
Install liferay SDK
Unzip the liferay SDK in the liferay home directory
Install liferay Portal
Unzip the liferay Portal in the liferay home directory
Connect SDK with Portal
In the SDK directory create a build.liferay.properties, where liferay is the new user name. In this file (that could be a duplicate of build.properties) we need to se the directory of the portal in the property app.server.dir.
Install an ant build (if not yet installed)
- Go to Jenkins > Manage Jeknins > Configure System > Ant
- press Ant installations...
- give a name, for example ant1.9.0
- select “Install automatically”
- select a ant version
- press “Save” for the configuration
- the ant will be installed with the first build that will use this ant installation
Install the Publish over SSH plugin
Go to Jenkins > Manage Jeknins > Manager Plugins > Available
- select Publish Over SSH
- press “Install without restart
- wait for the plugin to be installed
Configure an SSH Server
Go to Jenkins > Manage Jeknins > Configure System > Publish Over SSH
- Type a “Passphrase”
- Press Add for the new server
- give the name, hostname, username
- as a remote directory give the portal directory of the remote server
Edit the build.xml of your plugin
We will make a small change in the build.xml of your plugin. This is needed only if you want to deploy in a remote server. If not, then skip this step. The old code is market with italics, the new one with bold:
<project name="CosmoOne-portlet" basedir="." default="deploy"> <import file="../build-common-portlet.xml" /> <target name="deploy-jenkins" > <delete dir="tempDist" /> <antfetch target="clean" inheritall="false" return="sdk.dir"/> <antfetch target="war" inheritall="true" return="plugin.file"/> <mkdir dir="tempDist"/> <copy file="${plugin.file}" todir="tempDist"/> <script language="javascript"> <![CDATA[ revision = project.getProperty("SVN_REVISION"); buildnumber = project.getProperty("plugin.file"); warName = buildnumber.substring(buildnumber.lastIndexOf("/") + 1); project.setProperty("warName",warName); tempName = warName.substring(0, warName.lastIndexOf(".") - 1); newName = tempName.substring(0, tempName.lastIndexOf(".")) + '.' + revision + '.war' ; project.setProperty("newName",newName); ]]> </script> <echo message="WAR FILE NAME WITH REVISION IS ${newName}"></echo> <move file="tempDist/${warName}" tofile="tempDist/${newName}"/> <copy file="tempDist/${newName}" todir="${sdk.dir}/dist/"/> </target> </project>
The build will firstly update the version of the plugin based on the revision number of the SVN. This takes place in the liferay-plugin-package.properties file in the module-incremental-version property. This value will be used in the war name as the last number in the war name.
Create a jenkins node
Go to Jenkins > Manage Jeknins > Manage Nodes
- select “New Node”
- give the node name
- select “Dumb Slave”
- press “OK”
- in “” give for example 2
- in “Labels” give the remote directory, for example the liferay SDK directory
- in “Launch method” select “Launch slave agents on Unix machines via SSH”
- and select the credentials for the user liferay as defined
Create a jenkins job
The time has come at last, to create the plugin job.
- give a name to your job
- check the option “Restrict where this project can be run” and give as the label expression the node name.
- open the “Advanced Project Options” and check “Use custom workspace” and fill in in the Directory text field the path to liferay home directory concatenated with the liferay SDK directory name plus the “portlets” in case your plugin is a portlet. Example: /home/liferay/liferay-plugins-sdk-6.1.20/portlets
- Now, we need to configure the SVN with jenkins.
- In the area “Source Code Management” check “Subversion”.
- In “Repository URL” fill in something like: https://svn.server.com/svn/project/MyPortletPlugin-portlet
- In “Local module directory (optional)” fill in: MyPortletPlugin-portlet
- [optional] If you need to create a build automatically every time you make a commit to the SVN repository, then you can check “Poll SCM” in the “Build Triggers” area. As a value you can set “@hourly”. Now every hour jenkins will check for changes in the plugin module in SVN. If something is found, then it will trigger a new build for this job. If not, then nothing will be done.
- Lets define now the main task, the “Build”.
- create a new “build step” from type “Invoke Ant”
- select the ant version
- in “Targets” type the new target name “deploy-jeknins”
- select “Advanced” and in the properties type “SVN_REVISION=${SVN_REVISION}”
- The last step is to define the “Post-build Actions”. Here you can set notifications, but we will describe only the copy of the last created war in the deploy directory of the remote server.
- Select the SSH server
- In “Source files” type “MyPortletPlugin-portlet/tempDist/MyPortletPlugin-portlet-*.war”
- In “Remove prefix” type “MyPortletPlugin-portlet/tempDist”
- In “Remote directory” type “deploy”