我正在尝试使用 Kotlin 的配置即代码来设置 TeamCity。我正在为构建步骤编写包装器,以便我可以隐藏默认的公开配置并仅公开重要的参数。这将使我能够防止该类的用户更改会导致构建错误的值。
我想要这个:
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
的函数 buildstep1
中。
使用此函数签名并将 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
文件中声明并导入(理论上)。
关于configuration - 如何在 Kotlin for TeamCity 中隐藏自定义构建步骤中的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40050329/