通过 azure devops 管道上传 .pfx 证书

Bad*_*der 5 devops azure-devops azure-pipelines

我想通过 azure devops 任务为我的应用服务上传 .pfx 证书。有人可以帮助我如何通过ARM模板上传证书

Lev*_*SFT 9

您可以按照以下步骤使用 ARM 上传证书。

1,转到Pipelines,Library下的安全文件并上传您的证书。 在此处输入图片说明

2、添加下载安全文件任务以将您的证书下载到您的管道。您可以通过路径引用它 $(<mySecureFile>.secureFilePath) or $(Agent.TempDirectory)。检查这里的有关预定义变量的详细信息

3、添加一个powershell任务来运行下面的脚本将你的证书转换为base64字符串。并将其存储到自定义环境变量中certificateBase64Content。查看此处以了解有关变量的更多信息

$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"
Run Code Online (Sandbox Code Playgroud)

4、创建一个keyvault,让Microsoft.Web 资源提供者访问KeyVault 获取证书,证书将存储在keyvault 中。

请查看博客“使用所需设置创建 KeyVault ”部分以获取 ARM 模板示例。

5、将证书存储在上述步骤中创建的密钥库中。

请查看博客将证书存储在 KeyVault部分以获取 ARM 模板示例。

6、参考博客的最后一步Deploy the certificate to your Web App来部署你的证书。

提醒

在上面的博客中,ARM 模板中定义的参数在 Azure 资源组部署任务中被覆盖。您可以在azure 资源组部署任务中模板设置下进行配置 在此处输入图片说明

补充

如果您不想使用密钥库。上面的第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"
      }
    }
  ]
Run Code Online (Sandbox Code Playgroud)