Search code examples
curlnexus3

How do we search nexus repo tag using curl?


Does anyone know how to search for a certain tag in nexus repo?

I've tagged some artifacts and docker images in my repo and I'm able to find them using the search: nexus search

I would like to do the same using the command line interface.

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

but it doesn't return anything. When I use the browser interface, I can the tagged artifacts.


Solution

  • for those looking for this answer :

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

    edit2 : Here's the full code to retrieve docker tags from the docker repo and find all the shas bound to that tag. Then for each sha found, it finds all the artifacts related to that sha. Here's the result. feel free to change it if needed. I'm not a groovy expert so please feel free to comment for improvement.

    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
    

    call example :

    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
     }