我已经配置了 CICD 管道,用于构建前端文件并将其部署到 Azure Blob 存储中。
我将发布管道配置为在使用 az copy 上传新的文件之前清除所有文件。
IP=`curl -s http://ipinfo.io/json | jq -r '.ip'`
echo "firewall - Agent IP: $IP"
sleep 50
az storage account network-rule add -g Test_RG --account-name "Test_RG_1" --ip-address $IP
sleep 30
az storage blob delete-batch --account-name "Test_RG_1" --source '$web'
echo "Removing :$IP"
az storage account network-rule remove --account-name "Test_RG_1" --ip-address $IP
上面的脚本有时可以正常工作,无需任何更改,但它多次失败,抛出类似的错误
ERROR: BadRequestError: (InvalidValuesForRequestParameters) Values for request parameters are invalid: networkAcls.ipRule[*].value. For more information, see - https://aka.ms/storagenetworkruleset
The request may be blocked by network rules of storage account. Please check network rule set
有人可以就此向我提出建议吗?
引用
Azure Devops MS-hosted agent IP address
How to get the IP Address for Azure DevOps Hosted Agents to add to the white list
Azure DevOps pipeline cannot copy to Azure storage
https://learn.microsoft.com/en-us/cli/azure/ext/storage-preview/storage?view=azure-cli-latest
VSTS Release - Delete Azure BLOB Container / Contents
Uploading File in Azure using CLI
Network Rules of storage account blocking container creation
最佳答案
根据错误信息,该问题的根本原因是脚本获取的IP无效。
我过去遇到过同样的问题,但它可以使用相同的脚本(IP=curl -s http://ipinfo.io/json | jq -r '.ip'
).
当您执行脚本时,您可以在任务日志中看到IP。
您可以尝试在存储帐户 -> 网络 -> 防火墙中手动添加此 IP。
如果您看到如下截图所示的错误,则表示 IP 有问题。
但是根据我的测试,这个脚本可以正常工作。也可以手动添加IP。
此外,我在 Microsoft 托管的代理上运行这些脚本(例如 Ubuntu 16.04、18.04、20.04、windows-2019),您可以更改为使用这些代理并检查它是否可以工作。
更新:
您可以使用 Azure PowerShell 任务设置防火墙并使用 Azure CLI 任务执行 az cli 脚本:
这是一个例子:
steps:
- task: AzurePowerShell@5
displayName: 'Azure PowerShell script: Set Rule'
inputs:
azureSubscription: kevin0215
ScriptType: InlineScript
Inline: |
$IP= Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
$IP
Add-AzStorageAccountNetworkRule -ResourceGroupName "ResourceGroup" -AccountName "kevin0204" -IPAddressOrRange "$IP"
preferredAzurePowerShellVersion: ' 3.1.0'
- task: AzureCLI@2
displayName: 'Azure CLI Remove files'
inputs:
azureSubscription: kevin0215
scriptType: bash
scriptLocation: inlineScript
inlineScript: 'az storage blob delete-batch --account-name kevin0204 --source kevin0204 --auth-mode login'
- task: AzurePowerShell@5
displayName: 'Azure PowerShell script: Remove Rule'
inputs:
azureSubscription: kevin0215
ScriptType: InlineScript
Inline: |
$IP= Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
$IP
Remove-AzStorageAccountNetworkRule -ResourceGroupName "ResourceGroup" -AccountName "kevin0204" -IPAddressOrRange "$IP"
preferredAzurePowerShellVersion: ' 3.1.0'
经典:
关于用于删除 Blob 存储中所有文件的 Azure CLI 命令不适用于启用防火墙的 AZ 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66165741/