Azure DevOps管道任务来更新文件和签入TFS [英] Azure DevOps pipeline task to update a file and check-in TFS
问题描述
我正在使用Azure Dev OPS触发生成和部署.我在GIT分支中有角度代码,将从中触发生成,并且基于build#,我需要在TFS中更新文件并进行检入.
I'm using the Azure Dev OPS to trigger the build and deploy. I have angular code in GIT branch from which the build will be triggered and based on build# I need to update a file in TFS and check-in the same.
我添加了PowerShell任务以从GIT分支读取build#.但我不知道进一步的步骤来更新文件并在TFS分支中检入相同的文件.
I have added the PowerShell task to read the build# from GIT branch. But I don't know the further steps to update the file and check-in the same in TFS branch.
请建议使用PowerShell命令来实现上述任务.
Please suggest PowerShell commands to achieve the ablove mentioned tasks.
推荐答案
Azure DevOps管道任务以更新文件并签入TFS
Azure DevOps pipeline task to update a file and check-in TFS
You could invoke the REST API Pushes - Create to update the file and check-in the same in TFS branch with powershell scripts.
-
转到代理阶段,然后选择允许脚本访问OAuth令牌".请参阅在构建管道中添加PowerShell任务,以获取要更新文件并签入的分支上的最新提交:
Add a PowerShell task in your build pipeline to get the latest commit on the branch you want to update the file and check-in:
GET https://{instance}/{collection}/{project}/_apis/git/repositories/{repositoryId}/commits?api-version=5.0&branch={BranchName}&$top=1
-
更新文件并通过REST API在TFS分支中检入该文件:
update the file and check-in the same in TFS branch via REST API:
POST https://{instance}/{collection}/{project}/_apis/git/repositories/{repositoryId}/pushes?api-version=5.0
- 如果文件内容包含引号(\"test \"),则需要分析引号, 与其他特殊宪章相同.
- 使用存储库-列出以获取
repositoryId
. - You need to parse quotes if file content contains quotes (\"test\"), the same as other special charters.
- Use the Repositories - List to get the
repositoryId
.
正文(application/json)
Body (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"
}
}
]
}
]
}
作为测试结果:
注意:
希望这会有所帮助.
这篇关于Azure DevOps管道任务来更新文件和签入TFS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!