ARTIFACTORY: How to retrieve all artifacts in a repo except the latest 2 versions
This article will provide a solution for searching all artifacts in a repo except the latest 2 versions based on their names, irrespective of the created date and download history. For example, using this method, I can retrieve the RPM packages
rpm-4.18.2-3-omv2390.x86_64.rpm
rpm-4.18.3-3-omv2390.x86_64.rpm
Which are older versions.
This solution can be applied to maven, npm, nuget, debian, rpm, python, gems.
Some tools that will be used:1. JFrog CLI to look up artifact folders in Artifactory. Learn more about how to download and install JFrog CLI.
2. File Spec to define specific criteria. Learn more about File Spec.
3. AQL documentation to create a schema.
Steps:1. Create a File Spec and use AQL to specify the repo and path to the targeted artifacts. All artifacts under the listed repo/path will be sorted in descending order by their names, and the last 2 artifacts will be excluded. If one looks for all artifacts in multiple folders which share the same directory structure, one can include the depth in the AQL query.
Below is an example:
{
"files": [
{
"aql": {
"items.find": {
"repo": "$repoName",
"path": "path/to/artifacts",
"type": "folder"
}
},
"sortBy": ["name"],
"sortOrder": "desc",
"offset": 2
}
]
}
Please note that the offset value can be changed if you are looking to exclude more versions.
2. Run JFrog CLI and pass the File Spec that was created. It will return a list of targeted artifacts that met the requirements:
jf rt s --spec=fileSpec.json