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

标签 configuration teamcity kotlin dsl

我正在尝试使用 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/

相关文章:

continuous-integration - 代理运行我们不知道的未知构建

android - Firestore 查询 StartAt(DocumentReference) 没有给出正确的结果

constructor - 如何在 Kotlin 的构建过程中修改 val 成员

javascript - 运行 gulpserve 或 build 时加载器的 Webpack 配置问题

Android应用环境配置

java - 如何配置apache Camel查看关闭原因?

configuration - 如何动态更改 xmobar 配置

TeamCity - 在 ASP.net 站点上发布的问题

c# - 如何调试从 TeamCity 部署的 nuget 包?

android - 将 LiveData<Resource<User>> 转换为 LiveData<User> 时的类型干扰问题