在Team Build 2015中控制构建任务流程

时间:2016-04-10 07:59:19

标签: build tfsbuild tfs2015

是否可以在TFS2015 build vNext中集中控制构建流程?

说,我希望在构建之前和之后执行一些业务任务(自定义任务),但不希望用户能够在构建定义编辑器中删除或更改这些任务的位置。

或者有没有办法通过不暴露任务来创建构建定义模板以实现相同的目标?

注意:我不想使用XAML构建定义,因为它没有新的xPlat构建功能等。

2 个答案:

答案 0 :(得分:2)

不,这是不可能的。如果它们是自定义任务,则可以设置并检查变量以确保它们以正确的顺序运行,但这是您必须自己实现的。

构建框架或代理基础结构中目前没有任何内容可以强制执行或部分指定只能在特定位置扩展的构建模板。

也不可能在工作流程的早期阶段注册类似“终结者”的内容。

答案 1 :(得分:0)

至少,我认为应该包括 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的构建变量,并在队列时检查允许,然后将变量放在任务的 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 描述的功能。