【问题标题】:Azure DevOps pipeline task to update a file and check-in TFS用于更新文件和签入 TFS 的 Azure DevOps 管道任务
【发布时间】:2020-04-05 19:36:55
【问题描述】:

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

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

请建议 PowerShell 命令来完成上述任务。

【问题讨论】:

  • 这个问题有什么更新吗?你解决了这个问题吗?如果没有,请告诉我有关此问题的最新信息吗?
  • 谢谢利奥。刚才看到你的回复会尽量让你知道的。
  • 现在结果如何?下面的答案是否解决了您的问题,如果是,您可以接受它作为答案,这样它可以帮助遇到同样问题的其他社区成员,我们可以存档这个帖子,谢谢。如果没有,请告诉我们您是否需要进一步的帮助。
  • 我厌倦了在 Powershell 中运行以下命令,但它显示未知命令“GET https://{instance}/{collection}/{project}/_apis/git/repositories/{repositoryId}/commits ?api-version=5.0&branch={BranchName}&$top=1 " 请告诉我确切的命令。
  • 这个API你好像不太熟悉,有时间我给你提供完整的代码。

标签: git powershell tfs azure-devops azure-pipelines


【解决方案1】:

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

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

  1. 转到代理阶段并选择允许脚本访问 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 API 更新文件并在 TFS 分支中签入相同的文件:

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

正文(应用程序/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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-28
    • 2019-07-15
    • 2019-05-14
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多