cloud

JFrog Artifactory REST API in 5min

A good API is like a classic car - You want to use it again and again.
A good API is like a classic car – You want to use it again and again.

Most of the interactions with Artifactory will be from your CI/CD tools. It might be your build engine or from your log aggregator. This powerful API can be invoked in any of the standard ways you like to work with any other RESTful APIs (e.g. curl, CLI, your source code, etc’).
In many cases, it’s the preferred ‘glue’ for developers when it comes to automation. The options are extensive and you can do many useful things with this API. However, in this short post, we will cover the most popular actions you ‘must have’.

Let’s start with the most common action: “upload/download binaries“. This action could run automatically from the build machine to Artifactory using:

curl -u myUser:myP455w0rd! -X PUT "http://localhost:8081/artifactory/my-repository/my/new/artifact/directory/myAppBuild.exe" -T  ./myAppBuild.exe

And with the JFrog CLI:

jfrog rt u ./myAppBuild.exe my-repository/my/new/artifact/directory/

As for can see, you can work with the REST API from any technology you like. One good option is to use the JFrog CLI.
It is a compact client (written in Go) that provides a simple interface to automate access to Artifactory. As a wrapper to the API, it offers a way to simplify automation scripts making them more readable and easier to maintain, features such as parallel uploads and downloads, checksum optimization and wildcards/regular expressions make your scripts more efficient and reliable.

Downloading artifacts is one line of GET command:

curl -u myUser:myP455w0rd! -X GET http://localhost:8081/artifactory/libs-release-local/ch/qos/logback/logback-classic/0.9.9/logback-classic-0.9.9.jar

And with JFrog CLI:

jfrog rt dl libs-release-local/ch/qos/logback/logback-classic/0.9.9/logback-classic-0.9.9.jar

Search

You will be astonished how quickly Artifactory will be the one source of true to all your binaries and how many of them you have. If you wish to find a certain package/library or build file – There is a powerful search API.
You can do some quick searches like this one:

curl -u myUser:myP455w0rd! http://localhost:8081/artifactory/api/search/artifact?name=lib&repos=libs-release-local

OR with JFrog CLI:
jfrog rt s libs-release-local/lib

{ "results": [
  {
      "uri": "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/lib/ver/lib-ver.pom"
   },{
      "uri": "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/lib/ver2/lib-ver2.pom"
   }
 ]
}

You can also search and find interesting data points like in the example below, we wish to query all the files that came out of the build (in this case, Jenkins) and that are bigger than 100Mb.

// Put this:
items.find(
  {
     "type":"file",
     "created_by":"jenkins",
     "size":{"$gt":"100000"}
   })
.sort({"$desc":["size","name"]})
.limit(50)

// Add the query to file: aql-query-1.txt so in the next command, You can execute it.

//
// Search for the biggest files that our Jenkins built. 
// You can change it and tune the size/type of the files.
// Let's use curl to get the results
curl -uYourUserName:YourPassword -X POST 
  http://ArtifactoryUrl/artifactory/api/search/aql
  -d @aql-query-1.txt -H \"content-type: text/plain\"";

You will get a JSON response that will look like:

{
"results" : [ {
  "repo" : "docker-prod-local",
  "path" : "docker-app/65",
  "name" : "sha256__a56a07bf8a7e40e8580e26b5c7ad51e88e130cce5537bd53aec960de3e9f4e3f",
  "type" : "file",
  "size" : 182286302,
  "created" : "2019-06-10T22:59:11.432Z",
  "created_by" : "jenkins",
  "modified" : "2019-06-10T22:59:08.982Z",
  "modified_by" : "jenkins",
  "updated" : "2019-06-10T22:59:11.433Z"
},{
  "repo" : "docker-prod-local",
  "path" : "docker-framework/69",
  "name" : "sha256__a56a07bf8a7e40e8580e26b5c7ad51e88e130cce5537bd53aec960de3e9f4e3f",
  "type" : "file",
  "size" : 182286302,
  "created" : "2019-06-10T22:57:22.855Z",
  "created_by" : "jenkins",
  "modified" : "2019-06-10T22:57:19.454Z",
  "modified_by" : "jenkins",
  "updated" : "2019-06-10T22:57:22.856Z"
},{
  "repo" : "docker-stage-local",
  "path" : "docker-app/65",
  "name" : "sha256__a56a07bf8a7e40e8580e26b5c7ad51e88e130cce5537bd53aec960de3e9f4e3f",
  "type" : "file",
  "size" : 182286302,
  "created" : "2019-06-10T22:59:11.432Z",
  "created_by" : "jenkins",
  "modified" : "2019-06-10T22:59:08.982Z",
  "modified_by" : "jenkins",
  "updated" : "2019-06-10T22:59:11.433Z"
} ],
"range" : {
  "start_pos" : 0,
  "end_pos" : 3,
  "total" : 3,
  "limit" : 3
}
}

Now, you can take this information and get even more data on each docker image or any other package, library, artifact you are using.

Docker

Docker is quite popular these days, so let’s get a list of all our Docker repositories:

curl -u youUserName:youPassWord http://YouArtifactoryServer.com/artifactory/api/docker/docker-stage-local/v2/_catalog

// The response:
{
  "repositories" : [ "docker-local-apps", "docker-prod-frameworks" ]
}

Next, we can fetch the list of all the tags of specified Artifactory Docker repository:

//  GET /api/docker/{repo-key}/v2/{image name}/tags/list?n=<n from the request>&last=<last tag value from previous response>

curl -u youUserName:youPassWord http://YouArtifactoryServer.com/artifactory/api/docker/docker-prod-local/v2/docker-app/tags/list

// The response:
{
  "name" : "docker-app",
  "tags" : [ "59", "60", "61", "62", "63", "64", "65", "66", "67", "68" ]
}

The full example

<?php
/**
*
* Find all the biggest 📦 Dockers with most tags in Artifactory
* @author: Ido Green
* @date: Aug 2019
* @see: https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-aqlArtifactoryQueryLanguage(AQL)
*/
// Make sure to change these values
$outputFile = "allBiggestDockers.csv";
// Please change: user/pass and the url to your artifactory
$userPass = "-uUSER_NAME:YOUR_PASSWORD";
$artifactoryUrl = "YOUR_ARTIFACTORY_URL";
echo "🏃🏼‍♀️ Starting...";
//
// You also needs this file: aql-2.txt as it contains the query for the largest docker images sorting by size
// Something like this query should work:
// items.find(
// {
// "type":"file",
// "created_by":"jenkins",
// "size":{"$gt":"1000"}
// })
// .sort({"$desc":["size","name"]})
// .limit(50)
//
// Search for the biggest files that our jenkins built. You can change it and tune the size/type of the files.
$curlCmd = "curl {$userPass} -X POST http://{$artifactoryUrl}/artifactory/api/search/aql -d @aql-2.txt -H \"content-type: text/plain\"";
$allDockerStats = array();
exec($curlCmd, $allDockerStats, $retStatsVal);
$allDockerStr = implode(" ", $allDockerStats);
$allDockerJson = json_decode($allDockerStr);
$outputStr = "repo, path, name, size, type, tags\n";
echo $outputStr;
echo "==================================\n";
// Let's go over all the big dockers and see how many tags we have per image
foreach ($allDockerJson->results as $key => $value) {
$mbSize = round($value->size / 1024);
$outputStr .= "$key, {$value->repo} , {$value->path} , {$value->name} , {$mbSize} mb , {$value->type} , ";
$appName = preg_replace('/[0-9]+/', '', $value->path);
$appName = str_replace("/", "", $appName);
// Get all the tags per images
$curlCmdForTags = "curl {$userPass} http://{$artifactoryUrl}/artifactory/api/docker/{$value->repo}/v2/{$appName}/tags/list";
exec($curlCmdForTags, $allTags, $retVal);
$tagString = $allTags[2];
$tagString = str_replace('"tags" : ', "", $tagString);
$tagsArray = explode(',' , $tagString);
// Add that numbner of tags to our image's details
$outputStr .= count($tagsArray) . "\n";
}
// write the results to a CSV file so it will be easy to share it with G-sheet/Excel
file_put_contents($outputFile, $outputStr);
echo "😎 Done. \nPlease check $outputFile and see the results.\n";

There are many more options, but it will take (a bit) more than 5 minutes. Check it out at: Artifactory REST API doc.

Be strong!

Standard

One thought on “JFrog Artifactory REST API in 5min

  1. Pingback: Jfrog Rt Login - LoginCrunch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s