在发布期间将发布说明发布到 TFS/VSTS Wiki

Publishing release notes to TFS/VSTS Wiki during Release

我的任务是在发布触发时自动在 WIKI 上生成和发布发布说明,为此我正在关注这个 Blog,它非常方便的博客,但我的运气不好仍然无法创建 wiki带有发布模板的页面。 (同时使用 Azure DevOps 和 TFS)

模板:

**Build Number**  : $($build.buildnumber)    
**Build started** : $("{0:dd/MM/yy HH:mm:ss}" -f [datetime]$build.startTime)     
**Source Branch** : $($build.sourceBranch)  
###Associated work items  
@@WILOOP@@  
* #$($widetail.id)
@@WILOOP@@  
###Associated change sets/commits  
@@CSLOOP@@  
* **ID $($csdetail.changesetid)$($csdetail.commitid)** 
  >$($csdetail.comment)    
@@CSLOOP@@

PowerShell 脚本

$content = [IO.File]::ReadAllText("$(System.DefaultWorkingDirectory)\releasenotes.md")
$data = @{content=$content;} | ConvertTo-Json;
$params = @{uri = '$(WikiPath)';
  Method = 'PUT';
  Headers = @{Authorization = "Bearer $(System.AccessToken)" };
  ContentType = "application/json";
  Body = $data;
}
Invoke-WebRequest @params

请指导我做错了什么

检查对 System.AccessToken 职位级别的访问权限:https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=classic

此外,检查您的 wiki 权限

经过测试,我们发现您提到的 blog uses this Rest API: Pages - Create Or Update 中的 PowerShell 脚本,因此 wikipath 是请求的 url 格式如下:

https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/{wikiIdentifier}/pages?path={path}&api-version=6.0

例如,我们创建了一个名为scrum-test.wiki的项目wiki,想新建一个名为Release notes[=的wiki页面34=], url 想在下面

https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/scrum-test.wiki/pages?path=Release notes&api-version=6.0

如果我们现在想在发行说明页面下创建一个名为 0.1.0 的子 wiki 页面,url 将如下所示

https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/scrum-test.wiki/pages?path=Release notes/0.1.0&api-version=6.0

此外,使用 AccessToken 我们总是会收到错误提示 "The wiki page operation failed with message : User does not have write permissions for this wiki." 即使我们授予身份的完整 wiki 权限:{项目名称} 构建服务({组织名称}),所以我们使用具有完全访问权限的 PAT authentication,它可以在下面的 PowerShell 脚本中正常工作。

$content = [IO.File]::ReadAllText("$(System.DefaultWorkingDirectory)\releasenotes.md")
$data = @{content=$content;} | ConvertTo-Json;

$connectionToken="PAT here"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$params = @{uri = '$(WikiPath)';
  Method = 'PUT';
  Headers = @{Authorization = "Basic $base64AuthInfo" };
  ContentType = "application/json";
  Body = $data;
}
Invoke-WebRequest @params