Skip to content

Instantly share code, notes, and snippets.

@dlwyatt
Last active September 4, 2015 16:06
Show Gist options
  • Save dlwyatt/fb2bc0718267de8adcb6 to your computer and use it in GitHub Desktop.
Save dlwyatt/fb2bc0718267de8adcb6 to your computer and use it in GitHub Desktop.
TeamCity Example Code
try
{
$path = '%system.teamcity.build.checkoutDir%'
Set-Location $path
$psd1 = Join-Path $path Pester.psd1
Import-Module $psd1 -ErrorAction Stop
$xml = Join-Path $path Test.Version.xml
$result = Invoke-Pester -Path $path -Tag VersionChecks -OutputFile $xml -OutputFormat NUnitXml -PassThru -Strict -ErrorAction Stop
if ($result.FailedCount -gt 0)
{
throw "$($result.FailedCount) tests did not pass."
}
}
catch
{
$userId = '%TeamCityApi.UserName%'
$pw = '%TeamCityApi.Password%' | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object pscredential($userId, $pw)
$body = "<buildCancelRequest comment='Version Numbers do not match; cancelling release.' readdIntoQueue='false' />"
$result = Invoke-RestMethod -Uri '%teamcity.serverUrl%/httpAuth/app/rest/builds/id:%teamcity.build.id%' `
-Credential $cred `
-Method POST `
-Body $body `
-Header @{ 'Content-Type' = 'application/xml' }
exit 1
}
$temp = $null
try
{
$path = '%teamcity.build.checkoutDir%'
$apiKey = '%PowerShellGetApiKey%'
Write-Host 'Importing PowerShellGet module...'
Import-Module PowerShellGet -ErrorAction Stop
$temp = Join-Path $env:temp "$([guid]::NewGuid().Guid)\Pester"
$null = New-Item -Path $temp -ItemType Directory -ErrorAction Stop
Write-Host "Copying release files to temporary folder '$temp'..."
Copy-Item $path\Pester.ps?1 $temp\ -ErrorAction Stop
Copy-Item $path\Pester.Tests.ps1 $temp\ -ErrorAction Stop
Copy-Item $path\LICENSE $temp\ -ErrorAction Stop
Copy-Item $path\nunit_schema_2.5.xsd $temp\ -ErrorAction Stop
Copy-Item $path\bin $temp\ -Recurse -ErrorAction Stop
Copy-Item $path\Functions $temp\ -Recurse -ErrorAction Stop
Copy-Item $path\Examples $temp\ -Recurse -ErrorAction Stop
Copy-Item $path\Snippets $temp\ -Recurse -ErrorAction Stop
Copy-Item $path\en-US $temp\ -Recurse -ErrorAction Stop
Write-Host 'Copy complete. Contents:'
Get-ChildItem $temp -Recurse | Out-Host
Write-Host 'Publishing module to PowerShellGet...'
$null = Publish-Module -Path $temp -NuGetApiKey $apiKey -ErrorAction Stop
}
catch
{
Write-Error -ErrorRecord $_
exit 1
}
finally
{
if ($temp)
{
Write-Host 'Cleaning up temporary folder...'
Remove-Item -LiteralPath $temp -ErrorAction Ignore -Recurse -Force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment