配置Jenkins作业以从SVN选择应用程序和主干/标签/分支 [英] Configure a Jenkins job to select an application and trunk/tags/branches from SVN
问题描述
我们要使用一个Jenkins作业来构建应用程序.
We want to use a single Jenkins job to build an application.
来自如何配置单个Jenkins作业以从主干或分支进行释放的解决方案?是 <不起作用,因为我们的SVN结构不同(出于历史原因,我们无法更改):
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
所描述的解决方案如何配置单个Jenkins作业以使从主干或分支的释放过程成为可能?显示供选择这个:
The described solution How to configure a single Jenkins job to make the release process from trunk or branches? shows for selection this:
- 树干
- 分支机构/app1
- 分支机构/app2
- 分支机构/app3
- 标签/app1
- 标签/app2
- 标签/app3
我们想要的是以下内容:
What we would like to have is the following:
选择1:
- app1
- app2
- app3
选择2(自动基于选择1,例如针对app2):
Selection 2 (automatically based on the selection 1, e.g. for app2):
- 树干
- BRANCH_D
- BRANCH_E
- BRANCH_F
- TAG_D
- TAG_E
- TAG_F
推荐答案
Use Active Choice Parameter and Groovy script.
- 创建参数
APP
以选择应用程序. Groovy脚本应返回选择列表,因此只需返回硬编码列表(或从文件或所需的任何位置读取它):
- 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):
-
创建参数
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
- 参考参数必须设置为
APP
.每当APP
的值更新时,Groovy脚本都会使用参考参数的更新值来重新评估选择列表.
- Referenced parameters must be set to
APP
. Whenever the value ofAPP
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
- 在 Build Triggers 部分中,设置另一个变量(以提高可读性):
BASE_SVN_URL=http://my-svn-repo/projects
- In the Build Triggers section, set another variable (for better readability):
BASE_SVN_URL=http://my-svn-repo/projects
- 现在您已经准备好所有变量,可以将它们打印到Jenkins作业控制台:
用于复制&的脚本粘贴:
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
- 最后,调用另一个Jenkins作业
Build-application-core-job
,它将执行实际的构建.将作业传递所有必需的参数:
- Finally, call another Jenkins job
Build-application-core-job
, which will perform the actual build. Pass the job all the necessary parameters:
- 运行作业时,您将获得一个带有两个参数的屏幕,其中第二个参数将始终包含有效值,具体取决于第一个参数的值:
- 最后但并非最不重要的一点是:遵循以下标准是好的:)如果您遵循标准,则可以像
List Subversions标记(及更多)插件. ="https://stackoverflow.com/a/32622725/2886891"> https://stackoverflow.com/a/32622725/2886891
- 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
这篇关于配置Jenkins作业以从SVN选择应用程序和主干/标签/分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!