简体   繁体   English

TeamCity 在 powershell 步骤上的 powershell 脚本中未解决的参考

[英]TeamCity Unresolved reference from powershell script on a powershell step

I have the next step on one of my pipelines on Teamcity我在 Teamcity 上的一个管道上有下一步

powerShell {
                    name = "Start e2e-container-group build at Azure"
                    executionMode = BuildStep.ExecutionMode.ALWAYS
                    scriptMode = script {
                        content = """
                            try {
                               $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                               $headers.Add("Authorization", "Basic token")
                               $headers.Add("Content-Type", "application/json")

                               $body = "{
                               `n  `"definitionId`": 7,
                               `n  `"description`": `"Release for E2E-UAT testing`"
                               `n}"

                               Invoke-RestMethod 'https://vsrm.dev.azure.com/company/projectname/_apis/release/releases?api-version=5.1' -Method 'POST' -Headers $headers -Body $body
                            } catch [Exception] {
                                Write-Output ${'$'}_.Exception.Message
                                exit 1
                            }
                        """.trimIndent()
                    }
                }

The output I get when running the pipeline is:我在运行管道时得到的 output 是:

 Kotlin DSL compilation errors
Compilation error Extra\buildTypes\E2ETestsBuild.kt[89:33]: Unresolved reference: headers
Compilation error Extra\buildTypes\E2ETestsBuild.kt[90:33]: Unresolved reference: headers
Compilation error Extra\buildTypes\E2ETestsBuild.kt[91:33]: Unresolved reference: headers
Compilation error Extra\buildTypes\E2ETestsBuild.kt[93:33]: Unresolved reference: body
Compilation error Extra\buildTypes\E2ETestsBuild.kt[98:172]: Unresolved reference: headers
Compilation error Extra\buildTypes\E2ETestsBuild.kt[98:187]: Unresolved reference: body
StackTrace
Load project model
Read build settings from revision c8c0bd16eb07f4165ca37f310d0ee25a69a3d448

The script is working perfectly in powershell, is there any extra steps I have to do to run this on the pipeline?该脚本在 powershell 中运行良好,是否需要执行任何额外步骤才能在管道上运行它?

I'm aware this question is almost a year old but I'm posting a solution for anyone who ends up with the same situation, as I did.我知道这个问题已经将近一年了,但我正在为任何最终遇到相同情况的人发布一个解决方案,就像我一样。

One way to solve this is to do the following: When running a PowerShell script from TeamCity there are some limitation with characters, therefore, in order to use those characters you need to add a ${''} between the character.解决此问题的一种方法是执行以下操作:从 TeamCity 运行 PowerShell 脚本时,字符存在一些限制,因此,要使用这些字符,您需要在字符之间添加 ${''}。 With these changes your script could look something like this:通过这些更改,您的脚本可能看起来像这样:

powerShell {
                name = "Start e2e-container-group build at Azure"
                executionMode = BuildStep.ExecutionMode.ALWAYS
                scriptMode = script {
                    content = """
                        try {
                           ${'$'}headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                           ${'$'}headers.Add("Authorization", "Basic token")
                           ${'$'}headers.Add("Content-Type", "application/json")

                           ${'$'}body = "{
                           `n  `"definitionId`": 7,
                           `n  `"description`": `"Release for E2E-UAT testing`"
                           `n}"

                           Invoke-RestMethod 'https://vsrm.dev.azure.com/company/projectname/_apis/release/releases?api-version=5.1' -Method 'POST' -Headers $headers -Body $body
                        } catch [Exception] {
                            Write-Output ${'$'}_.Exception.Message
                            exit 1
                        }
                    """.trimIndent()
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM