0

我已将我的 Teamcity 构建提取为 Kotlin 输出。我想创建一个定义许多常见步骤/设置的基类,但允许单个构建扩展这些属性。

例如

open class BuildBase(init: BuildBase.() -> Unit) : BuildType({
    steps {
        powerShell {
            name = "Write First Message"
            id = "RUNNER_FirstMessage"
            scriptMode = script {
                content = """
                    Write-Host "First Message"
                """.trimIndent()
            }
        }
    }
})

object Mybuild : BuildBase({
    steps { // This would add a new step to the list, without wiping out the original step
        powerShell {
            name = "Write Last Message"
            id = "RUNNER_LastMessage"
            scriptMode = script {
                content = """
                    Write-Host "Last Message"
                """.trimIndent()
            }
        }
    }
})

在此示例中,我想从基类继承步骤,但添加与特定构建相关的其他步骤。此外,我想继承基础disableSettings(如果有的话)并禁用其他步骤。

这甚至可能吗?如果是这样,我将如何构建类以启用它?

4

1 回答 1

0

您可能已经找到了解决方案,但这就是我将如何解决您的问题。

与 GUI 中一样,TeamCity 支持构建模板。在您的情况下,您将拥有如下模板:

object MyBuildTemplate: Template({
  id("MyBuildTemplate")
  name = "My build template"

  steps {
    powerShell {
        name = "Write First Message"
        id = "RUNNER_FirstMessage"
        scriptMode = script {
            content = """
                Write-Host "First Message"
            """.trimIndent()
        }
    }
  }
})

然后,您可以定义扩展此模板的构建配置:

object MyBuildConfig: BuildType({
  id("MyBuildConfig")
  name = "My build config"

  steps { // This would add a new step to the list, without wiping out the original step
    powerShell {
        name = "Write Last Message"
        id = "RUNNER_LastMessage"
        scriptMode = script {
            content = """
                Write-Host "Last Message"
            """.trimIndent()
        }
    }

    // afaik TeamCity would append the build config's steps to the template's steps but there is way to explicitly define the order of the steps:
    stepsOrder = arrayListOf("RUNNER_FirstMessage", "RUNNER_LastMessage")
  }
})

这样,您还应该能够disableSettings从模板继承。

于 2020-08-06T10:26:06.770 回答