Upload Builds to SVN using Jenkins

In my early posts I have described how to use Jenkins to build a sample project.
This time lets inspect how to build an app server project with Maven and push the builds to the Subversion(SVN).

Configure Maven in Jenkins

So we will need Maven to build the project. Goto Manage Jenkins->Global Tool Configuration->Maven give a name and select the maven version to install from Apache.

Create Jenkins Project

lets assume that the project is in Git hub. eg: https://github.com/test/cloud.git.

Create the project with Source code management as Git. and configure repo name and add credentials as necessary.

 

Add Build Steps

Step 1

Add a build step into the Jenkins project.

Click Add Build Step -> select Invoke Top-level Maven Targets

Select the maven you added in the global configurations. For the Goal, you can provide different options according to the task.

Maven is based around the central concept of a build lifecycle.
There are three built-in build lifecycles:

  1. default
  2. clean
  3. site
For example the default life cycle comprises of the following Build Phases: ◾validate - validate the project is correct and all necessary information is available
◾compile - compile the source code of the project
◾test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
◾package - take the compiled code and package it in its distributable format, such as a JAR.
◾integration-test - process and deploy the package if necessary into an environment where integration tests can be run
◾verify - run any checks to verify the package is valid and meets quality criteria
◾install - install the package into the local repository, for use as a dependency in other projects locally
◾deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

In here I”m going to use Clean Package in order to create a .zip attributable to be uploaded into the SVN repo.

lets assume in the Git repository there are many applications and you need to build only one specific application. We can always specify the POM file in the Maven build options.
Click Advanced and enter POM file path relative to workspace (eg: workspaceName/applications/mytestapp1/pom.xml )

When we run this Jenkins build, It’ll create a folder inside the workspace of the project and push the build zip into it.

=>  workspaceName/applications/mytestapp1/target/specificBuild.zip

 

Step 2

In the workspace of the project we have the .zip distributable of the build, but yet we cant push it to the svn.
We need to create a svn local repository or move the distributable to an existing maven repository.

We can setup a script to copy the distributable from workspace to the local repo and then push into SVN remote repo

To do this we need to setup SVN credentials as environment variables to be accessed in the script.
Go to Manage Jenkins -> Configure System -> Environment Variables -> Specify the SVN user name and Password as 2 seperate variables.

 

Now add another build step as an Execute Shell  after the Maven build step and provide script to copy and push the .zip build to the SVN  repository that you provide.

Lets assume my .zip is in,

 /home/ubuntu/.jenkins/workspace/workspaceName/applications/mytestapp1//target/distributable.zip

and the local svn repo is in

/home/ubuntu/test/jaggeryapps

Then the script will be something like

#!/bin/bash#!/bin/bash
set -e
uname=${SVN_USERNAME}
upwd=${SVN_PASSWORD}

file="/home/ubuntu/test/jaggeryapps/cloudmgt.zip"

if [ -f "$file" ]
then 
    cp /home/ubuntu/.jenkins/workspace/workspaceName/applications/mytestapp1//target/distributable.zip /home/ubuntu/test/jaggeryapps 
    cd /home/ubuntu/test/jaggeryapps    
else 
    cp /home/ubuntu/.jenkins/workspace/Cloud_management/cloud-applications/cloud-management/target/cloudmgt.zip /home/ubuntu/test/jaggeryapps 
    cd /home/ubuntu/test/jaggeryapps 
    svn add cloudmgt.zip
fi

svn commit -m 'Adding new cloudmgt.zip' --username $uname --password $upwd --non-interactive

The uname and password variables take the pre set Environment variables to give svn access to the script.
It’ll copy the file and add to svn if its not already in the version management. Then commit the .zip attributable to the SVN repo.

There you go… Done!!! check the SVN in the browser for the commit you just did.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s