Jenkins Artifactory Plugin AQL下载最新的神器匹配模式

时间:2021-05-19 23:08:09

I'm in the process of moving my existing working series of Jenkins Jobs that are chained into a pipeline to the Modern Jenkins As Code Workflow Pipeline.

我正在将我现有的Jenkins Jobs工作系列移动到Modern Jenkins As Code Workflow Pipeline。

With this initiative comes various new plugins, that provide steps in which the Pipeline plugin can use. One of those is the Jenkins Artifactory Plugin.

通过这一举措,出现了各种新的插件,它们提供了Pipeline插件可以使用的步骤。其中一个是Jenkins Artifactory插件。

This plugin can accept a json object of Artifactorys "AQL" language, or conversely a very simple pattern to search a series of repos for Artifacts matching a pattern.

这个插件可以接受Artifactorys“AQL”语言的json对象,或者反过来一个非常简单的模式来搜索与模式匹配的Artifacts的一系列repos。

I've tried using both the "AQL" and the pattern to resolve my artifacts.

我已经尝试使用“AQL”和模式来解决我的工件。

The problem I'm having is, I would like to mimic the behavior of the existing Jenkins Artifact Resolver plugin that can be used in the Jenkins Jobs.

我遇到的问题是,我想模仿可以在Jenkins Jobs中使用的现有Jenkins Artifact Resolver插件的行为。

This plugin uses the same patterns I am current providing to the DSL workflow, and only downloads the LATEST, or LAST MODIFIED artifact in that particular return set. I would like to do the same for my new approach.

此插件使用我当前为DSL工作流程提供的相同模式,并且仅在该特定返回集中下载LATEST或LAST MODIFIED工件。我想为我的新方法做同样的事情。

Here is the result of using a patterned based search:

以下是使用基于图案的搜索的结果:

jfrog rt search "client-snapshots/com/client/content_services/search-dist/*.zip"

[Info:] Pinging Artifactory...
    [Info:] Done pinging Artifactory.
    [Info:] Searching Artifactory using AQL query: items.find({"repo": "client-snapshots","$or": [{"$and": [{"path": {"$match":"com/client/content_services/search-dist"},"name":{"$match":"*.zip"}}]},{"$and": [{"path": {"$match":"com/client/content_services/search-dist/*"},"name":{"$match":"*.zip"}}]}]}).include("name","repo","path","actual_md5","actual_sha1","size")
    [Info:] Artifactory response: 200 OK
    [Info:] Found 58 artifacts.

Here is the result of using an "AQL" query from a crafted json object

以下是使用精心设计的json对象中的“AQL”查询的结果

jfrog rt search --spec art-search.json
[Info:] Pinging Artifactory...
[Info:] Done pinging Artifactory.
[Info:] Searching Artifactory using AQL query: items.find({"repo":"client-snapshots","$and":[{"$or":[{"path":{"$match":"com/client/content_services"},"name":{"$match":"*search*"}}]},{"$or":[{"path":{"$match":"*dist*"},"name":{"$match":".zip"}}]},{"$or":[{"path":{"$match":"*1.0-SNAPSHOT*"},"name":{"$match":"*"}}]}]}).include("name","repo","path","actual_md5","actual_sha1","size")
[Info:] Artifactory response: 200 OK
[Info:] Found 116 artifacts.

And the json for the above query:

以及上面查询的json:

{
  "files": [
    {
      "aql": {
        "items.find": {
          "repo": "client-snapshots",
          "$and": [
            {
              "$or": [
                {
                  "path": {
                    "$match": "com/client/content_services"
                  },
                  "name": {
                    "$match": "*search*"
                  }
                }
              ]
            },
            {
              "$or": [
                {
                  "path": {
                    "$match": "*dist*"
                  },
                  "name": {
                    "$match": ".zip"
                  }
                }
              ]
            },
            {
              "$or": [
                {
                  "path": {
                    "$match": "*1.0-SNAPSHOT*"
                  },
                  "name": {
                    "$match": "*"
                  }
                }
              ]
            }
          ]
        }
      },
      "target": "Bazinga/Artifactory/"
    }
  ]
}

The first one returns just the zips from the repo I specified, which is what I really want. The json object returns both the poms and the zips from the repo I specified. I could do without the poms, as I'm only interested in downloading zips.

第一个只返回我指定的repo的拉链,这是我真正想要的。 json对象从我指定的repo返回poms和zip。我可以不用poms,因为我只对下载拉链感兴趣。

More to my point, I would like to just return the latest zip using one of the above patterns.

更重要的是,我想使用上述模式之一返回最新的zip。

Any suggestions will be appreciated

任何建议将不胜感激

1 个解决方案

#1


0  

So I found an alternate solution using AQL and PowerShell.

所以我找到了使用AQL和PowerShell的替代解决方案。

$pair = "$($art_user):$($art_pass)"
Write-Verbose "Attempting to convert Artifactory credentials to a base64 string for automation" 
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$headers = @{
    Authorization = $basicAuthValue
}

Write-Host "Attempting to perform a AQL search."
$aql_search = $art_base_url + "/api/search/aql"
Write-Host "Building aql query with the following parameters, groupID: $group_id, artifactID: $artifact_id, version: $version, classifier: $classifier and repos: $art_generic_repokey."
$aql_query = 'items.find({"repo":"' + $art_generic_repokey + '","$or":[{"$and":[{"path":{"$match":"' + $group_id + '/' + $artifact_id + '/' + $version + '"},"name":{"$match":"' + $artifact_id + '*' + $classifier + '*.' + $extension + '"}}]}]}).sort({"$desc":["modified"]}).limit(1)' 
Write-Host "Built the following aql query: '$aql_query' ."
$aql_content = Invoke-RestMethod -Uri $aql_search -Headers $headers -Method Post -Body $aql_query -ContentType 'text/plain'
Write-Host "Attempting to submit the aql query to the following artifactory server: $art_base_url."
$aql_results = ($aql_content).results
Write-Host "Attempting to parse query results and build the artifact download uri."
$aql_repo,$aql_path,$aql_name = ($aql_results).repo,($aql_results).path,($aql_results).name
$artifactDownloadUri = $art_base_url + '/' + $aql_repo + '/' + $aql_path + '/' + $aql_name 
Write-Host "Found the following uri: $artifactDownloadUri !!"

if ($artifactMimeType  -eq 'application/zip' -or $extension -eq 'zip') {
    Write-Verbose "Attempting to save the artifact to $download_dir/$art_dist_name.zip"
    Invoke-RestMethod -Uri $artifactDownloadUri -Headers $headers -OutFile "$download_dir/$art_dist_name.zip"
}

#1


0  

So I found an alternate solution using AQL and PowerShell.

所以我找到了使用AQL和PowerShell的替代解决方案。

$pair = "$($art_user):$($art_pass)"
Write-Verbose "Attempting to convert Artifactory credentials to a base64 string for automation" 
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$headers = @{
    Authorization = $basicAuthValue
}

Write-Host "Attempting to perform a AQL search."
$aql_search = $art_base_url + "/api/search/aql"
Write-Host "Building aql query with the following parameters, groupID: $group_id, artifactID: $artifact_id, version: $version, classifier: $classifier and repos: $art_generic_repokey."
$aql_query = 'items.find({"repo":"' + $art_generic_repokey + '","$or":[{"$and":[{"path":{"$match":"' + $group_id + '/' + $artifact_id + '/' + $version + '"},"name":{"$match":"' + $artifact_id + '*' + $classifier + '*.' + $extension + '"}}]}]}).sort({"$desc":["modified"]}).limit(1)' 
Write-Host "Built the following aql query: '$aql_query' ."
$aql_content = Invoke-RestMethod -Uri $aql_search -Headers $headers -Method Post -Body $aql_query -ContentType 'text/plain'
Write-Host "Attempting to submit the aql query to the following artifactory server: $art_base_url."
$aql_results = ($aql_content).results
Write-Host "Attempting to parse query results and build the artifact download uri."
$aql_repo,$aql_path,$aql_name = ($aql_results).repo,($aql_results).path,($aql_results).name
$artifactDownloadUri = $art_base_url + '/' + $aql_repo + '/' + $aql_path + '/' + $aql_name 
Write-Host "Found the following uri: $artifactDownloadUri !!"

if ($artifactMimeType  -eq 'application/zip' -or $extension -eq 'zip') {
    Write-Verbose "Attempting to save the artifact to $download_dir/$art_dist_name.zip"
    Invoke-RestMethod -Uri $artifactDownloadUri -Headers $headers -OutFile "$download_dir/$art_dist_name.zip"
}