'jq - find latest container image tag for a specific minor branch
crrently I am writing a small script to find out if newer images, which are defined in my docker-compose, are available on a registry.
To get a list of all available tags I use skopeo and get the following output.
{
"Repository": "registry.example:8080/my-namespace/my-app",
"Tags": [
"84.3200.0-alpha-1",
"84.3200.0-alpha-2",
"84.3200.0-alpha-3",
"84.3200.1",
"84.3300.0-alpha-1",
"84.3300.0-alpha-2",
"84.3300.1",
"84.3400.0-alpha-1",
"84.3400.0-alpha-2",
"84.3400.0-alpha-3",
"84.3400.0-alpha-4",
"84.3400.1",
"84.3400.2",
"84.3500.0-alpha-1",
"84.3500.0-alpha-2",
"84.3500.1",
"84.3500.2",
"84.3502.1",
"84.3600.1",
"84.3600.2",
"84.3600.3",
"84.3600.4",
"latest"
]
}
Now, in my docker-compose.yaml is currently the following image tag defined: image: registry.example:8080/my-namespace/my-app:84.3400.0-alpha-1. With jq I want to sort and filter the json to get the latest version of 84.3400 branch. Which is tag 84.3400.2.
There are also newer tags available, for example for the branch 84.3500.x and 84.3600.x. This images should not be used to detect the latest version until any one has updated the tag inside the docker-compose.yaml manually to one of the this branch.
Has any one a good idea how to sort and filter the json to get the latest version of the branch?
EDIT:
Hello, the solution from @inian is incomplete because the sort command sorts the characters one by one. This means that version 84.3400.2 is greater than 84.3400.12.
Does anyone have an idea how I can sort the versions correctly in ascending order?
Here is an adapted json:
{
"Repository": "registry.example:8080/my-namespace/my-app",
"Tags": [
"84.3400.1",
"84.3400.12",
"84.3400.2",
"latest"
]
}
Solution 1:[1]
You can pass the base version as a variable and sort on that. E.g. for your provided input
jq --arg base "84.3400" '.Tags | map(select(startswith($base))) | sort | last'
This works, if your versions are named as int the question. In-place of last, you could also just do reverse | first
Solution 2:[2]
Demo https://jqplay.org/s/t8dyM4tmmJ
filter:
"84.3400" as $base | .Tags | map(select(startswith($base))) | sort | last
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Logan Lee |
