将数组值作为参数传递给 azure devops yaml 模板

问题描述 投票:0回答:1

我们正在从 Azure DevOps yaml 管道任务动态获取 pom 文件列表。我们需要将每个 pom 文件作为参数传递给另一个 yaml 模板文件来构建 Java 应用程序。你能帮忙解决这个问题吗?

  1. 我们的目标是使用 Azure 管道从 Azure 存储库的拉取请求中获取更改的文件。那么我们只对那个 Maven 项目进行 sonarqube 分析

  2. 您得到的列表的格式是什么? - 数组列表

  3. 一个 PowerShell 任务会将更改的文件收集为 ArrayList。我需要将其传递给另一个任务

  4. “将每个 pom 文件作为参数传递给另一个 yaml 模板文件” - 您使用的模板是否与获取 pom 列表的任务在同一管道中? MainTemplate 不会有将所有更改的文件收集为数组列表的任务。然后我们将每个 pom 文件传递给另一个具有 Maven 任务的模板

#主模板

trigger:
  none
jobs:
- job: 'PSScriptAnalyzer'
  displayName: PSScriptAnalyzer
  pool:
    vmImage: 'ubuntu-latest'
  steps:
    - task: PowerShell@2
      displayName: 'GetChangedFiles'
      inputs:
        targetType: inline
        pwsh: true
        script: |     
          //it will collet changed files from a azure repo as arraylist from a pull request using azure devops api
          //I can able to get those files. Now need to pass this array list to another template which maven task
          //we will get this values as dynamically.           
          ex: $files = "folder1/pom.xml","folder2/xml"        
    template : maventemplate.yml

      Here i need to pass as parameter**
azure azure-pipelines
1个回答
0
投票

您可以在“PSScriptAnalyzer”作业的 changedFiles 任务中创建

输出变量
PowerShell@2
。然后在模板中定义一个作业级别变量
pomFiles
并使用参数传递
changedFiles
的值。请参阅下面的示例。

main.yaml

  • 创建输出变量
    changedFiles
    并将其值设置为
    $files
  • 将输出变量
    changedFiles
    的值传递给模板参数
    pomFiles
# `main.yaml`
jobs:
- job: 'PSScriptAnalyzer'
  displayName: PSScriptAnalyzer
  steps:
  - task: PowerShell@2
    name: setvar
    inputs:
      targetType: 'inline'
      script: |
        $files = "my-app/pom.xml","my-newapp/pom.xml"
        Write-Host "##vso[task.setvariable variable=changedFiles;isoutput=true]$files"
- template: maventemplate.yml
  parameters:
    pomFiles: $[ dependencies.PSScriptAnalyzer.outputs['setvar.changedFiles'] ]

maventemplate.yml

  • 定义参数
    pomFiles
  • 将参数
    pomFiles
    的值传递给作业级别变量
    pomFiles
parameters:
  - name: pomFiles
    type: string

jobs:
- job: Maven
  dependsOn: PSScriptAnalyzer
  variables:
    pomFiles: ${{parameters.pomFiles}}
  steps:
  - script: echo $(pomFiles)
  - task: Maven@3
  ...

然后我们可以看到

$files
的值被传递给作业级别变量
pomFiles

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.