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

1

7

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
Glycerite answered 20/10, 2020 at 15:57 Comment(1)
Because it doesn't work. As far as I know I'm unable to do this in the pipeline because I'm unable to generate a date at compile time, so I had to do it all in a script. I've left the question up in the hopes that someone will post a solution one day, should pipelines get better/easier to use.Glycerite
V
1

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.

Vacillate answered 20/10, 2020 at 16:47 Comment(2)
I'm not using runtime expressions here ($[ <expression> ]), I'm using compile time expressions (${{ <expression> }}). The documentation shows an example of using format with pipeline.startTime in the documentation, hence why I have an expectation of it working.Glycerite
You are right, but I think that I found a proof in docs that this is not possible. Please check my edit.Vacillate

© 2022 - 2024 — McMap. All rights reserved.