如何从PowerShell脚本调用Azure Devops任务?

时间:2020-08-20 12:23:16

标签: powershell azure-devops azure-pipelines

我在存储库中有许多子文件夹,每个子文件夹都包含一个Azure ARM模板(文件夹名称=资源组)。

我试图遍历每个文件夹,然后根据该文件夹的内容创建管道工件。

如何从PowerShell脚本调用devops任务?下面的脚本给出一个错误,指出未找到PublishPipelineArtifact任务。

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 个答案:

答案 0 :(得分:0)

不支持从PowerShell脚本调用devops任务,您可以尝试使用each函数来循环PublishPipelineArtifact任务:

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

 
steps:
  - ${{ each p in parameters.param }}:
      - task: PublishPipelineArtifact@1
        inputs:
          targetPath: '$(Pipeline.Workspace)' 
          artifactName: '${{ p }}'

答案 1 :(得分:0)

我发现使用“记录命令”是可能的。参见https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md

例如,此脚本将所有文件夹打包为工件:

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)..."
}