如何隐藏Kotlin TeamCity的自定义构建步骤中的参数?

我试图设置TeamCity,使用配置作为与Kotlin的代码。 我正在编写构建包装,所以我可以隐藏默认的公开配置,只公开重要的参数。 这将允许我阻止类的用户更改会导致构建错误的值。

我要这个:

steps { step { name = "Restore NuGet Packages" type = "jb.nuget.installer" param("nuget.path", "%teamcity.tool.NuGet.CommandLine.3.3.0%") param("nuget.updatePackages.mode", "sln") param("nuget.use.restore", "restore") param("sln.path", "path_to_solution") //parameter here param("toolPathSelector", "%teamcity.tool.NuGet.CommandLine.3.3.0%") } 

…是这样的:

 MyBuildSteps.buildstep1("path_to_solution") 

以下是步骤的功能签名:

 public final class BuildSteps { public final fun step(base: BuildStep?, init: BuildStep.() -> Unit ): Unit { /* compiled code */ } } 

这是我试过的:

 class MyBuildSteps { fun restoreNugetPackages(slnPath: String): kotlin.Unit { var step: BuildStep = BuildStep { name = "Restore NuGet Packages" type = "jb.nuget.installer" } var stepParams: List = Parametrized { param("build-file-path", slnPath) param("msbuild_version", "14.0") param("octopus_octopack_package_version", "1.0.0.%build.number%") param("octopus_run_octopack", "true") param("run-platform", "x86") param("toolsVersion", "14.0") param("vs.version", "vs2015") } return { step.name step.type stepParams } //how do I return this? } } 

任何意见将不胜感激!

我假设你想用step {...}将参数slnPath封装到函数slnPath

使用这个函数签名,然后复制粘贴step {...} 。 添加你认为合适的参数:

 fun BuildSteps.buildstep1(slnPath: String) { step { name = "Restore NuGet Packages" type = "jb.nuget.installer" param("nuget.path", "%teamcity.tool.NuGet.CommandLine.3.3.0%") param("nuget.updatePackages.mode", "sln") param("nuget.use.restore", "restore") param("sln.path", slnPath) // your parameter here param("toolPathSelector", "%teamcity.tool.NuGet.CommandLine.3.3.0%") } } 

就这样! 使用它来代替step {...}构造:

 steps { buildstep1("path_to_solution") } 

这个函数可以在配置文件中的任何地方声明(我通常把它们放在底部),或者在一个单独的.kts文件中,然后导入(理论上)。

Interesting Posts