使用 Powershell Az 模块将修订发布到开发人员门户

Use Powershell Az Module to Publish Revision to Developer Portal

我有一个 powershell 脚本在我的 API 的 Azure Devops 发布管道中运行,它使用我的 API 中的 swagger 文档自动将更改推送到 APIM。

缺少的步骤是,一旦我进行了修改,我需要使用最新的架构和规范更新开发人员门户。我不能让开发人员在每次发布时都出去点击发布按钮。

根据 ,我可以获得一个带有用户 ID 的令牌(不确定它想要什么用户 ID,我使用的是服务主体)并执行 post,但我希望模块中有一个可用的实用程序,就像我在其余脚本中使用的那样。

param([string]$subscriptionId = "", [string]$resourceGroup = "", [string]$apimName = "", [string]$apiId = "", [string]$swaggerJsonPath = "", [string]$apiPath = "", [string]$baseApiUrl = "", [string]$version = "")

$ErrorActionPreference = 'Stop'

# Install-Module -Name Az -AllowClobber -Scope CurrentUser
Import-Module Az

$ctx = Get-AzContext

if ($subscriptionId -eq '')
{
    $subscriptionId = $ctx.Subscription.Id
}

#fix version - this will come in as a build number with periods, which are not allowed
#use dashes instead
$version = $version.Replace('.', '-')

Write-Output "Resource Group: ${resourceGroup}"
Write-Output "APIM Name: ${apimName}"
Write-Output "Api Id: ${apiId}"
Write-Output "Swagger Path: ${swaggerJsonPath}"
Write-Output "Api Path: ${apiPath}"
Write-Output "Base Api Url: ${baseApiUrl}"
Write-Output "Version: ${version}"

# Set the context to the subscription Id
Select-AzSubscription -SubscriptionId $subscriptionId

Write-Output "Subscription loaded: ${subscriptionId}"

# load the API management gateway context 
$apimContext = New-AzApiManagementContext -ResourceGroupName $resourceGroup -ServiceName $apimName

Write-Output "APIM Context"
Write-Output $apimContext

# create a new revision with the supplied version (this should be the release number)
$apiRevision = New-AzApiManagementApiRevision -Context $apimContext -ApiId $apiId -ApiRevision $version

Write-Output "API Revision"
Write-Output $apiRevision

try
{
    Write-Output "Begin API Import from Swagger"

    # import the swagger as open id spec - this will fail if the name of the api in swagger does not match the name of the api in the gateway
    $importResult = Import-AzApiManagementApi -Context $apimContext -SpecificationUrl $swaggerJsonPath -SpecificationFormat OpenApi -ApiId $apiId -Path $apiPath -ServiceUrl $baseApiUrl -ApiRevision $apiRevision.ApiRevision -ApiVersion $apiRevision.ApiVersion

    Write-Output "Api refreshed from swagger [${swaggerJsonPath}]"
    Write-Output $importResult
}
catch
{
    Write-Output 'Api Import Failure'
    Write-Output $_.Exception

    Write-Output 'Beginning Cleanup'

    #clean up the revision we made
    Remove-AzApiManagementApiRevision -Context $apimContext -ApiId $apiId -ApiRevision $version

    Write-Output "Revision ${$apiRevision.ApiRevision} removed"

    exit 10
}

# set the revision as current (release it to the public)
$apiRelease = New-AzApiManagementApiRelease -Context $apimContext -ApiId $apiId -ApiRevision $version

Write-Output "API Release"
Write-Output $apiRelease

# TODO: Publish dev portal

如果没有已经提供的模块,我可以使用我的服务主体获得 sas 令牌吗?

azure powershell 中没有用于发布开发门户的内置命令,您的选择是使用您的服务主体获取 sas 令牌并通过 powershell 调用 api。

首先,确保设置 Enable Management REST API 在 Azure 门户中是 Yes

然后使用下面的命令。

$resourceGroup = "xxxxxx"
$apimName = "xxxxxx"
$apimContext = New-AzApiManagementContext -ResourceGroupName $resourceGroup -ServiceName $apimName
$Access = Get-AzApiManagementTenantAccess -Context $apimContext
$token = (New-AzApiManagementUserToken -Context $apimContext -UserId $Access.Id).UserToken

$header = @{
    'Authorization' = 'SharedAccessSignature ' + $token
}

Invoke-RestMethod -Method Post -Uri "https://$apimName.developer.azure-api.net/publish" -Headers $header

一段时间后,在门户中查看,它工作正常。