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

為{幸葍}努か 提交于 2019-12-03 08:28:33
Honza Zidek

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):

  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.

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

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

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:

  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:

  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

You can use one or booth of the plugins below.

1 - Dynamic Extended Choice Paramenter This seems to make exactly what you want but I never use this one.

2 - Dynamic Parameter In this you can configure a script to read all app's, branch or trunks and transform this on choices options for your job or just add a list of static options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!