【发布时间】:2020-10-24 13:42:04
【问题描述】:
我在自托管 Azure DevOps 管道中有以下脚本,以便将构建工件复制到 Azure blob 存储(静态网站)。
任务如下,我尝试使用New-AzStorageContainerSASToken,构建步骤日志也告诉它获取服务主体。但是,我似乎一直无法从 DevOps 管道放入任务中的服务主体为容器生成 SAS 令牌,因此我可以在 AzCopy 中使用它。
- task: AzureCLI@2
displayName: 'Copy test'
inputs:
azureSubscription: 'TestAccount (aaaaaaaa-bbbb-cccc-bbbb-ffffffffffff)'
addSpnToEnvironment: true
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
$ctx = New-AzStorageContext -StorageAccountName "some123teststorage" -UseConnectedAccount
$StartTime = Get-Date
$EndTime = $startTime.AddDays(1)
$sasToken = New-AzStorageContainerSASToken -Name "$web" -Permission rwd -StartTime $StartTime -ExpiryTime $EndTime -context $ctx
azcopy copy "$(System.DefaultWorkingDirectory)\test-app\dist\" "$(destination_blob)" --put-md5 --recursive --overwrite=false --check-length=false --log-level=info
管道中的错误是
New-AzStorageContext : Context cannot be null. Please log in using Connect-AzAccount.
总而言之,在 DevOps 管道日志中注明
C:\WINDOWS\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" cloud set -n AzureCloud"
C:\WINDOWS\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" login --service-principal -u *** "--password=***" --tenant *** --allow-no-subscriptions"
[
{
"cloudName": "AzureCloud",
"homeTenantId": "***",
"id": "<snip>",
"isDefault": true,
"managedByTenants": [],
"name": "TestAccount",
"state": "Enabled",
"tenantId": "***",
"user": {
"name": "***",
"type": "servicePrincipal"
}
}
]
C:\WINDOWS\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" account set --subscription <snip>"
"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\azuredevops\agent\_work\_temp\azureclitaskscript1593799829465.ps1'"
***
New-AzStorageContext : Context cannot be null. Please log in using Connect-AzAccount.
值得注意的是 Azure file copy 任务在发布管道中有效。不过,我现在想直接从构建管道中执行此操作。
【问题讨论】:
标签: azure powershell azure-devops azure-cli azure-devops-self-hosted-agent