Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you conditionally set the value of an Azure DevOps parameter?

I have an Azure DevOps pipeline that is used to rotate some keys. There are two primary requirements -

  • Only rotate one key at at time.
  • Any key can be rotated ad-hoc through a manual run.

To do this, I plan to use a cron schedule that runs on various days. A parameter should then be used with the default being set to a specific 'Key Kind' based on the day of the week. Using a parameter means that a user can also specify the key to rotate when running the pipeline manually.

Unfortunately what I've come up with doesn't work. Each of the four expressions in the parameters yeild the following error -

A template expression is not allowed in this context

According to the documentation...

Compile time expressions can be used anywhere

...but that does not seem to be correct. I'm hoping that I'm missing something rather than the documentation being incorrect, but either way I'm not sure how I can achieve my goal.

pr: none
trigger: none
schedules:
- cron: 0 2 * * 0-3,6
  displayName: Rotate a key
  branches:
    include:
    - develop
  always: true

parameters:
- name: keyKinds
  displayName: Key Kinds
  type: string
  ${{ if not(in(format('{0:dddd}', pipeline.startTime), 'Sunday', 'Monday', 'Tuesday')) }}:
    default: primary
  ${{ if eq(format('{0:dddd}', pipeline.startTime), 'Sunday') }}:
    default: secondary
  ${{ if eq(format('{0:dddd}', pipeline.startTime), 'Monday') }}:
    default: primaryReadonly
  ${{ if eq(format('{0:dddd}', pipeline.startTime), 'Tuesday') }}:
    default: secondaryReadonly
  values:
  - primary
  - secondary
  - primaryReadonly
  - secondaryReadonly
like image 412
David Gard Avatar asked Oct 20 '20 15:10

David Gard


People also ask

How do you add multiple conditions in Azure pipeline?

You could add two same tasks in the pipeline, one with the condition ((Var1==A || Var1==B || Var1==C) && (Var2==2)) and another with condition ((Var1==A) &&(Var2==1)) , this should be work.

What are parameters in Azure DevOps?

Azure DevOps Services | Azure DevOps Server 2022 | Azure DevOps Server 2020. Runtime parameters let you have more control over what values can be passed to a pipeline. With runtime parameters you can: Supply different values to scripts and tasks at runtime. Control parameter types, ranges allowed, and defaults.

How do I add parameters to Azure DevOps pipeline?

Option 1: Create a pipeline parameter in the settings panel Next to the name of your pipeline draft, select the gear icon to open the Settings panel. In the Pipeline parameters section, select the + icon. Enter a name for the parameter and a default value.


1 Answers

I think that this may answer for the question:

Use template expressions to specify how values are dynamically resolved during pipeline initialization. Wrap your template expression inside this syntax: ${{ }}.

Template expressions can expand template parameters, and also variables. You can use parameters to influence how a template is expanded. The parameters object works like the variables object in an expression. Only predefined variables can be used in template expressions.

Expressions are only expanded for stages, jobs, steps, and containers (inside resources). You cannot, for example, use an expression inside trigger or a resource like repositories. Additionally, on Azure DevOps 2020 RTW, you can't use template expressions inside containers.

like image 134
Krzysztof Madej Avatar answered Jan 04 '23 13:01

Krzysztof Madej