在 Azure Devops Build Pipeline 中添加 terraform 计划输出作为 PR 注释
恐怕目前还没有这种开箱即用的方式来添加 terraform plan output 作为 PR 评论。
我现在正在考虑的解决方法是调用 Rest API 以使用来自 terraform plan 命令的内容创建 PR 评论。
我们可以使用 Rest API Pull Request Thread Comments - Create:
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/threads/{threadId}/comments?api-version=5.1
创建对 PR 的评论。我们可以将 terraform plan 命令中的内容写为请求正文:
{
"content": "YourContentsFromTerraformPlan ",
"parentCommentId": 1,
"commentType": 1
}
但以上 API 需要pullRequestId。因此,我们还需要另一个 API 来获取当前项目的 pullRequestId Pull Requests - Get Pull Requests By Project:
GET https://dev.azure.com/{organization}/{project}/_apis/git/pullrequests?api-version=5.1
它会返回一系列 pullRequestIds,然后我们可以使用 powershell 参数Select-Object -first 1 like: $LastPullRequestId= $pullRequestIds.value.id | Select-Object -first 1 来获取最新的pullRequestId。
因此,我们可以在构建管道中添加两个内联 powershell 任务来调用 Rest API 以获取最新的 pullRequestId,然后使用此 pullRequestId 创建 PR 评论。
希望这会有所帮助。