简体   繁体   中英

Compare two versions in logic apps

I have two version strings and I need to compare them like the .NET Version type can (with awareness of Major, Minor versions, instead of just as strings) to see which one is newer. My question here is: How can I check which version string is newer in logic apps?

Is there any nicer way than the 'brute force' approach of string manipulation?

major = split(variables('CurrentImageVer', '.'))[0]
minor = split(variables('CurrentImageVer', '.'))[1]

and so on...

I agree with @Skin, Since the versions themselves are of string type, this can be done through string manipulation. Below is something that worked for me after reproducing from my end.

For demonstration purposes I have used the below 2 versions in an array variable.

[
  "1.2.31",
  "1.2.30"
]
  • Firstly, I have tried to split both the versions from the array.

    在此处输入图像描述

  • Then I have used Condition connector to check if version 1 is greater or not than the other.

    在此处输入图像描述在此处输入图像描述

RESULTS:

在此处输入图像描述

Below is the complete JSON of my logic app.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@variables('Latest Version')",
                "runAfter": {
                    "Until": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Flag": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Flag",
                            "type": "integer",
                            "value": 0
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Versions",
                            "type": "array",
                            "value": [
                                "1.2.31",
                                "1.2.30"
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Latest_Version": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Latest Version",
                            "type": "string",
                            "value": "Latest Not Found"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Split_Version1": {
                "inputs": "@split(variables('Versions')[0],'.')",
                "runAfter": {
                    "Latest_Version": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Split_Version2": {
                "inputs": "@split(variables('Versions')[1],'.')",
                "runAfter": {
                    "Flag": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Until": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Set_variable_2": {
                                "inputs": {
                                    "name": "Latest Version",
                                    "value": "@{variables('Versions')[0]} is the latest version"
                                },
                                "runAfter": {},
                                "type": "SetVariable"
                            }
                        },
                        "else": {
                            "actions": {
                                "Condition_2": {
                                    "actions": {
                                        "Set_variable": {
                                            "inputs": {
                                                "name": "Latest Version",
                                                "value": "@{variables('Versions')[1]} is the latest version"
                                            },
                                            "runAfter": {},
                                            "type": "SetVariable"
                                        }
                                    },
                                    "expression": {
                                        "and": [
                                            {
                                                "less": [
                                                    "@outputs('Split_Version1')[variables('Flag')]",
                                                    "@outputs('Split_Version2')[variables('Flag')]"
                                                ]
                                            },
                                            {
                                                "equals": [
                                                    "@variables('Latest Version')",
                                                    "Latest Not Found"
                                                ]
                                            }
                                        ]
                                    },
                                    "runAfter": {},
                                    "type": "If"
                                }
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "greater": [
                                        "@outputs('Split_Version1')[variables('Flag')]",
                                        "@outputs('Split_Version2')[variables('Flag')]"
                                    ]
                                },
                                {
                                    "equals": [
                                        "@variables('Latest Version')",
                                        "Latest Not Found"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    },
                    "Increment_variable": {
                        "inputs": {
                            "name": "Flag",
                            "value": 1
                        },
                        "runAfter": {
                            "Condition": [
                                "Succeeded"
                            ]
                        },
                        "type": "IncrementVariable"
                    }
                },
                "expression": "@equals(variables('Flag'), length(outputs('Split_Version1')))",
                "limit": {
                    "count": 60,
                    "timeout": "PT1H"
                },
                "runAfter": {
                    "Split_Version1": [
                        "Succeeded"
                    ],
                    "Split_Version2": [
                        "Succeeded"
                    ]
                },
                "type": "Until"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM