首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Team City和Power Shell

Team City和Power Shell
EN

Stack Overflow用户
提问于 2012-12-23 23:46:28
回答 3查看 7.5K关注 0票数 4

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

这是PS我的脚本:

代码语言:javascript
复制
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的时候遇到了这个错误:

代码语言:javascript
复制
[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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-24 01:30:09

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

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

代码语言:javascript
复制
cp %agent.work.dir%\config .\config

该命令将扩展为类似以下内容

代码语言:javascript
复制
cp C:\teamcity install\config .\config

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

代码语言:javascript
复制
cp "%agent.work.dir%\config" .\config

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

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

票数 3
EN

Stack Overflow用户

发布于 2014-06-06 13:10:13

代码语言:javascript
复制
param (
    [string]BuildNumber
    )

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

票数 0
EN

Stack Overflow用户

发布于 2017-03-29 02:54:56

使用"Execute .ps1 from external file",将参数放在“脚本参数”中,并从“附加命令行参数”中删除。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14012318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档