如何使用 CURL 列出工件存储库中包含超过 10 个工件的所有文件夹?

问题描述 投票:0回答:1

我有一个伪影问题。 我需要实施保留策略,每个文件夹仅保留 10 个工件。 我需要帮助。

这是我最初的curl命令,用于获取计数> 10的所有文件夹,

execute_aql() {
    # Execute AQL query to find folders with more than 10 artifacts
    curl_response=$(curl -s -u "admin:$API_KEY" -X POST "$ARTIFACTORY_URL/artifactory/api/search/aql" -H "Content-Type: text/plain" -d 'items.find({"type": "folder", "repo": "'"$REPO_NAME"'"}).include("name","children.items.name","children.items.@count").filter(function(item){ return item.children.items.@count > 10 })')

    # Print the response
    echo "$curl_response"
}

但它没有按预期工作,事实上它失败了:

+ ./retPolicy.sh
Failed to parse query: items.find({"type": "folder", "repo": "HLRP_GENERIC_DEV"}).include("name","children.items.name","children.items.@count").filter(function(item){ return item.children.items.@count > 10 }), it looks like there is syntax error near the following sub-query: children.items.name","children.items.@count").filter(function(item){ return item.children.items.@count > 10 })

我不知道为什么!

我想在其之上添加一个排序算法,按时间排序,并删除最旧的工件,但我没有走那么远!

希望大家能帮帮我!

感谢您阅读本文!

bash jenkins curl artifactory
1个回答
0
投票

AQL 非常强大,但它不是 JavaScript,不支持您尝试使用的语法。

为了实现您的目标,您可以按下述方式使用 AQL。请注意 -

  1. 它会给 Artifactory 带来一些负载,因此最好限制执行速度
  2. 您可以使用 JFrog CLI 而不是使用
    curl
    ,这使得它更简单,并且还提供使用 AQL 删除文件的功能(请参阅下面的注释)

步骤:

  1. 查找文件夹
  2. 在文件夹中查找文件
  3. 删除文件

第 1 步 - 查找文件夹

给定一个存储库,查找存储库中的所有文件夹:

items.find({"repo":"my-repo","type":"folder"}).include("repo","path","name")

结果:

{
  "results" : [ {
      "repo" : "my-repo",
      "path" : "some/path",
      "name" : "foo"
    }, ... 
  ],
  "range" : { ... }
}

注释-

  • 最好避免搜索所有文件夹并通过路径或路径模式匹配(即以以下开头)限制搜索。
  • 您可以在多个存储库中搜索文件夹(例如使用
    "repo":{"$match":"*"}
    ),但一般来说,最好将搜索重点放在精确匹配(即相等)上。

第 2 步 - 查找要从每个文件夹中删除的文件

对于步骤 1 中的每个文件夹 - 找到文件夹中的文件,按

created
时间降序排序(可以改为按
modified
排序)。

items.find({"repo":"my-repo","path":"some/path/foo","type":"file"}).include("repo","path","name","created").sort({"$desc":["created"]})

结果:

{
  "results" : [ {
      "repo" : "my-repo",
      "path" : "some/path/foo",
      "name" : "bar.txt",
      "created" : "2023-09-04T10:40:58.737Z"
    }, {
      "repo" : "my-repo",
      "path" : "some/path/foo",
      "name" : "baz.txt",
      "created" : "2023-09-04T10:35:43.274Z"
    }, ... ],
  "range" : { ... }
}

第 3 步 - 删除文件

对于每个文件夹 - 保留前 10 个文件,其他所有文件都可以删除。

JFrog CLI 使用注意事项

使用 JFrog CLI,您可以将上述步骤更改为 -

  1. 使用 cli 执行相同操作
  2. 搜索按时间排序的文件,但限制为前 10 个结果
  3. 使用 AQL 删除第 10 个文件之前创建的所有文件(文件夹中)

请参阅博客使用 JFrog CLI 和 AQL 的高效质量工件清理方法

© www.soinside.com 2019 - 2024. All rights reserved.