Search code examples
jenkinspropertiesartifactoryartifactory-query-lang

Artifactory and Jenkins - get file with newest/biggest custom property


I have generic repository "my_repo". I uploaded files there from jenkins with to paths like my_repo/branch_buildNumber/package.tar.gz and with custom property "tag" like "1.9.0","1.10.0" etc. I want to get item/file with latest/newest tag.

I tried to modify Example 2 from this link ... https://www.jfrog.com/confluence/display/JFROG/Using+File+Specs#UsingFileSpecs-Examples

... and add sorting and limit the way it was done here ... https://www.jfrog.com/confluence/display/JFROG/Artifactory+Query+Language#ArtifactoryQueryLanguage-limitDisplayLimitsandPagination

But im getting "unknown property desc" error.

jenkins code


Solution

  • After a lot of testing and experimenting i found that there are many ways of solving my main problem (getting latest version of package) but each of way require some function which is available in paid version. Like sort() in AQL or [RELEASE] in REST API. But i found that i still can get JSON with a full list of files and its properties. I can also download each single file. This led me to solution with simple python script. I can't publish whole but only the core which should bu fairly obvious

    import requests, argparse
    from packaging import version
    
    ...
    
    query="""
    items.find({
      "type" : "file",
      "$and":[{
        "repo" : {"$match" : \"""" + args.repository +  """\"}, 
        "path" : {"$match" :  \"""" + args.path +  """\"} 
        }]
    }).include("name","repo","path","size","property.*")
    """       
    auth=(args.username,args.password) 
    
    
    def clearVersion(ver: str):
        new = ''
        for letter in ver:
            if letter.isnumeric() or letter == ".":
                new+=letter
        return new
    
    def lastestArtifact(response: requests):
        response = response.json()
        latestVer = "0.0.0"
        currentItemIndex = 0
        chosenItemIndex = 0
        for results in response["results"]:
            for prop in results['properties']:
                if prop["key"] == "tag":
                    if version.parse(clearVersion(prop["value"])) > version.parse(clearVersion(latestVer)):
                        latestVer = prop["value"]
                        chosenItemIndex = currentItemIndex 
            currentItemIndex += 1
        return response["results"][chosenItemIndex]
    
    req = requests.post(url,data=query,auth=auth)
    if args.verbose:
        print(req.text)
    
    latest = lastestArtifact(req)
    ...
    

    I just want to point that THIS IS NOT permanent solution. We just didnt want to buy license yet only because of one single problem. But if there will be more of such problems then we definetly buy PRO subscription.