1

I have several script steps in Kotlin DSL for TeamCity. For example here I update the parameter that was changed in previous buildTypes.

script {
  scriptContent = """        
    TEMP_VAR=${'$'}(curl --insecure -v --Header "Content-Type: text/plain" --user '%apiName%:%apiPassNew%' <TC-url>/httpAuth/app/rest/projects/<projectName>/parameters/%$myVar%)
    echo "##teamcity[setParameter name='%$myVar%' value='${'$'}TEMP_VAR']" 
  """.trimIndent()
}

And then I have another script step that runs an ansible-playbook with extraVars. I want to move a JSON from extraVars to Kotlin collection, however, for this, I want to use Kotlin variables instead of TeamCity parameters. I could assign it like this

val kotlinVar = "%myParameterFromTeamCity%"

But how could I update the value of this parameter in the script block? For example, I have this part of pipeline.

    var myVar = "%myParameterFromTeamCity%"
    steps {
      script {
        scriptContent = """        
          TEMP_VAR=${'$'}(curl --insecure -v --Header "Content-Type: text/plain" --user '%apiName%:%apiPassNew%' <TC-url>/httpAuth/app/rest/projects/<projectName>/parameters/myParameterFromTeamCity)
        """.trimIndent()
    }
      }

How could I set the value of TEMP_VAR to myVar after curl runs?

1 Answers1

1

Probably it's not actual anymore but maybe it will help someone else.

Simple answer: you can't.

Long answer: Kotlin code "exists" only before being uploaded to the Teamcity server. After that Teamcity generates XML config files and uses them. Explanation from the documentation:

Note: DSL scripts is essentially another way of writing TeamCity configuration files. DSL scripts do not have direct control on how builds are executed. For instance, it is impossible to have a condition and change something depending on the current state of a build. The result of scripts execution is configuration files, which are loaded by TeamCity server and then behavior of the newly triggered builds changes.

The resulting XML config can be generated manually using maven command mvn teamcity-config:generate or on IntelliJ IDEA Maven panel and found inside .teamcity\target\generated-configs folder.

In your case, I assume, you want to use that JSON data later in another build step. If that another build step is a part of the same build type then simpliest solution will be to save the JSON file to the known location and access it later from that location.
If data from the JSON file needs to be used in another build type, artifacts dependency can help.
If you need to modify the JSON it also must be done inside a build step.

gost-serb
  • 13
  • 4