4
votes

I'm new to team city and trying to invoke deployment tool using REST API. I'm trying to pass power shell script the build.number from team city. My question is how can I run PS script from TeamCity and pass it the $build parameter value

This is PS my script:

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

I'm setting the followoing configuration:

enter image description here

But I'm getting this errors when running the 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
3
the error seems pretty clear. not familiar With "team city", but you're trying to run PowerShell With a paramter -build that doesn't exist in PowerShell.exe . Remove this in "additional command line parameters" and it should be fine. Atm. it equals running PowerShell.exe -build %build.number% -command ...Your..code..Frode F.
is there an option to run the script using the -file paramter in that listbox? instead of that ... -command .. that you use now. I think the -build paramter should work then.Frode F.
Yes there is and it works the only disadvantage that I have to set the Set-ExecutionPolicy RemoteSigned ont the agent to have -File method to workDmitry R
that's no disadvantage in my eyes. remotesigned only runs scripts made on the local computer unsigned. so code downloaded from internet/viruses and such will not run.Frode F.

3 Answers

3
votes

Graimer is correct; you can use %build.number% to insert the build number into your script. To expand on the answer, this is one of many of TeamCity's predefined build parameters. If you type an opening percent sign in the code textbox, TeamCity will show a dropdown containing all the possible parameters you can insert.

You have to be careful about some, because they're inserted as barewords into your script. If, for example, you store common configuration files in %agent.work.dir%, and you try to run the following copy command:

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

The command will be expanded to something like

cp C:\teamcity install\config .\config

And that won't work, because Powershell will think you're trying to copy the file C:\teamcity. So make sure you put that entire argument inside quotes:

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

As a side note, using templates with custom Configuration Parameters is incredibly useful, so you can use the same scripts in multiple build configurations. It's like adding functions to a language: you get reuse and ease of modification.

Also, in versions of TeamCity before 7.1.1, there's a bug relating to running scripts with the Script Execution Mode set to -Command, so if you're running 7.0 or earlier, it's safer to use -File

0
votes
param (
    [string]BuildNumber
    )

and -BuildNumber %build.number% in TeamCity settings should work

0
votes

Using "Execute .ps1 from external file", put arguments inside "Script arguments" and remove it from "Additional command line parameters".