【问题标题】:bash if statement inside the azure devops task is not executingbash if azure devops 任务中的语句未执行
【发布时间】:2021-11-29 17:57:03
【问题描述】:

我试图在现有存储帐户中自动创建 Azure 文件系统和目录(如果它们尚不存在)。我为此使用 Azure devops 管道任务。在 Azuredevops 管道内的“bash”和“azurecli”任务中尝试以下内联脚本。但是它无法检查容器中是否存在 blob 或文件,只是简单地传递任务而不给出任何错误,但它也没有创建任何资源。 在这里,所有这些参数都被正确定义为 yaml 中的 runtinme 参数。我正在使用的 bash 脚本似乎有些问题

      - task: AzureCLI@2         
        displayName: 'create the filesystem ${{parameters.fsname}} & parent directory ${{parameters.parentdirectory}} if not exists'
        inputs:
          azureSubscription: ${{parameters.subscription}}
          scriptType: 'bash'
          scriptLocation: 'inlineScript'
          inlineScript: |
                 existing_fs=$(az storage fs exists -n ${{parameters.fsname}} --account-name ${{parameters.storagename}} --auth-mode login)|cut -d : -f 2 | sed 's/.//'
                 existing_directory=$(az storage fs directory exists -n ${{parameters.parentdirectory}} -f ${{parameters.fsname}} --account-name ${{parameters.storagename}} --auth-mode login)|cut -d : -f 2 | sed 's/.//'
                 if [ "$existing_fs" = false ]; then
                   az storage fs create -n ${{parameters.fsname}} --account-name ${{parameters.storagename}} --auth-mode login
                 fi      
                 if [ "$existing_directory" = false ]; then
                     az storage fs directory create -n ${{parameters.parentdirectory}} -f ${{parameters.fsname}} --account-name ${{parameters.storagename}} --auth-mode login
                 fi 

【问题讨论】:

  • 我不太精通 azurecli,但 bash 永远不会认识到这一点:${{parameters.fsname}} 作为变量...
  • 不.. 但对于我的另一项任务,它按预期工作。
  • 内联脚本:| acl=user:${{ user }}:${{parameters.permission}} az storage fs access update-recursive --acl=$acl -p ${{parameters.subdirectory}} -f ${{parameters.fsname }} --account-name ${{parameters.storagename}}

标签: bash shell azure-pipelines azure-cli


【解决方案1】:

我已经在我的环境中测试过了。

azure devops 任务中的 if 语句未执行,因为变量existing_fs 和existing_directory 不等于false。

您可以使用 powershell 脚本代替 bash 脚本。

而内联脚本可以如下:

$existing_fs=(az storage fs exists -n ${{parameters.fsname}} --account-name ${{parameters.storagename}} --auth-mode login | ConvertFrom-Json).exists
$existing_directory=(az storage fs directory exists -n ${{parameters.parentdirectory}} -f ${{parameters.fsname}} --account-name ${{parameters.storagename}} --auth-mode login | ConvertFrom-Json).exists 
if (!$existing_fs)
   az storage fs create -n ${{parameters.fsname}} --account-name ${{parameters.storagename}} --auth-mode login
if (!$existing_directory)
   az storage fs directory create -n ${{parameters.parentdirectory}} -f ${{parameters.fsname}} --account-name ${{parameters.storagename}} --auth-mode login

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多