Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Powershell中的TFS生成定义上添加计划?_Tfs_Azure Devops_Azure Pipelines_Azure Powershell - Fatal编程技术网

如何在Powershell中的TFS生成定义上添加计划?

如何在Powershell中的TFS生成定义上添加计划?,tfs,azure-devops,azure-pipelines,azure-powershell,Tfs,Azure Devops,Azure Pipelines,Azure Powershell,我想在使用PowerShell的TFS REST API中获取的生成定义上添加一个调度(在代码示例中将调度添加到$buildDef变量) 我通过一个API请求获得构建定义,但是我不能为每个星期创建一个时间表作为触发器。我使用了下面的Api来更新触发计划 $buildDef = Invoke-RestMethod -Method Get -UseDefaultCredentials -ContentType application/json -Uri $TfsBuildDefinitionUri

我想在使用PowerShell的TFS REST API中获取的生成定义上添加一个调度(在代码示例中将调度添加到$buildDef变量)

我通过一个API请求获得构建定义,但是我不能为每个星期创建一个时间表作为触发器。我使用了下面的Api来更新触发计划

$buildDef = Invoke-RestMethod -Method Get -UseDefaultCredentials -ContentType application/json -Uri $TfsBuildDefinitionUri

任何帮助都将不胜感激,因为我无法完成。谢谢

首先,您必须使用以下方法获得ADO构建的构建定义:

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.1
附加参数:

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?revision={revision}&minMetricsTime={minMetricsTime}&propertyFilters={propertyFilters}&includeLatestBuilds={includeLatestBuilds}&api-version=5.1
然后它会给您一个
**[BuildTrigger][1]**
数组,然后您必须更新的时间表

时间表

无论变更集是否存在,都应按照指定的计划启动生成

以下是更新生成触发器的示例代码:

$definitionToUpdate = Invoke-RestMethod -Uri "$($collection)$($project.name)/_apis/build/definitions/$($definition.id)" -Method GET -Header $header
    $trigger = $definitionToUpdate.triggers | Where {$_.triggerType -eq 'continuousIntegration'}

    if ($trigger) {
        $trigger.branchFilters = $branchNames | % {"+refs/heads/$_/*"}
        Invoke-RestMethod -Uri "https://devops.domain.com/Collection/Project/_apis/build/definitions/$($definition.id)?api-version=5.0" -Method PUT -ContentType application/json -Body ($definitionToUpdate | ConvertTo-Json -Depth 10) -Header $header
    }

您可以参考线程以供进一步参考,希望对您有所帮助。

首先,您必须使用以下方法获得ADO构建的构建定义:

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.1
附加参数:

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?revision={revision}&minMetricsTime={minMetricsTime}&propertyFilters={propertyFilters}&includeLatestBuilds={includeLatestBuilds}&api-version=5.1
然后它会给您一个
**[BuildTrigger][1]**
数组,然后您必须更新的时间表

时间表

无论变更集是否存在,都应按照指定的计划启动生成

以下是更新生成触发器的示例代码:

$definitionToUpdate = Invoke-RestMethod -Uri "$($collection)$($project.name)/_apis/build/definitions/$($definition.id)" -Method GET -Header $header
    $trigger = $definitionToUpdate.triggers | Where {$_.triggerType -eq 'continuousIntegration'}

    if ($trigger) {
        $trigger.branchFilters = $branchNames | % {"+refs/heads/$_/*"}
        Invoke-RestMethod -Uri "https://devops.domain.com/Collection/Project/_apis/build/definitions/$($definition.id)?api-version=5.0" -Method PUT -ContentType application/json -Body ($definitionToUpdate | ConvertTo-Json -Depth 10) -Header $header
    }

您可以参考线程以供进一步参考,希望它能有所帮助。

因为您已经获得了构建定义。然后您只需要使用RESTAPI进行更新

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?secretsSourceDefinitionId={secretsSourceDefinitionId}&secretsSourceDefinitionRevision={secretsSourceDefinitionRevision}&api-version=5.0
在请求主体中,有一个BuildTrigger表示buld定义的触发器。这是一个数组[]。它包含时间表 字符串:无论变更集是否存在,都应按照指定的计划启动生成

身体样本供您参考:

Content-Type: application/json
{
  "id": 29,
  "revision": 1,
  "name": "myFavoriteDefinition",
  "definitionType": "build",
  "documentQuality": "definition",
  "queue": {
    "id": 1
  },
  "build": [
    {
      "enabled": true,
      "continueOnError": false,
      "alwaysRun": false,
      "displayName": "Build solution **\\*.sln",
      "task": {
        "id": "71a9a2d3-a98a-4caa-96ab-affca411ecda",
        "versionSpec": "*"
      },
      "inputs": {
        "solution": "**\\*.sln",
        "msbuildArgs": "",
        "platform": "$(platform)",
        "configuration": "$(config)",
        "clean": "false",
        "restoreNugetPackages": "true",
        "vsLocationMethod": "version",
        "vsVersion": "latest",
        "vsLocation": "",
        "msbuildLocationMethod": "version",
        "msbuildVersion": "latest",
        "msbuildArchitecture": "x86",
        "msbuildLocation": "",
        "logProjectEvents": "true"
      }
    },
    {
      "enabled": true,
      "continueOnError": false,
      "alwaysRun": false,
      "displayName": "Test Assemblies **\\*test*.dll;-:**\\obj\\**",
      "task": {
        "id": "ef087383-ee5e-42c7-9a53-ab56c98420f9",
        "versionSpec": "*"
      },
      "inputs": {
        "testAssembly": "**\\*test*.dll;-:**\\obj\\**",
        "testFiltercriteria": "",
        "runSettingsFile": "",
        "codeCoverageEnabled": "true",
        "otherConsoleOptions": "",
        "vsTestVersion": "14.0",
        "pathtoCustomTestAdapters": ""
      }
    }
  ],
  "repository": {
    "id": "278d5cd2-584d-4b63-824a-2ba458937249",
    "type": "tfsgit",
    "name": "Fabrikam-Fiber-Git",
    "localPath": "$(sys.sourceFolder)/MyGitProject",
    "defaultBranch": "refs/heads/master",
    "url": "https://fabrikam.visualstudio.com/DefaultCollection/_git/Fabrikam-Fiber-Git",
    "clean": "false"
  },
  "options": [
    {
      "enabled": true,
      "definition": {
        "id": "7c555368-ca64-4199-add6-9ebaf0b0137d"
      },
      "inputs": {
        "parallel": "false",
        "multipliers": "[\"config\",\"platform\"]"
      }
    }
  ],
  "variables": {
    "forceClean": {
      "value": "false",
      "allowOverride": true
    },
    "config": {
      "value": "debug, release",
      "allowOverride": true
    },
    "platform": {
      "value": "any cpu",
      "allowOverride": true
    }
  },
  "triggers": [],
  "comment": "renamed"
}
至于如何在powershell中调用Rest API,google中有多个示例,您也可以看一下这个示例:

$body = '
{ 
       ...
}
'
$bodyJson=$body | ConvertFrom-Json
Write-Output $bodyJson
$bodyString=$bodyJson | ConvertTo-Json -Depth 100
Write-Output $bodyString
$user="name"
$token="PAT"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$Uri = "rest api url"
$buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $bodyString -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
write-host ....

因为您已经获得了构建定义。然后您只需要使用RESTAPI进行更新

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?secretsSourceDefinitionId={secretsSourceDefinitionId}&secretsSourceDefinitionRevision={secretsSourceDefinitionRevision}&api-version=5.0
在请求主体中,有一个BuildTrigger表示buld定义的触发器。这是一个数组[]。它包含时间表 字符串:无论变更集是否存在,都应按照指定的计划启动生成

身体样本供您参考:

Content-Type: application/json
{
  "id": 29,
  "revision": 1,
  "name": "myFavoriteDefinition",
  "definitionType": "build",
  "documentQuality": "definition",
  "queue": {
    "id": 1
  },
  "build": [
    {
      "enabled": true,
      "continueOnError": false,
      "alwaysRun": false,
      "displayName": "Build solution **\\*.sln",
      "task": {
        "id": "71a9a2d3-a98a-4caa-96ab-affca411ecda",
        "versionSpec": "*"
      },
      "inputs": {
        "solution": "**\\*.sln",
        "msbuildArgs": "",
        "platform": "$(platform)",
        "configuration": "$(config)",
        "clean": "false",
        "restoreNugetPackages": "true",
        "vsLocationMethod": "version",
        "vsVersion": "latest",
        "vsLocation": "",
        "msbuildLocationMethod": "version",
        "msbuildVersion": "latest",
        "msbuildArchitecture": "x86",
        "msbuildLocation": "",
        "logProjectEvents": "true"
      }
    },
    {
      "enabled": true,
      "continueOnError": false,
      "alwaysRun": false,
      "displayName": "Test Assemblies **\\*test*.dll;-:**\\obj\\**",
      "task": {
        "id": "ef087383-ee5e-42c7-9a53-ab56c98420f9",
        "versionSpec": "*"
      },
      "inputs": {
        "testAssembly": "**\\*test*.dll;-:**\\obj\\**",
        "testFiltercriteria": "",
        "runSettingsFile": "",
        "codeCoverageEnabled": "true",
        "otherConsoleOptions": "",
        "vsTestVersion": "14.0",
        "pathtoCustomTestAdapters": ""
      }
    }
  ],
  "repository": {
    "id": "278d5cd2-584d-4b63-824a-2ba458937249",
    "type": "tfsgit",
    "name": "Fabrikam-Fiber-Git",
    "localPath": "$(sys.sourceFolder)/MyGitProject",
    "defaultBranch": "refs/heads/master",
    "url": "https://fabrikam.visualstudio.com/DefaultCollection/_git/Fabrikam-Fiber-Git",
    "clean": "false"
  },
  "options": [
    {
      "enabled": true,
      "definition": {
        "id": "7c555368-ca64-4199-add6-9ebaf0b0137d"
      },
      "inputs": {
        "parallel": "false",
        "multipliers": "[\"config\",\"platform\"]"
      }
    }
  ],
  "variables": {
    "forceClean": {
      "value": "false",
      "allowOverride": true
    },
    "config": {
      "value": "debug, release",
      "allowOverride": true
    },
    "platform": {
      "value": "any cpu",
      "allowOverride": true
    }
  },
  "triggers": [],
  "comment": "renamed"
}
至于如何在powershell中调用Rest API,google中有多个示例,您也可以看一下这个示例:

$body = '
{ 
       ...
}
'
$bodyJson=$body | ConvertFrom-Json
Write-Output $bodyJson
$bodyString=$bodyJson | ConvertTo-Json -Depth 100
Write-Output $bodyString
$user="name"
$token="PAT"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$Uri = "rest api url"
$buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $bodyString -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
write-host ....

最后,我在构建定义中创建/添加了一个定时触发器。如果生成定义没有任何触发器,则属性(生成对象)或json数组(生成对象转换为数组)不存在。所以,我们必须加上它。以下是解决方案:

$triggerValue = @"
[
  {
    "schedules":[
        {
            "branchFilters":[
                "+$/Roche.DP.CommServer"
            ],
            "timeZoneId":"W. Europe Standard Time",
            "startHours":$startHoursNB,
            "startMinutes":$startMinutesNB,
            "daysToBuild":"all"
        }
    ],
    "triggerType":"schedule"
  }
]
"@

$buildDef | add-member -Name "triggers" -value (Convertfrom-Json $triggerValue) -MemberType NoteProperty

也感谢您的帮助。

最后,我在构建定义中创建/添加了一个计划触发器。如果生成定义没有任何触发器,则属性(生成对象)或json数组(生成对象转换为数组)不存在。所以,我们必须加上它。以下是解决方案:

$triggerValue = @"
[
  {
    "schedules":[
        {
            "branchFilters":[
                "+$/Roche.DP.CommServer"
            ],
            "timeZoneId":"W. Europe Standard Time",
            "startHours":$startHoursNB,
            "startMinutes":$startMinutesNB,
            "daysToBuild":"all"
        }
    ],
    "triggerType":"schedule"
  }
]
"@

$buildDef | add-member -Name "triggers" -value (Convertfrom-Json $triggerValue) -MemberType NoteProperty

还感谢您的帮助。

感谢您的帮助@PatrickLu MSFT,但我终于找到了在PowerShell的生成定义上创建触发器的方法。感谢您的帮助@PatrickLu MSFT,但我终于找到了在PowerShell的生成定义上创建触发器的方法。