团队城市和Power Shell

4

我是一名新手,正在尝试使用REST API调用部署工具来进行团队城市的操作。我正在尝试从团队城市传递build.number给PowerShell脚本。我的问题是如何从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

我正在设置以下配置:

enter image description here

但是运行构建时出现以下错误:

[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

1
错误看起来很明显。虽然不熟悉“Team City”,但您正在尝试使用 PowerShell 运行一个在 PowerShell.exe 中不存在的参数 -build。请在“附加命令行参数”中删除它,应该就可以了。目前相当于运行 PowerShell.exe -build %build.number% -command ...Your..code.. - Frode F.
在那个列表框中,有没有使用“-file”参数运行脚本的选项?而不是现在使用的“... -command ..”。我认为使用“-build”参数应该可以解决这个问题。 - Frode F.
是的,有这个功能,并且它可以工作。唯一的缺点是我必须在代理上设置Set-ExecutionPolicy RemoteSigned才能使-File方法起作用。 - Dmitry R
在我看来,这并不是什么劣势。remotesigned 只能运行在本地计算机上未签名的脚本,因此从互联网/病毒等下载的代码将无法运行。 - Frode F.
3个回答

3
Graimer是正确的;你可以使用%build.number%将构建号插入到你的脚本中。为了扩展答案,这是TeamCity的预定义构建参数之一。如果在代码文本框中输入一个开头的百分号,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

作为一则旁注,使用自定义 配置参数 的模板非常有用,这样您可以在多个构建配置中使用相同的脚本。就像向语言中添加函数一样:您可以获得重用和修改的便利性。
此外,在 TeamCity 版本 7.1.1 之前的版本中,存在一个与运行脚本相关的 错误,该错误将脚本执行模式设置为 -Command,因此,如果您正在运行 7.0 或更早版本,则使用 -File 更安全。

1

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


0
param (
    [string]BuildNumber
    )

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接