How to get url when searching using the artifactory jfrog cli

4.4k views Asked by At

The jfrog cli for Artifactory can be used for searching for artifacts across multiple servers configured in ~/.jfrog/jfrog-cli.conf:

jfrog rt s repo_name/path/to/artifact*

The URL in the result is only the part relative to the server base URL, and does not contain any reference to the server where the artifact was found:

[
    {
        "path": "repo_name/path/to/artifact.tar"
    }
]

I know could traverse the list of servers in the config file, or use the REST API, but I would prefer if the cli could return it. I haven't found any option to tell jfrogto include the server URL in the result, so it looks like this is not possible. Hopefully I am wrong.

The URL is to be sent in a downstream event to other components which have no clue what an ARM is.

Sample jfrog-cli.conf

{
  "artifactory": [
    {
      "url": "https://arm1.foo.bar/artifactory/",
      "apiKey": "AKEY",
      "serverId": "1",
      "isDefault": true
    },
    {
      "url": "https://arm2.foo.bar/artifactory/",
      "apiKey": "ANOTHERKEY",
      "serverId": "2",
      "isDefault": false
    }
  ],
  "Version": "1"
}
1

There are 1 answers

0
Gunnar On

The jfrog cli does not search across the list of configured servers. Instead the --server-id option for jfrog rt s should be used, or jfrog rt use <server id> used to set the default server, see https://www.jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory#CLIforJFrogArtifactory-UsingaPredefinedArtifactoryServer.

You can configure multiple Artifactory instances using the config command. The 'use' command is used for specifying which of the configured Artifactory instances should be used for the following CLI commands.

$ jfrog rt use artifactory-server-1

This updates the isDefault setting to true for the given server, and false for the rest. I would not recommend using this way in scripts, since there will be interference if more than one is executing at a time.

The servers should be looped over one by one, and the server URL picked from the jfrog-cli.conf JSON, or using the jfrog rt c show <server id> command. Some python code:

import json
import os
from subprocess import check_output

def find_one_artifact(pattern):
    # Make jfrog less talkative so that JSON parsing works
    os.environ['JFROG_CLI_LOG_LEVEL'] = 'ERROR'
    with open(os.getenv('HOME') + '/.jfrog/jfrog-cli.conf') as fp:
        conf = json.load(fp)
        for server in conf['artifactory']:
            output = check_output(['jfrog', 'rt', 's', '--server-id',
                                   server['serverId'], pattern])
            hits = json.loads(output)
            if hits and 'errors' not in hits:
                for hit in hits:
                    return server['url'] + '/' + hit['path']
    return None