用于删除 Blob 存储中所有文件的 Azure CLI 命令不适用于启用防火墙的 AZ 存储

标签 azure azure-devops continuous-deployment azure-blob-storage azure-cli

我已经配置了 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

enter image description here

有人可以就此向我提出建议吗?

引用

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

https://matthewleak.medium.com/deploying-a-static-website-to-azure-storage-using-azure-devops-fa0bed457d07

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。

enter image description here

您可以尝试在存储帐户 -> 网络 -> 防火墙中手动添加此 IP。

如果您看到如下截图所示的错误,则表示 IP 有问题。

enter image description here

但是根据我的测试,这个脚本可以正常工作。也可以手动添加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'

经典:

enter image description here

关于用于删除 Blob 存储中所有文件的 Azure CLI 命令不适用于启用防火墙的 AZ 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66165741/

相关文章:

docker - 使用 docker 持续部署

azure - 使用 REST API 导出搜索词的 Azure Application Insights

Azure DevOps VSTest 任务 - appconfig 设置

azure - 是否可以通过 Azure DevOps 通过 Linux 环境对 Azure Function 进行持续部署 CI/CD?

java - Jenkins 错误 - errorWhenCurrentBuildResultIsWorseOrEqualTo.groovy

Docker CD 工作流程 - 使 docker 主机拉取新图像并部署它们

azure - AZURE VM (Ubuntu Server 16.04 LTS) 上的 Coturn WebRTC 问题

azure - Silverlight 3 中的新客户端网络堆栈可以直接连接到 Azure 存储或 Mesh 吗?

azure - Terraform:将敏感数据添加到 Azure KeyVault

c# - Azure Web 应用程序中的 ASP.Net Core 数据保护 API