1

I have a number of subfolders in a repository that all contain an Azure ARM template (folder name = resource group).

I'm trying to iterate each folder and then create a pipeline artifact from contents of that folder.

How do I invoke a devops task from PowerShell script? The script below gives an error saying that PublishPipelineArtifact task is not found.

foreach ($folder in (Get-ChildItem -Path $env:SYSTEM_DEFAULTWORKINGDIRECTORY -Directory -ErrorAction SilentlyContinue | Select-Object Name))
{
    Write-Host "Processing folder $($folder.Name)..."

    Write-Host "##vso[task.PublishPipelineArtifact targetPath=$folder;artifactName=$($folder.Name);]"

    Write-Host "Published artifact $($folder.Name)..."
}

2 Answers 2

1

It's not supported to invoke a devops task from PowerShell script, you could try each function to loop the PublishPipelineArtifact task:

parameters:
- name: param
  type: object
  default:
  - folder1
  - folder2

 
steps:
  - ${{ each p in parameters.param }}:
      - task: PublishPipelineArtifact@1
        inputs:
          targetPath: '$(Pipeline.Workspace)' 
          artifactName: '${{ p }}'
Sign up to request clarification or add additional context in comments.

Comments

1

I found out that it's possible with "Logging commands". See https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md

For example this script packages all folders as artifacts:

Write-Host ("Working folder is {0}" -f $($env:SYSTEM_DEFAULTWORKINGDIRECTORY))

foreach ($folder in (Get-ChildItem -Path $env:SYSTEM_DEFAULTWORKINGDIRECTORY -Directory -ErrorAction SilentlyContinue | Select-Object Name))
{
    Write-Host "Processing folder $($folder.Name)..."

    $folderPath = $env:SYSTEM_DEFAULTWORKINGDIRECTORY + "\" + $folder.Name
    $artifactFolder = $env:BUILD_ARTIFACTSTAGINGDIRECTORY + "\" + $folder.Name
    $pathToZip = $artifactFolder + "\" + $folder.Name + ".zip"

    Write-Host "Creating archive to " + $artifactFolder

    New-Item -ItemType directory -Path $artifactFolder

    $compress = @{
        Path = $($folderPath) + "\*"
        CompressionLevel = "Fastest"
        DestinationPath = $pathToZip
    }
    Compress-Archive @compress

    Write-Host "##vso[artifact.upload containerfolder=$($folder.Name);artifactname=$($folder.Name)_drop;]$pathToZip"

    Write-Host "Published artifact $($folder.Name)..."
}

1 Comment

this is exactly what I have been trying using a yaml each loop (as in the answer from @Cece Dong) but if the list of folders to publish is generated dynamically at run time it is not possible de to the expansion of the parameter in the expression occurring before the list is generated leading to a "Expected sequence or mapping" error on parsing the pipeline

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.