microsoft / azure-pipelines-yaml

Azure Pipelines YAML examples, templates, and community interaction
MIT License
1.19k stars 924 forks source link

Hi Team While running my Azure pipeline.yaml I am getting following error: directive are not supported for the expression that are embedded within the string Directive are only supported when the entire value is an expression #595

Closed svikash4 closed 1 year ago

svikash4 commented 1 year ago

This repo is for working in the open on Azure Pipelines YAML features. It's not a great place to make new feature requests, report problems, or get support. Issues you open here may sit unreviewed for a very long time. name: $(Build.BuildNumber).$(Version.Revision)$(Version.Suffix)

trigger:

pool: name: Portal21

parameters:

variables: Build.BuildNumber: rel23.3.2 Version.Revision: $[counter(variables['Build.BuildNumber'], 0)]

Set the suffix of the version number depending on whether this is master, pr, or other branch

${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master') }}: Version.Suffix: "" # master ${{ if eq(variables['Build.Reason'], 'PullRequest') }}: Version.Suffix: "-pr" # pull request ${{ if and(ne(variables['Build.SourceBranch'], 'refs/heads/master'), ne(variables['Build.Reason'], 'PullRequest')) }}: Version.Suffix: "Branch"

steps:

From the Azure Key vault when I am using if/else statment it is failling with the error as directive are not supported for the expression that are embedded within the string Directive are only supported when the entire value is an expression

DmitriiBobreshev commented 1 year ago

Hi @svikash4, thank you for the question, the reason why you are getting the error is that you trying to set if else statements in the task inputs, it's working for the Bash task because the syntax if [[ "$(environment)" == "dev" ]]; then is valid for bash.

In your case you probably need something like this(documentation):

- task: AzureKeyVault@2
  inputs:
    azureSubscription: 'MemberPortalNonProdServicePrincipalConnection'
    KeyVaultName: 'mbr-portal-common-kv'
    RunAsPreJob: true
    ${{ if eq(parameters.environment, 'dev') }}:
      SecretsFilter: dev-apim-subscription-key
    ${{ if eq(parameters.environment, 'prf') }}:
      SecretsFilter: prf-apim-subscription-key
    ${{ if eq(parameters.environment, 'stg') }}:
      SecretsFilter: stg-apim-subscription-key
    ${{ if eq(parameters.environment, 'tst') }}:
      SecretsFilter: tst-apim-subscription-key
    ${{ else }}:
      SecretsFilter: default value