點選上方藍字關註“汪宇傑部落格”

今天我在為一個從TFVC遷移到Git的老專案重新配置釋出到Azure App Service的CI/CD管線的時候,Azure DevOps竟然爆了。這是一個微軟已知的bug,目前還未修複,我來帶大家看看如何手工workaround這個問題。
首先,部署的時候報錯訊息如下:

Failed to get resource ID for resource type ‘Microsoft.Web/Sites’ and resourcename ‘moonglade’. Error: Could not fetch access token for Managed ServicePrincipal. Please configure Managed Service Identity (MSI) for virtual machine’https://aka.ms/azure-msi-docs’.Status code: 400, status message: Bad Request
本質上是因為Azure DevOps到Azure的連線無效,或者過期了。但是當我嘗試重新認證的時候,竟然又爆了:
TF14045: The identity with type ‘Microsoft.IdentityModel.Claims.ClaimsIdentity’ could not be found.

對此,微軟有個已知bug可以追蹤:https://developercommunity.visualstudio.com/content/problem/412380/tf14045-the-identity-with-type-microsoftidentitymo-1.html
解決方法
進入 Azure Active Directory > App registrations (Preview)
點選 “+ New registration“

指定一個 Name,如 ediwang-AzureDevOps
選擇 “Accounts in this organizational directory only“
Redirection URL 填: https://VisualStudio/SPN

Description 任意,Expires 建議選 Never

然後把 Client Secrets 裡的金鑰複製出來

還有Overview 裡的 Application (client) ID 以及 Directory (tenant) ID

然後到 Subscription > Access control (IAM) 裡搜尋剛才建立的App名字,搜到以後,Add a role assignment

然後安排一個 Owner,Azure這邊就搞定了!

然後回到 Azure DevOps
在 Azure App Service Deploy 的任務裡點 Manage

在 Service Connections 裡新增一個 Azure Resource Manager

然後點“use the full version of the service connection dialog.”

選擇和輸入對應的值。其中 Service pricipal client ID 就是剛才複製的 Application (client) ID,Service pricipal key 就是剛才 Client Secrets 裡複製的 VALUE

最後點選 Verify connection,不成功便成仁!

到此為止,就全部搞定了。重新回到你的部署任務裡,就能成功選擇標的Azure App Service上的網站了,並且能部署成功。

真是不容易……