TFS REST Api Update Build Definition Or Pass Variable While Queuing the Build C#

我们两清 提交于 2021-01-28 21:23:42

问题


I've ran into a problem while trying to update my existing build definition in TFS by a console application written on C#. I am queuing a build successfully but the arguments i pass in parameters are not applied in the build as I can see. I tried to update the definition before I trigger it but i receive bad requests on it also.Which approach is better and if someone did it before I will ask for assistance with it. The only thing i need is to pass some parameters. I prefer not to pass strings as well too. Thanks :)

I am following the TFS REST Api documentation -> https://www.visualstudio.com/en-us/integrate/api/build/definition-templates


回答1:


You can try this. Customize your build process on fly

$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/$($env:SYSTEM_DEFINITIONID)?api-version=2.0"

$definition = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Method Get -ContentType application/json
Write-Host "Befor json = $($definition | ConvertTo-Json -Depth 100)" 

    $definition.build[1].enabled = "True"   
    $definition.build[1].inputs.msbuildArgs = "\OutPath bla-bla-bla"

    $Updatedefinition = Invoke-RestMethod -uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Method PUT -Body (Convertto-Json  $Updatedefinition  -Depth 100) -ContentType "application/json"

Write-Host "After json = $($Updatedefinition | ConvertTo-Json -Depth 100)" 



回答2:


Bad Request means you have a bad syntax. The syntax of Update a build definition should be look like:

PUT https://{instance}/defaultcollection/{project}/_apis/build/definitions/{definitionid}?api-version={version}

You can refer to the sample request to write your own syntax:

PUT https://fabrikam.visualstudio.com/DefaultCollection/Fabrikam-Fiber-Git/_apis/build/definitions/29?api-version=2.0
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"
}


来源:https://stackoverflow.com/questions/37351534/tfs-rest-api-update-build-definition-or-pass-variable-while-queuing-the-build-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!