在带有Azure Pipeline模板的两个作业之间使用参数

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

我正在作业A中创建参数,并想在作业B中使用它们,但是当我使用模板时,作业B无法从作业A中获取参数值。这是我正在尝试的内容:

fetchingfilenames.yml

jobs:
 - job: A
   pool:
    vmImage: 'ubuntu-latest'
   steps:
   - checkout: myrepo
     persistCredentials: true
   - task: PowerShell@2
     displayName: 'Fetching FileNames'
     inputs:
       targetType: 'inline'
       script: |
         Write-Host "Fetching filenames"
         cd $(valuepath)
         Write-Host $(valuepath)
         ##Listing files in the directory and saving them in an array
         $a=ls
         Write-Host "Files:"$a
         $List = $a | foreach {'$(valuepath)/' + $_}
         Write-Host "Files with complete path:"$List
         $b = '"{0}"' -f ($List -join '","')
         Write-Host $b    ####Output is: "$(valuepath)/file1.yml, $(valuepath)/file2.yml"
         Write-Host "##vso[task.setvariable variable=valuefiles;isOutput=true]$b"
     name: fileoutput          

deploy.yml

parameters:
  files: []
jobs:
- job: B
  dependsOn: A
  pool:
    vmImage: 'ubuntu-latest'  
  variables:
    filenames: ${{ parameters.files }}  
  steps:  
  - checkout: myrepo
    persistCredentials: true
  - task: AzureCLI@2
    inputs:
      azureSubscription: 'mysubscription'
      scriptType: 'bash'
      scriptLocation: 'inlineScript'
      inlineScript: |
        echo "fetching filenames"
        echo $(filenames)   ####Error: output is empty
        for i in $(echo $(filenames) | sed "s/,/ /g");  ###it doesn't run this line as it seems it can not find $(filename)
        do 
          echo "valuefiles= $i"; 
        done       

azure-pipeline.yml

trigger:
  branches:
    include:
      - master
  paths:
    include:
      - azure-pipeline.yml

variables:
  azureSubscription: 'mysubscription'
  containerRegistry: 'myacr'
  repository: 'myrepo'
  chartPath: 'mypath'

resources:
  repositories:
  - repository: pipelinetemplates
    type: github
    name: myorg/mytemplates
    endpoint: myendpoint
  - repository: myrepo
    type: github
    name: myorg/myrepo
    endpoint: myendpoint
    trigger:
      branches:
        include:
        - master
      paths:
        include:
        - myfolder/*

stages:
- stage: Deploy
  variables:
    azureResourceGroup: 'myresourcegroup'
    kubernetesCluster: 'myk8s'
    domain: 'mydomain'
    valuepath: myfolder   

  jobs:
  - template: Template-Yaml/fetchingfilenames.yml@pipelinetemplates
  - template: Template-Yaml/deploy.yml@pipelinetemplates
    parameters: 
      ##Fetching variable as parameters
      files : $[dependencies.A.outputs['fileoutput.valuefiles']]       

如果我将作业A直接放在azure-pipeline.yml中而不使用模板,它可以很好地工作,但是从模板中获取作业A无效,因为作业B不能再从作业A中获取参数。

有人知道这里缺少什么吗?

parameter-passing azure-pipelines
1个回答
0
投票

fetchingfilenames.yml

jobs: 
- job: A 
  steps:
  - powershell: |
      cd $(System.defaultworkingdirectory)
      $a=ls
      Write-Host "Files:"$a
      $List = $a | foreach {'$(valuepath)/' + $_}
      Write-Host "Files with complete path:"$List
      $b = '"{0}"' -f ($List -join '","')
      Write-Host $b    
      Write-Host "##vso[task.setvariable variable=valuefiles;isOutput=true]$b"   
    name: power

deploy.yml

parameters:
  files: []

jobs: 
- job: B
  dependsOn: A
  variables: 
    filenames: ${{parameters.files}}
  steps:
  - powershell: |
      echo $(filenames)
      foreach($a in $(filenames)){write-host $a} 

azure-pipelines.yml:

resources:
  repositories:
  - repository: pipelinetemplates
    type: git
    name: PipelineBuildResourceYaml

stages:
- stage: SA
  pool: Default
  variables:
    valuepath: "myfolder"   

  jobs:
  - template: fetchingfilenames.yml@pipelinetemplates
  - template: deploy.yml@pipelinetemplates
    parameters:  
      ##Fetching variable as parameters
      files: $[dependencies.A.outputs['power.valuefiles']] 

作业B的输出:

enter image description here

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