【发布时间】:2023-03-17 10:30:02
【问题描述】:
我想通过 azure devops 任务为我的应用服务上传 .pfx 证书。有人可以帮助我如何通过 ARM 模板上传证书
【问题讨论】:
标签: azure-devops azure-pipelines devops
我想通过 azure devops 任务为我的应用服务上传 .pfx 证书。有人可以帮助我如何通过 ARM 模板上传证书
【问题讨论】:
标签: azure-devops azure-pipelines devops
您可以按照以下步骤使用 ARM 上传证书。
2,添加download secure file task 以将您的证书下载到您的管道。您可以通过路径$(<mySecureFile>.secureFilePath) or $(Agent.TempDirectory) 引用它。有关预定义变量的更多信息,请查看here
3,添加一个 powershell 任务以在脚本下方运行,以将您的证书转换为 base64 字符串。并将其存储到自定义环境变量certificateBase64Content。查看here 了解有关变量的更多信息
$secName = “<certificateName>.pfx
$tempDirectory = $env:AGENT_TEMPDIRECTORY
$pfxFilePath = Join-Path $tempDirectory $secName
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$cert.Import($pfxFilePath, "$(certificatePassword)", $flag)
$bin = $cert.RawData
$base64Value = [System.Convert]::ToBase64String($bin)
Write-Host "##vso[task.setvariable variable=certificateBase64Content;]$base64Value"
4、创建一个keyvault并授予Microsoft.Web资源提供者访问KeyVault的权限以获取证书,该证书将存储在keyvault中。
请查看博客 "Create the KeyVault with the required settings" 部分以获取 ARM 模板示例。
5,将证书存储在上一步创建的密钥库中。
请查看博客 Store the certificate in KeyVault 部分以获取 ARM 模板示例。
6、参考博客Deploy the certificate to your Web App的最后一步部署你的证书。
提醒:
在上述博客中,ARM 模板中定义的参数在 Azure 资源组部署任务中被覆盖。您可以在 azure 资源组部署任务 中的 Template 设置下进行配置
加法:
如果您不想使用 keyvault。上面的第4、5步可以省略,直接上传证书,在上面第3步中把你的证书转换并存入自定义变量后,把parameters('certificatePfxBase64')替换成你自己定义的变量certificateBase64Content
"variables": {
"certificateName": "[concat(parameters('certificatePrefixName'), uniqueString(resourceGroup().id))]"
},
"resources": [
{
"apiVersion": "2015-08-01",
"name": "[variables('certificateName')]",
"type": "Microsoft.Web/certificates",
"location": "[resourceGroup().location]",
"properties": {
"pfxBlob": "[parameters('certificatePfxBase64')]",
"password": "[parameters('certificatePfxPassword')]"
},
"tags": {
"displayName": "Certificate"
}
}
]
【讨论】: