首页 > 解决方案 > 我们如何使用 curl 搜索 nexus repo 标签?

问题描述

有谁知道如何在nexus repo中搜索某个标签?

我在我的 repo 中标记了一些工件和 docker 图像,我可以使用搜索找到它们: 联系搜索

我想使用命令行界面做同样的事情。

curl -u user:pwd -X GET https://nexus.acme.com/service/rest/v1/tags/testTag31

curl -u user:pwd -X GET https://nexus.acme.com/service/rest/v1/search/tags/testTag31

但它不返回任何东西。当我使用浏览器界面时,我可以标记工件。

标签: curlnexus3

解决方案


对于那些寻找这个答案的人:

curl -u user:password -v -X GET https:///service/rest/v1/search?docker.imageTag=testTag31

edit2:这是从 docker repo 中检索 docker 标签并找到绑定到该标签的所有 shas 的完整代码。然后对于找到的每个 sha,它会找到与该 sha 相关的所有工件。这是结果。如果需要,请随时更改它。我不是一个 groovy 专家,所以请随时发表评论以进行改进。

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

/*
Makes recursive call to handle the pagination
*/

def call(user, token, param, continuationToken, response, type, depth) {

 //echo ' param =>' + param + ' continuationToken =>' + continuationToken + 
' type =>'+  type + ' depth' + depth
 def curlResponse
 def parsedJson

 switch (type) {
case 1: curlResponse = sh (script: "curl -u ${user}:${token} -sb GET https://nexus.hostname/service/rest/v1/search?docker.imageTag=${param}", returnStdout: true).trim()
        break;
case 2: curlResponse = sh (script: "curl -u ${user}:${token} -sb GET https://nexus.hostname/service/rest/v1/search?docker.imageTag=${param}&continuationToken=${continuationToken}", returnStdout: true).trim()
        break;
case 3: curlResponse = sh (script: "curl -u ${user}:${token} -sb GET https://nexus.hostname/service/rest/v1/search?sha1=${param}", returnStdout: true).trim()
        break;
case 4: curlResponse = sh (script: "curl -u ${user}:${token} -sb GET https://nexus.hostname/service/rest/v1/search?sha1=${param}&continuationToken=${continuationToken}", returnStdout: true).trim()
        break;
default:
  echo 'invalid value '
  break;
 }

if (curlResponse == null) {
echo 'curlResponse is NULL'
return
 }

  parsedJson = new JsonSlurper().parseText(curlResponse)
  if(depth ==1){
      for (item in parsedJson.items){
       response.add(item.assets.checksum.sha1)
    if (parsedJson.continuationToken) {
      curl(param,parsedJson.continuationToken,response,2,1)
    }
  }
  }

  if(depth ==2){
      for (item in parsedJson.items){
        if (response.get(item.name)== null) {
          response.put(item.name,[])
        }
        response.get(item.name).add(item.version)
        if (parsedJson.continuationToken) {
          curl(param,parsedJson.continuationToken,response,4,2)
        }
      }
  }
  return response
}

 return this

调用示例:

shas = listComponentsForTag(configs.nexusRegistry.credentialsId, configs.nexusRegistry.token,params.deliveryTag,"",shas,1,1)
for(sha in shas) {
   componentsByVersion = listComponentsForTag(configs.nexusRegistry.credentialsId, configs.nexusRegistry.token, sha.getAt(0),"",componentsByVersion,3,2)
}

 if (componentsByVersion.size() == 0) {
     echo 'found no artifacts for tag provided'
 }

 echo 'found '+ componentsByVersion.size() + ' artifacts'

 componentsByVersion.each{
  echo ' Component => ' + it.key + ' versions =>' + it.value
 }

推荐阅读