'Accessing teamcity parameters in kotlin DSL
I am creating a kotlin DSL for a TeamCity project, and want to get the current branch name in the actual kotlin script.
If I run a script step, the current branch renders correctly
script {
name="print branch"
scriptContent = """echo "Branch is %teamcity.build.branch%""""
}
From the actual settings.kts, I don't seem to have access to it
val currentBranch = DslContext.getParameter("teamcity.build.branch") //yields '<placeholder-1>'
How can I get this parameter in my Kotlin code?
Solution 1:[1]
So it turns out it isn't possible - the DSL is merely used to express a config, and isn't invoked during the build process. In my case, I wanted to enable or disable some steps according to whether I was on main or a feature branch, so I created 2 BuildDefinition objects,, which subclassed the main build, passing in a bool of whether the branch was main. With that, I was able to enable or disable build steps and update the VCS triggers for each build
Solution 2:[2]
What seems to work for me is
object : BuildType({
val changeList = if ("%teamcity.build.branch%" == "master") "" else "-%teamcity.build.branch%"
...
steps {
maven {
jvmArgs = "-Xmx1024m -Xss1g -Drevision=1.1.%build.counter% -Dchangelist=$changeList"
}
}
})
YMMV
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jon Bates |
| Solution 2 | TimA |
