繁体   English   中英

Azure DevOps 模板管道函数

[英]Azure DevOps template pipeline functions

我正在使用 Azure 管道模板。 我知道可以迭代项目列表,例如

parameters:
jobs: []

jobs:
  job: SomeSpecialTool # Run your special tool in its own job first
    steps:
      ${{ each job in parameters.jobs }}: # Then do each job
        ${{ each pair in job }}: 
        # Insert all properties other than "dependsOn"
        ${{ if ne(pair.key, 'dependsOn') }}:
            ${{ pair.key }}: ${{ pair.value }}
            dependsOn: # Inject dependency
            SomeSpecialTool
            ${{ if job.dependsOn }}:
        ${{ job.dependsOn }}

是否也可以先检查列表是否为空? 在迭代之前做一些预处理? 例如这样的事情?

parameters:
jobs: []

jobs:
  job: SomeSpecialTool # Run your special tool in its own job first
    steps:
        ${{ if not(empty(jobs))}}: 
        # Then do job preparation that is not needed when there is no job e.g.
        - checkout: self
            displayName: 'Get source files from GitHub'

        - task: HelmInstaller@1
          displayName: 'Install Helm on the agent'
          inputs:
                helmVersionToInstall: 'latest'    
      ${{ each job in parameters.jobs }}: # Then do each job
        ${{ each pair in job }}: 
        # Insert all properties other than "dependsOn"
        ${{ if ne(pair.key, 'dependsOn') }}:
            ${{ pair.key }}: ${{ pair.value }}
            dependsOn: # Inject dependency
            SomeSpecialTool
            ${{ if job.dependsOn }}:
        ${{ job.dependsOn }}

有可能的。 因为empty是不承认的。 您可以使用- ${{if parameters.jobs[0]}}:来检查作业是否为空。

如果jobs 为空,则parameters.jobs[0]将被评估为false。 请查看下面我的示例 yaml。

parameters:
  buildSteps: []


stages:
- stage: secure_buildstage
  pool: Hosted VS2017

  jobs:
  - job: secure_buildjob
    steps:

    - ${{if parameters.buildSteps[0]}}:  # will be evaluated to false if buildsteps is empty, and the following task will not be run,

      - script: echo ${{parameters.buildSteps[0]}} 
        displayName: 'Base: Pre-build'

      - script: echo Building
        displayName: 'Base: Build'

    - ${{ each step in parameters.buildSteps }}:
      - ${{ each pair in step }}:
          ${{ if ne(pair.key, 'script') }}:
            ${{ pair.key }}: ${{ pair.value }}       
          ${{ if eq(pair.key, 'script') }}: # checks for buildStep with script
            'Rejecting Script: ${{ pair.value }}': error # rejects buildStep when script is found         

我的 azure-pipeline.yml:

trigger: none

stages:

- template: deploy-jobs.yaml
  parameters:
    buildSteps:  
      - bash: echo Test #Passes
        displayName: Test - Will Pass
      - bash: echo "Test"
        displayName: Test 2 - Will Pass
      - bash: echo "Script Test" # Comment out to successfully pass
        displayName: Test 3 - Will Fail

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM