Problem with multiple conditions in the If block of ADO YAML file

Divakaran, D (Dipu) 0 Reputation points
2025-03-06T03:53:46.6533333+00:00

I am migrating a .NET Core 6.0 API into .NET 8.0 . Migration is completed locally and the features are working as expected too. Now I am facing a problem with the ADO deployment templates. We have a shared deployment template in ADO, which is consumed by both .NET 6.0 and .NET 8.0 applications. Mine is the first application which is going to Azure with .NET 8. Now coming to the topic, We have a deployment template which is responsible to publishing the Swagger.Json into the APIM. And here is the code which which is calling that particular deployment template.

We are passing BuildType as api and coreSDKVersion as 8.x from the parent of this attached template

User's image

The problem is with the both if blocks, which are not evaluated properly and the code is not choosing the both templates. We are expecting the second IF to be evaluated to true and code should run the steps inside  task-netcore-net8-api-swagger-publish

Unfortunately, that is not happening. Can someone help on it?

Thanks

DD

Azure DevOps
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Suresh Chikkam 235 Reputation points Microsoft External Staff
    2025-03-10T05:57:57.0333333+00:00

    Hello @Divakaran, D (Dipu) ,

    The issue is that startswith(parameters.coreSDKVersion, '8.') checks for an exact match at the beginning of the string. If the value is set as '8.x', '8.0', or has extra spaces, the condition doesn't match, and the pipeline skips the expected steps. Azure DevOps evaluates conditions at compile time, and if the parameter isn't set correctly in the parent template, it falls back to the default value '6.x', leading to issue. So, try using contains() instead of startswith(). It will check that any version containing '8' It get recognized, Check this corrected condition:

    - ${{ if and(eq(parameters.buildType, 'api'), contains(parameters.coreSDKVersion, '8')) }}:
      - template: task-netcore-net8-api-swagger-publish.yml
        parameters:
          artifactStagingDirectory: '{{ parameters.artifactStagingDirectory }}'
          swaggerDoc: '{{ parameters.swaggerDoc }}'
          startUpAssembly: '{{ parameters.startUpAssembly }}'
    

    To confirm that the correct value is being passed, add a debug step to print coreSDKVersion:

    - script: echo "coreSDKVersion: $(coreSDKVersion)"
      displayName: "Debug: Print coreSDKVersion"
    

    This will show the value in the logs, making it easier to verify what’s being used in the condition.

    This all things follows Azure DevOps documentation on YAML expressions and template conditions. You can check the official docs on expressions and template expressions for more details.

    Hope it helps!


    Please do not forget to click "Accept the answer” and Yes wherever the information provided helps you, this can be beneficial to other community members.
    User's image

    If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.