用于更新文件和签入 TFS 的 Azure DevOps 管道任务

Azure DevOps pipeline task to update a file and check-in TFS

我正在使用 Azure Dev OPS 触发构建和部署。我在 GIT 分支中有 angular 代码,将从中触发构建并基于构建# 我需要更新 TFS 中的文件并签入相同的文件。

我添加了 PowerShell 任务以从 GIT 分支读取构建号。但我不知道在 TFS 分支中更新文件和签入文件的进一步步骤。

请建议 PowerShell 命令以实现上述任务。

Azure DevOps pipeline task to update a file and check-in TFS

您可以调用 REST API Pushes - Create 来更新文件并使用 powershell 脚本在 TFS 分支中签入相同的文件。

  1. 转到代理阶段并select允许脚本访问 OAuth 令牌。参见 Use the OAuth token to access the REST API

  2. 在您的构建管道中添加一个 PowerShell 任务以获取分支上的最新提交您想要更新文件并签入:

    GET https://{instance}/{collection}/{project}/_apis/git/repositories/{repositoryId}/commits?api-version=5.0&branch={BranchName}&$top=1
    
  3. 更新文件并通过 REST 在 TFS 分支中签入相同的文件 API:

    POST https://{instance}/{collection}/{project}/_apis/git/repositories/{repositoryId}/pushes?api-version=5.0
    

正文(application/json):

{
  "refUpdates": [
    {
      "name": "refs/heads/$(BranchName)",
      "oldObjectId": "[step 2 commit ID]"
    }
  ],
  "commits": [
    {
      "comment": "Added a few more items to the task list.",
      "changes": [
        {
          "changeType": "edit",
          "item": {
            "path": "/tasks.md"
          },
          "newContent": {
            "content": "# Tasks\n\n* Item 1\n* Item 2\n* Item 3\n* Item 4\n\nIf you need to add more, update this file and add them!",
            "contentType": "rawtext"
          }
        }
      ]
    }
  ]
}

作为测试结果:

注:

  • 如果文件内容包含引号 (\"test\"),您需要解析引号, 与其他特殊包机相同。
  • 使用 Repositories - List 得到 repositoryId

希望这对您有所帮助。