0

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

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

根据这篇文章,我可以获得一个带有用户 ID 的令牌(不确定它想要什么用户 ID,我正在使用服务主体)并发布一个帖子,但我希望像我一样的模块中有一个可用的实用程序用于我的脚本的其余部分。

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 令牌吗?

4

1 回答 1

2

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

首先,确保设置Enable Management REST APIYesAzure 门户中。

在此处输入图像描述

然后使用下面的命令。

$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

在此处输入图像描述

过了一会儿,在门户中检查它,它工作正常。

在此处输入图像描述

于 2021-04-16T02:39:30.633 回答