Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure a Jenkins job to select an application and trunk/tags/branches from SVN

We want to use a single Jenkins job to build an application.

The solution from How to configure a single Jenkins job to make the release process from trunk or branches? does not work since our SVN structure is different (from historical reasons and we cannot change it):

http://my-svn-repo/projects/
├───branches
│   ├───app1
│   │   ├───BRANCH_A
│   │   ├───BRANCH_B
│   │   └───BRANCH_C
│   ├───app2
│   │   ├───BRANCH_D
│   │   ├───BRANCH_E
│   │   └───BRANCH_F
│   └───app3
│       ├───BRANCH_G
│       ├───BRANCH_H
│       └───BRANCH_I
├───tags
│   ├───app1
│   │   ├───BRANCH_D
│   │   ├───BRANCH_E
│   │   └───BRANCH_F
│   ├───app2
│   │   ├───TAG_D
│   │   ├───TAG_E
│   │   └───TAG_F
│   └───app3
│       ├───TAG_G
│       ├───TAG_H
│       └───TAG_I
└───trunk
    ├───app1
    ├───app2
    └───app3

The described solution How to configure a single Jenkins job to make the release process from trunk or branches? shows for selection this:

  • trunk
  • branches/app1
  • branches/app2
  • branches/app3
  • tags/app1
  • tags/app2
  • tags/app3

What we would like to have is the following:

Selection 1:

  • app1
  • app2
  • app3

Selection 2 (automatically based on the selection 1, e.g. for app2):

  • trunk
  • BRANCH_D
  • BRANCH_E
  • BRANCH_F
  • TAG_D
  • TAG_E
  • TAG_F
like image 222
Martin Landa Avatar asked Jan 09 '17 13:01

Martin Landa


People also ask

How pull code from SVN to Jenkins?

Now create a Jenkins build job. Select New Item, give the build project a name such as svn-tomcat-demo, select Maven project, and click OK. Under source code management, select Subversion and enter your SVN repository URL and credential. Please download the sample code and check the code into your SVN server.

Can we use Jenkins with SVN?

The Jenkins Subversion plugin uses SvnKit as the subversion client and doesn't require a native client to be available on the host. Setting up a CI server with Jenkins and Subversion is limited to a very minimal installation process: just run the Jenkins WAR and you're done.


1 Answers

Use Active Choice Parameter and Groovy script.

  1. Create parameter APP to select the application. The Groovy script is expected to return the list of selections, so just return the hard-coded list (or read it from a file or from anywhere you wish):

enter image description here

  1. Create parameter VERSION to select the version.

    • Now use Active Choice Reactive Parameter, so you are able to react to the changes of the parameter APP.
    • The Groovy Script gradually builds the list of choices to be selectable. First trunk, then tags, then branches.
    • The data are obtained using the command-line svn list in the form of

    svn list http://my-svn-repo/projects/tags/appX --username some_name --password some_password

    • Referenced parameters must be set to APP. Whenever the value of APP is updated, the Groovy script will re-evaluate the choice list using the updated values of referenced parameters.

enter image description here

Here is the script for copy & paste:

def svnBaseUrl = 'http://my-svn-repo/projects/'
def versions = ['trunk/' + APP]

def svnTagsUrl = svnBaseUrl + 'tags/' + APP
def command = ['svn', 'list', svnTagsUrl,'--username', 'some_name', '--password', 'some_password']
def proc = command.execute()
proc.waitForOrKill(10000)
if ( proc.exitValue() == 0 ) {
  proc.text.eachLine { versions.add('tags/' + APP + '/' + it) }
}

def svnBranchesUrl = svnBaseUrl + 'branches/' + APP
command = ['svn', 'list', svnTagsUrl,'--username', 'some_name', '--password', 'some_password']
proc = command.execute()
proc.waitForOrKill(10000)
if ( proc.exitValue() == 0 ) {
  proc.text.eachLine { versions.add('branches/' + APP + '/' + it) }
}

return versions
  1. In the Build Triggers section, set another variable (for better readability): BASE_SVN_URL=http://my-svn-repo/projects

enter image description here

  1. Now you have all the variables ready and you may print them to the Jenkins job console:

enter image description here

The script for copy & paste:

#!/bin/bash
echo "================================================"
echo "Parameters for the build:"
echo "Application:  $APP"
echo "Base SVN URL: ${BASE_SVN_URL}"
echo "Version:      ${VERSION}"
echo "SVN URL:      ${BASE_SVN_URL}/${VERSION}"
echo "================================================"

During testing, you may add a line at the end of the script to immediately terminate your job and just see the values:

exit 1
  1. Finally, call another Jenkins job Build-application-core-job, which will perform the actual build. Pass the job all the necessary parameters:

enter image description here

  1. When you run your job, you will get a screen with two parameters, where the second will one always contain the valid values depending on the value of the first one:

enter image description here

  1. Last but not least: following standards is good :) Had you followed standards, you could have used the List Subversions tags (and more) plugin almost out of the box as described at https://stackoverflow.com/a/32622725/2886891
like image 164
Honza Zidek Avatar answered Oct 05 '22 01:10

Honza Zidek