4

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?

Jon Bates
  • 3,055
  • 2
  • 30
  • 48

3 Answers3

2

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

Jon Bates
  • 3,055
  • 2
  • 30
  • 48
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

TimA
  • 21
  • 3
0

Assuming you have a VCS root for each branch, DslContext.settingsRoot is the VCS root the Kotlin is executing against.

From our Kotlin which is used to build against several different branches:

val branchName = "${DslContext.settingsRoot}" // DslContext.settingsRoot.name - .name seems to be blank