Import/Export TeamCity build configuration from one server to another - build

What's the best way to move a single TeamCity build configuration from one server to another?
I have a local instance of TeamCity that I test builds on. Then when the build is sufficiently mature, I manually create it (eyeball-copy) on our main TeamCity server.
Is there an Export & Import feature that will do this for me?

Unfortunately there is no such thing. TeamCity 8 made the situation a little bit better though by introducing a Build Id format (project name + build config name, can be overwritten) that makes it feasible to "hand copy" build configurations:
Basically under the hood all your TeamCity build configurations are really just XML files in the BuildServer\config\projects\ folder and sub folders. While I haven't tried this you should be able to just copy your project folder or build config XML to the appropriate destination on your new TeamCity instance if the ids don't collide. At the very least you can definitely overwrite existing projects with updates this way (something I have done in the past to dynamically change build configs "on the fly").
Of course if your build config depends on other builds / artifacts those ids have to match as well, so either you have to copy those as well or adjust the ids accordingly. Same goes for agent requirements.
Edit:
With TeamCity 9 out now there's a much better option to move projects between TeamCity servers built in:
Now TeamCity provides the ability to move projects among servers: you
can transfer projects with all their data (settings, builds and
changes history, etc.) and with your TeamCity user accounts from one
server to another. All you need to do is create a usual backup file on
the source TeamCity server containing the projects to be imported, put
the backup file into the /import directory on
the target server and follow the import steps on the Administration |
Projects Import page.
For a full summary see what's new in TeamCity 9.

For TeamCity 9 and above:
Make sure both instances of TeamCity are running the same version.
Export data from TeamCity: Using the web UI on the source machine, go to Administration -> Backup and do a Basic backup. It will tell you the path to the backup file created.
Import data to TeamCity:
On the target server, open the web UI and navigate to Administration -> Projects Import. This will tell you the path to the import directory.
Copy the backup file to the import directory, refresh the web UI, and click 'Configure Import Scope'
Select the projects and categories of data you want to import. Given that the question was just about build configurations, you would uncheck importing users and groups. Click 'Start import'.

TeamCity 9 has this ability builtin - https://confluence.jetbrains.com/display/TCD9/Projects+Import

I found that the projects import function wasn't granular enough to restore just one build configuration, but managed to do this through the API. Using PowerShell you can call an invoke-webrequest against the source:
$serviceAccountCredentials = New-Object System.Management.Automation.PSCredential -ArgumentList #('<domain>\<user>',(ConvertTo-SecureString -String 'Password' -AsPlainText -Force))
$settings = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/settings' -Credential $serviceAccountCredentials
$parameters = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/parameters' -Credential $serviceAccountCredentials
$steps = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/steps' -Credential $serviceAccountCredentials
$features = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/features' -Credential $serviceAccountCredentials
$triggers = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/triggers' -Credential $serviceAccountCredentials
$agentReqs = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/agent-requirements' -Credential $serviceAccountCredentials
$artifactDep = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/artifact-dependencies' -Credential $serviceAccountCredentials
$snapshotDep = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/snapshot-dependencies' -Credential $serviceAccountCredentials
$vcsRoot = Invoke-RestMethod -Method Get -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/vcs-root-entries' -Credential $serviceAccountCredentials
You can then pass the XML through to the destination:
#import settings
Invoke-RestMethod -Method put -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/settings' -body $settings.OuterXml -ContentType application/xml -Credential $serviceAccountCredentials
#import parameters
Invoke-RestMethod -Method put -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/parameters' -body $parameters.OuterXml -ContentType application/xml -Credential $serviceAccountCredentials
#import steps
Invoke-RestMethod -Method put -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/steps' -body $steps.OuterXml -ContentType application/xml -Credential $serviceAccountCredentials
#import features
Invoke-RestMethod -Method put -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/features' -body $features.OuterXml -ContentType application/xml -Credential $serviceAccountCredentials
#import triggers
Invoke-RestMethod -Method put -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/triggers' -body $triggers.OuterXml -ContentType application/xml -Credential $serviceAccountCredentials
#Import VCS root setting
Invoke-RestMethod -Method put -Uri 'http://<TeamCity_Build_server>/httpAuth/app/rest/buildTypes/id:<buildID>/vcs-root-entries' -body $VCSRoots.OuterXml -ContentType application/xml -Credential $serviceAccountCredentials
The TeamCity api documentation about build configurations is available here: https://confluence.jetbrains.com/display/TW/REST+API#RESTAPI-BuildConfigurationAndTemplateSettings

Related

Powershell can't remove file i just wrote

I wanted to install git on an AWS EC2 Windows Server. So i used an Invoke-WebRequest to download the portable git exe
Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.23.0.windows.1/PortableGit-2.23.0-64-bit.7z.exe -UseBasicParsing -OutFile git.exe
But the Download got stuck and i terminated the session. Now i want to remove git.exe but for some reason i'm not allowed to.
I tried removing the file with:
Remove-Item .\git.exe
But i got an error message telling me i'm not allowd to
Remove-Item : Cannot remove item C:\git.exe: Access to the path 'C:\git.exe' is denied.
At line:1 char:1
+ Remove-Item .\git.exe
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\git.exe:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
I just found out that the Invoke-WebRequest was not properly terminated and the stuck process still kept the file on lock. after restarting the server i was able to delete the file. Thanks for the suggestions and comments.

How to make windows EC2 user data script run again on startup?

A user data script will run the first time an EC2 is started.
How can I restore/reactivate this ability on a windows EC2?
Note
I have tried the script suggested here but it fails immediately as there is no file C:\Program Files\Amazon\Ec2ConfigService\Settings\Config.xml and nothing similarly named (not that I found; not even an Ec2ConfigService directory)
Also note, my question is identical to this question but for windows ec2, not linux
I understand that the point is about just running user-data, and not all the other stuff ...
To run (only) user-data script, you can run it by:
Import-Module (Join-Path (Join-Path $env:ProgramData -ChildPath "Amazon\EC2-Windows\Launch") -ChildPath "Module\Ec2Launch.psd1")
Invoke-Userdata -OnlyExecute
let's say you save this as 'C:\ProgramData\Amazon\EC2-Windows\Launch\Config\run-user-data.ps1', then you can use PowerShell to schedule a new task to run at startup:
$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-ExecutionPolicy Bypass C:\ProgramData\Amazon\EC2-Windows\Launch\Config\run-user-data.ps1'
$Trigger = New-ScheduledTaskTrigger -AtStartup
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
Register-ScheduledTask -TaskName 'Execute user-data' -InputObject $Task -User 'NT AUTHORITY\SYSTEM' -Force
I use this sort of solution by creating the mentioned file and command on 'AWS::CloudFormation::Init' sections.
Hope it helps!

How to pause a TeamCity build from a build step of another build

In TeamCity I have 2 related builds having different triggers.
While the first build is executing I'd like to pause the second build.
So my idea was to have a build step in build 1 to pause build 2. And then another build step to activate it again.
Is this possible in TeamCity? Or should I use a different approach?
I solved this problem by adding PowerShell build step sending a web request to the TeamCity API.
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(($($Username):$($Password)")))
$headers = #{ Authorization = "Basic $base64AuthInfo" }
Invoke-RestMethod -Method Put -Uri "http://rzaplp4:8088/app/rest/buildTypes/$($BuildId)/paused" -Body "true" -ContentType "text/plain" -Headers $headers > $null

Start-Process cmd does not work with Credential parameter when invoked from AWS Cloudformation template

I am creating an EC2 instance on AWS using Cloudformation template. As part of that template i am invoking a powershell script which performs the below steps (the script performs other tasks as well, but the below lines cause the error)
$MyFolder = "C:\installers\temp\Database"
$MyRHFolder = $MyFolder + "\Roundhouse"
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
Write-Host "Secure password generated"
$cred = New-Object System.Management.Automation.PSCredential $Username, $securePassword
Start-Process cmd -ArgumentList "/c RoundHouseInstall.cmd" -WorkingDirectory $MyRHFolder -Credential $cred -Wait
This fails everytime when it is invoked as part of the Cloudformation stack creation. There are strangely no errors thrown or any other output in the log. The RoundHouseInstall.cmd simply does not get invoked. However, if I manually log in to the EC2 and invoke the same script it works.
Also when I removed the -Credential parameter from the Start-Process command, the script worked fine even from Cloudformation, which means there is something going wrong with the credential parameter.
I need to run the script using the particular credential only otherwise the RoundHouseInstall.cmd will fail. I am out of my depth to understand why this fails and also what to try to make it work. Any assistance is appreciated.

Sitecore Ship returning 404 when trying to install update package

Ship returning 404 when trying to install update package.
Here is the script i'm trying for.
Invoke-WebRequest -Uri "http://mydomain/services/package/install" -Method Post -ContentType "application/x-www-form-urlencoded" -Body #{"path"="#D:\Users\Parsh\Desktop\test.update";"DisableIndexing"=$true} -UseBasicParsing -TimeoutSec 5000
And i'm getting below error.
`
404 - NotFound
The resource you have requested cannot be found.
We're sorry :-(
Graphics courtesy of the awesome Matthew Inman
At line:1 char:1
Invoke-WebRequest -Uri "http://mydomain/services/package/inst ...
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand`
I'm using .net framework 4.6.2 and Sitecore 8.2 initial release. also doubled checked all the configuration, everything looks fine,
Appreciate your support.
Update : Attached screenshot:
It took some time to debug the issue as it was returning 404. Initially i thought that, resource itself not available.Later debugging the Sitecore.Ship framework i found that, 404 throwing bcz the path was not found. See screenshot below.
When i changed the location of the update file it worked.