I'm setting up a build template and can't figure out the syntax for an optional object type parameter. In my pipeline I'm calling the template like this:
stages:
- template: aspnet-core.yml@templates
parameters:
database:
name: 'SomeDatabase'
server: 'SomeServer'
I have the parameter defined like this in the template:
parameters:
database: null
I want to do a check like this in the template so I can run a task conditionally:
- ${{ if ne('${{ parameters.database }}', null) }}:
However, it's not liking the keyword null in the if statement, and I don't know how to represent the fact that it wasn't passed in. What are my options here?
I've found another solution to that, you can work with the length of the incoming object. If the object is empty, it's length is 0
parameters:
- name: myObject
type: object
default: []
steps:
- ${{ if not(eq(length(parameters.myObject), 0)) }}:
- script: |
echo "hello world"
displayName: "next task"
You can use below expression to check if a parameter is empty. For below example
- ${{if parameters.database}}:
Below is my testing template and azure-pipeline.yml.
the script task will only get executed when database
is evaluated to true. I tested and found database: ""
and database:
will be evalutated to false. If it is defined as database: {}
, it will be evaluated to true.
Template: deploy-jobs.yaml
parameters:
database: {}
stages:
- stage: buildstage
pool: Hosted VS2017
jobs:
- job: secure_buildjob
steps:
- ${{if parameters.database}}:
- script: echo "will run if database is not empty"
displayName: 'Base: Pre-build'
azure-pipeline.yml:
stages:
- template: deploy-jobs.yaml
parameters:
database: ""
To execute some tasks if database is empty you can use below statement:
steps:
- ${{if not(parameters.database)}}:
- script: echo "will run if database is empty"
displayName: 'Base: Pre-build'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With