Search code examples
azureazure-devopsazure-yaml-pipelines

Azure yml - Search folder recursively in $(System.DefaultWorkingDirectory)


In azure.yml script, I would like to recursively search for a folder that ends with _test in $(System.DefaultWorkingDirectory)

So far, tried out

[ -d "**/$(System.DefaultWorkingDirectory)/**/?(*_test)" ]

and

 [ -d "$(System.DefaultWorkingDirectory)/**/*_test" ]

However, both patterns are not working.

Is it possible to do recursive search for a folder in $(System.DefaultWorkingDirectory)?

Can you please suggest a suitable method to search for folders ending with _test in $(System.DefaultWorkingDirectory)?

Thank you!


Solution

  • If you want to search for folders ending with _test in $(System.DefaultWorkingDirectory) in azure.yml script, please try the following yaml sample.

    pool:
      vmImage: ubuntu-latest
    
    steps:
    - pwsh: Get-ChildItem -Path $(System.DefaultWorkingDirectory) -Recurse -Directory -Name -Include "**_test"
    

    Update>> Microsoft-hosted Ubuntu agent has installed PowerShell tools, so PowerShell is available in it. If you prefer to use Bash script, this command find $(System.DefaultWorkingDirectory) -name "**_test" -type d will output the target folders path.

    Please note that there might be multiple folders, you could use following script to assign the result to variable.

    - bash: |
        result=$(find $(System.DefaultWorkingDirectory) -name "**_test" -type d)
        echo "$result"
    

    Or you could assign the result to array by reference to this thread.

    - bash: |
        readarray -d '' result_array < <(find $(System.DefaultWorkingDirectory) -name "**_test" -type d)
        echo "$result_array"