【问题标题】:Team City and Power ShellTeamcity 和 Powershell
【发布时间】:2012-12-10 08:13:50
【问题描述】:

我是团队城市的新手,正在尝试使用 REST API 调用部署工具。我正在尝试将 power shell 脚本传递给团队城市的 build.number。我的问题是如何从 TeamCity 运行 PS 脚本并将 $build 参数值传递给它

这是 PS 我的脚本:

param (
    [string]$build = "#build#"
)
$cred = New-Object System.Net.NetworkCredential("user", "password")
$url = 'http://server-ip:8080/datamanagement/a/api/create-release'
$request = [Net.WebRequest]::Create($url)

$request.ServicePoint.Expect100Continue = $false
$request.PreAuthenticate = $true

$request.Credentials = $cred
$request.Headers.Add("AUTHORIZATION", "Basic c3VwZXJ7482ewfc3974yOnN1c2Vy"); # user:pass encoded in base 64
$request.ContentType = "application/json"
$request.Method = "POST"

$data = (New-Object PSObject |
    Add-Member -PassThru NoteProperty environment "QA" |
    Add-Member -PassThru NoteProperty template "Regression on AutoNolio" |
    Add-Member -PassThru NoteProperty release "Nolio build: $build" |
    Add-Member -PassThru NoteProperty application "RunAutomation" |
    Add-Member -PassThru NoteProperty version "$build" |
    Add-Member -PassThru NoteProperty doStepsValidation "false" |
    Add-Member -PassThru NoteProperty releaseType "Major"
) | ConvertTo-JSON

Write-Host $data
#   Write-Host $cred.Password


$bytes = [System.Text.Encoding]::ASCII.GetBytes($data)

$request.ContentLength = $bytes.Length

$requestStream = [System.IO.Stream]$request.GetRequestStream()
$requestStream.write($bytes, 0, $bytes.Length)

$response = $request.GetResponse()

[IO.Stream] $stream = $response.GetResponseStream()
[IO.StreamReader] $reader = New-Object IO.StreamReader($stream)
[string] $output = $reader.readToEnd()
$stream.flush()
$stream.close()

# // return the text of the web page
Write-Host $output

我正在设置以下配置:

但是我在运行 buld 时遇到了这个错误:

[17:43:37]Checking for changes
[17:43:37]Publishing internal artifacts (1s)
[17:43:37]Clearing temporary directory: C:\BuildAgent2\temp\buildTmp
[17:43:37]Checkout directory: C:\BuildAgent2\work\467ac7a3aa06b293
[17:43:37]Updating sources: agent side checkout (3s)
[17:43:41]Starting: C:\Windows\sysnative\cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -build 14 -Command - <C:\BuildAgent2\temp\buildTmp\powershell3648184935303703255.ps1 && exit /b %ERRORLEVEL%
[17:43:41]in directory: C:\BuildAgent2\work\467ac7a3aa06b293
[17:43:41]-build : The term '-build' is not recognized as the name of a cmdlet, 
[17:43:41]function, script file, or operable program. Check the spelling of the name, or 
[17:43:41]if a path was included, verify that the path is correct and try again.
[17:43:41]At line:1 char:1
[17:43:41]+ -build 14 -Command -
[17:43:41]+ ~~~~~~
[17:43:41]    + CategoryInfo          : ObjectNotFound: (-build:String) [], CommandNotFo 
[17:43:41]   undException
[17:43:41]    + FullyQualifiedErrorId : CommandNotFoundException
[17:43:41] 
[17:43:41]Process exited with code 1
[17:43:41]Publishing internal artifacts
[17:43:42]Build finished

【问题讨论】:

  • 错误似乎很明显。不熟悉“团队城市”,但您正在尝试使用 PowerShell.exe 中不存在的参数 -build 运行 PowerShell。在“其他命令行参数”中删除它应该没问题。自动取款机。它等于运行PowerShell.exe -build %build.number% -command ...Your..code..
  • 是否有使用该列表框中的 -file 参数运行脚本的选项?而不是您现在使用的... -command ..。我认为 -build 参数应该可以工作。
  • 是的,它唯一的缺点是我必须在代理上设置 Set-ExecutionPolicy RemoteSigned 才能使 -File 方法工作
  • 在我看来这不是缺点。 remotesigned 仅运行在未签名的本地计算机上创建的脚本。所以从互联网/病毒下载的代码等不会运行。

标签: powershell teamcity


【解决方案1】:

Graimer 是正确的;您可以使用%build.number% 将内部版本号插入到您的脚本中。为了扩展答案,这是 TeamCity 的众多predefined build parameters 之一。如果您在代码文本框中输入一个起始百分号,TeamCity 将显示一个下拉列表,其中包含您可以插入的所有可能参数。

您必须小心一些,因为它们是作为裸词插入到您的脚本中的。例如,如果您将常用配置文件存储在 %agent.work.dir% 中,并尝试运行以下复制命令:

cp %agent.work.dir%\config .\config

该命令将扩展为类似

cp C:\teamcity install\config .\config

这是行不通的,因为 Powershell 会认为您正在尝试复制文件 C:\teamcity。因此,请确保将整个论点放在引号内:

cp "%agent.work.dir%\config" .\config

附带说明一下,使用带有自定义 Configuration Parameters 的模板非常有用,因此您可以在多个构建配置中使用相同的脚本。这就像在语言中添加函数:您可以重用并易于修改。

另外,在 7.1.1 之前的 TeamCity 版本中,a bug 与脚本执行模式设置为 -Command 的运行脚本相关,因此如果您运行的是 7.0 或更早版本,使用 -File 会更安全

【讨论】:

    【解决方案2】:
    param (
        [string]BuildNumber
        )
    

    和 TeamCity 设置中的-BuildNumber %build.number% 应该可以工作

    【讨论】:

      【解决方案3】:

      使用“从外部文件执行 .ps1”,将参数放在“脚本参数”中,然后从“附加命令行参数”中删除。

      【讨论】:

        猜你喜欢
        • 2014-03-31
        • 1970-01-01
        • 1970-01-01
        • 2017-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多