I talked about the tfs-cli tool before. This tool allows you to create and upload your own custom build steps both in VSTS and TFS on premise. Unfortunately to be able to use it on your local TFS instance, you have to enable Basic Authentication. If you don’t want to do that, you have to use the REST api’s to upload your custom task.
Although this is certainly possible, it’s not that easy to do. Luckily a colleague spend some time creating a Powershell script to simplify the process:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true)][string]$TaskPath, | |
[Parameter(Mandatory=$true)][string]$TfsUrl, | |
[PSCredential]$Credential = (Get-Credential), | |
[switch]$Overwrite = $false | |
) | |
# Load task definition from the JSON file | |
$taskDefinition = (Get-Content $taskPath\task.json) -join "`n" | ConvertFrom-Json | |
$taskFolder = Get-Item $TaskPath | |
# Zip the task content | |
Write-Output "Zipping task content" | |
$taskZip = ("{0}\..\{1}.zip" -f $taskFolder, $taskDefinition.id) | |
if (Test-Path $taskZip) { Remove-Item $taskZip } | |
Add-Type -AssemblyName "System.IO.Compression.FileSystem" | |
[IO.Compression.ZipFile]::CreateFromDirectory($taskFolder, $taskZip) | |
# Prepare to upload the task | |
Write-Output "Uploading task content" | |
$headers = @{ "Accept" = "application/json; api-version=2.0-preview"; "X-TFS-FedAuthRedirect" = "Suppress" } | |
$taskZipItem = Get-Item $taskZip | |
$headers.Add("Content-Range", "bytes 0-$($taskZipItem.Length - 1)/$($taskZipItem.Length)") | |
$url = ("{0}/_apis/distributedtask/tasks/{1}" -f $TfsUrl, $taskDefinition.id) | |
if ($Overwrite) { | |
$url += "?overwrite=true" | |
} | |
# Actually upload it | |
Invoke-RestMethod -Uri $url -Credential $Credential -Headers $headers -ContentType application/octet-stream -Method Put -InFile $taskZipItem | |
Just invoke the script, specify the location of your custom tasks, the url of your TFS instance and your windows credentials.