有没有办法自动将安全文件上传到 azure devops 库?

Is there a way to upload secure files to azure devops library automatically?

我正在尝试将密钥作为安全文件上传到 azure devops 以在管道上下文中使用,我只发现手动上传但它不是选项,有没有一种方法可以通过 powershell 或任何管道任务自动完成?请提出建议。

您可以使用 REST API 来执行此操作:

POST https://dev.azure.com/{organization}/{project}/_apis/distributedtask/securefiles?api-version=5.0-preview.1&name={fileName}

Content-Type=application/octet-stream

请在 GitHUb 上检查 this topic

你会发现there甚至powershell脚本:

param
(
    [Parameter(Mandatory=$true)] [string] $PAT,
    [Parameter(Mandatory=$True)][ValidateNotNullOrEmpty()]$AzureDevOpsOrg,
    [Parameter(Mandatory=$True)][ValidateNotNullOrEmpty()]$AzureDevOpsProjectID,
    [Parameter(Mandatory=$true)] [string] $SecureNameFile2Upload,
    [Parameter(Mandatory=$true)] [string] $SecureNameFilePath2Upload
)

try{
    $base64AuthInfo=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$PAT)))
    $uploadSecureFileURI="https://dev.azure.com/$AzureDevOpsOrg/$AzureDevOpsProjectID/_apis/distributedtask/securefiles?api-version=5.0-preview.1&name=$SecureNameFile2Upload"
    $headers = @{
        Authorization=("Basic {0}" -f $base64AuthInfo)
    }
    Invoke-RestMethod -Uri $uploadSecureFileURI -Method Post -ContentType "application/octet-stream" -Headers $headers -InFile "$SecureNameFilePath2Upload"
}
catch
{
    write-host -f Red "Error upload client certificate file [$SecureNameFilePath2Upload] to AzureDevOps secure file!" $_.Exception.Message
    throw "Error occors -> $_.Exception.Message"
}