4

私はチーム シティを初めて使用し、REST API を使用して展開ツールを呼び出そうとしています。チームシティからビルド番号をパワーシェルスクリプトに渡そうとしています。私の質問は、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

次の構成を設定しています。

ここに画像の説明を入力

しかし、ブルドを実行すると次のエラーが発生します。

[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
4

3 に答える 3

3

グレイマーは正しいです。%build.number%ビルド番号をスクリプトに挿入するために使用できます。答えを拡張すると、これは TeamCity の事前定義されたビルド パラメーターの多くの 1 つです。コードテキストボックスに開始パーセント記号を入力すると、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

于 2012-12-23T17:30:09.183 に答える
0
param (
    [string]BuildNumber
    )

そして -BuildNumber %build.number%TeamCity設定で動作するはずです

于 2014-06-06T05:10:12.597 に答える