我正在使用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 }}
答案 0 :(得分:1)
有可能。由于那里empty
无法识别。您可以使用- ${{if 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