我认为至少应该包含 jessehouwing 所描述的功能。我已经建立了自定义任务来做到这一点,我希望有一个计划,但 MS 没有足够的时间来解决这个问题。包含任务的来源可在:https://github.com/Microsoft/vsts-tasks/tree/master/Tasks
这是一篇很好的参考文章:
http://www.colinsalmcorner.com/post/developing-a-custom-build-vnext-task-part-1
我获取了 CmdLine 源并对其进行了修改以包含 If Condition 属性。我创建了一个默认值为 false 的构建变量,并勾选了 Allow at Queue Time,然后将该变量放入任务的 If Condition 属性中。我修改了powershell脚本:
If ($ifCondition -eq "true" -Or $ifCondition -eq "True" -Or $ifCondition -eq 1 -Or $ifCondition -eq -1)
{
Write-Host "Creating process"
...
}
当我对构建进行排队时,如果我将变量更改为 1、-1、true 或 True,则流程会执行;否则它不会。
task.json
{
"id": "3A056A74-E34F-4767-8DCD-3F9461F4BCEC",<<<---BE SURE TO CHANGE THIS
"name": "CmdLineEx",
"friendlyName": "Command Line Ex (Conditional)",
"description": "Run a command line with arguments",
"helpMarkDown": "[More Information](http://go.microsoft.com/fwlink/?LinkID=613735)",
"category": "Utility",
"visibility": [
"Build",
"Release"
],
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 0,
"Patch": 22
},
"groups": [
{
"name": "advanced",
"displayName": "Advanced",
"isExpanded": false
}
],
"inputs": [
{
"name": "filename",
"type": "string",
"label": "Tool",
"defaultValue": "",
"required": true,
"helpMarkDown": "Tool name to run. Tool should be found in your path. Optionally, a fully qualified path can be supplied but that relies on that being present on the agent.<br/> Note: You can use **$(Build.SourcesDirectory)**\\\\ if you want the path relative to repo."
},
{
"name": "arguments",
"type": "string",
"label": "Arguments",
"defaultValue": "",
"helpMarkDown": "Arguments passed to the tool",
"required": false
},
{
"name": "ifCondition",
"type": "string",
"label": "If Condition",
"defaultValue": "",
"helpMarkDown": "Performs task if this property is set (true, True, 1, -1).",
"required": false
},
{
"name": "workingFolder",
"type": "filePath",
"label": "Working folder",
"defaultValue": "",
"required": false,
"groupName": "advanced"
},
{
"name": "failOnStandardError",
"type": "boolean",
"label": "Fail on Standard Error",
"defaultValue": "false",
"required": false,
"helpMarkDown": "If this is true, this task will fail if any errors are written to the StandardError stream.",
"groupName": "advanced"
}
],
"instanceNameFormat": "Run $(filename)",
"execution": {
"Node": {
"target": "task.js",
"argumentFormat": ""
},
"PowerShell": {
"target": "$(currentDirectory)\\task.ps1",
"argumentFormat": "",
"workingDirectory": "$(currentDirectory)"
}
},
"messages": {
"CmdLineReturnCode": "%s exited with return code: %d",
"CmdLineFailed": "%s failed with error: %s"
}
}
task.ps1
param (
[string]$filename,
[string]$arguments,
[string]$ifCondition,
[string]$workingFolder,
[string]$failOnStandardError
)
Write-Host "filename = $filename"
Write-Host "arguments = $arguments"
Write-Host "ifCondition = $ifCondition"
Write-Host "workingFolder = $workingFolder"
Write-Host "failOnStandardError = $failOnStandardError"
#########################################################################
If ($ifCondition -eq "true" -Or $ifCondition -eq "True" -Or $ifCondition -eq 1 -Or $ifCondition -eq -1)
{
Write-Host "Creating process"
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $filename
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $arguments
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.StartInfo.WorkingDirectory = $workingFolder
Write-Host "Executing process..."
$p.Start() | Out-Null
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
$p.WaitForExit(300 * 1000)
Write-Host "Executing process complete"
Write-Host $stdout
Write-Host -Message ("Exit code : {0}" -f $p.ExitCode) -Verbose
if ( $p.ExitCode -eq 1)
{
Write-Error -Message ("Stderr : {0}" -f $stderr)
}
}
#########################################################################
upload.bat
tfx build tasks upload --task-path ./ --overwrite
pause
同样,我认为至少应该包含 jessehouwing 所描述的功能。