templates - Azure DevOps 模板管道函数

标签 templates azure-devops azure-pipelines

我正在使用 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 }}

最佳答案

这是可能的。由于那里未识别。您可以使用 - $ {{如果parameters.jobs [0]}}:检查作业是否为空。

如果作业为空,则 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

关于templates - Azure DevOps 模板管道函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59783076/

相关文章:

C++:声明、定义和调用返回模板类对象的函数?

azure-devops - 作业失败时 Azure Devops 管道完成并带有警告标志

iis - 从 Visual Studio Team Services 中的构建部署到私有(private) IIS 服务器

azure-devops - 查看 Azure Devops 构建管道上的操作日志

JavaScript 模板 : How should the partial template code be organized?

使用类型特征调用 C++ 条件函数

c++ - 了解 C++ 中的外部模板

powershell - 如何使用 Azure DevOps API 列出对项目存储库具有权限的组/用户?

azure-devops - Azure DevOps 部署组 - 是否可以忽略离线目标?

azure - azure devops 管道任务中 Docker@0、Docker@1 和 Docker@2 有什么区别?