jfrog cli retrieve metadata - artifactory

I have a job which uses the jfrog cli to access artifactory and pull down a resource. eg:
jfrog rt c shared01 --url="xxx" --user=xxx --password=xxx
jfrog rt dl --server-id shared01 --flat true "source/file" "./destfile"
This resource has some properties that I would like to retrieve. I can see from the documentation that you can retrieve where properties match XYZ, but I would just like to retrieve the metadata (we are storing the "product" version here).
Can anyone tell me if this is achievable?

The Search command seems to return the property metadata, at least on version 1.50.2
jfrog rt s --server-id shared01 "source/file"
outputs something like
[Info] Searching artifacts...
[Info] Found 1 artifact.
[
{
"path": "source/file",
"type": "file",
"size": 12345,
"created": "2021-09-08T19:56:21.314Z",
"modified": "2021-09-08T19:56:21.063Z",
"sha1": "ffffffffffffffffffffffffffffffffffffffff",
"md5": "ffffffffffffffffffffffffffffffff",
"props": {
"someprop": [
"value"
],
"anotherprop": [
"anothervalue"
]
}
}
]

The CLI doesn't have this capability build in. You can use a REST API to get artifact's properties.
Another possibility(which I haven't tried, but seems valid) is to use the CLI Using File Specs.
With File Specs you can search using AQL, where you can ask directly for the properties and filter per your needs.
Good Luck!

Related

Artifactory old artifacts clean up (CLI + AQL)

I'm struggling to find a solution to clean up all RPM repositories from Artifactory using AQL and CLI. I would like for example to delete everything which was not downloaded in the last 12 months (also if the creation date is older than 1 year and it was never downloaded). The hardest part in my opinion is that I also need to keep the last x number of RPM files in each repository (for example the last 3 rpm files which were last downloaded). Any idea how to perform this task or if it is possible at all, especially the part with keeping the last downloads. Any suggestion is appreciated.
Thank you,
Dany
Clean up old artifacts using jfrog CLI and AQL
JFrog CLI is a compact and smart client that provides a simple interface to automate access to Artifactory. As a wrapper to the REST API.
Installing JFrog CLI
On MAC
brew install jfrog-cli-go
With Curl
curl -fL https://getcli.jfrog.io | sh
JFrog CLI syntax
jfrog target command-name global-options command-options arguments
Target - product on which you wish to execute the command:
rt: JFrog Artifactory
bt: JFrog Bintray
mc: JFrog Mission Control
xr: JFrog Xray
Configure JFrog cli
jfrog rt c Artifactory --url=https://artifactory.eqs.intra/artifactory --apikey=<add api key> #can generate api key from user profile
Generate API Key
Login to artifactory -> Click on username and edit profile -> under Authentication Settings
JFrog CLI configuration Official Documentation
Search for Artifacts
display artifacts from
- registry - docker
- path - *
- download status = null # Not downloaded
- created before 1 month
{
"files": [
{
"aql": {
"items.find": {
"repo": {"$eq":"docker"},
"path": {"$match":"*"},
"name": {"$match":"*"},
"stat.downloads":{"$eq":null},
"$or": [
{
"$and": [
{
"created": { "$before":"1mo" }
}
]
}
]
}
}
}
]
}
AQL Official Documentation.
Run a search and verify the packages you are about to delete
jfrog rt s --spec artifactory.spec
Delete artifacts as per the spec file
jfrog rt del --spec artifactory.spec
Insight Page

Artifactory AQL in jfrog rt search: include fields

Related: Artifactory aql: find builds of job with given property
As decribed in the blog, I want to query Artifactory with this AQL, using Jfrog CLI:
items.find(
{
"repo":"snapshot-local",
"artifact.module.build.name":"foo",
"artifact.item.#vcs.Revision":"aabbccddee123456"
}
).include("artifact.module.build.number")
My understanding of the file specs is that it should be along those lines:
{
"files":
[
{
"aql":{
"items.find":{
"repo":"snapshot-local",
"artifact.module.build.name":"foo",
"artifact.item.#vcs.Revision":"aabbccddee123456"
}
}
}
]
}
However, I am not sure how to request the artifact.module.build.number property.
How can I get the same behavior as with cURL using .include("artifact.module.build.number") in the request ?
Today the CLI's AQL support does not permit modifying the schema of the object returned.
This means you can not modify the "include" and add fields from a different domain.
I would therefore (in your case) use curl (as you suggested).
Something like:
items.find({
"repo":"snapshot-local",
"artifact.module.build.name":"foo",
"artifact.item.#vcs.Revision":"aabbccddee123456"
}).include("artifact.module.build.name","artifact.module.build.number")

Artifactory CLI - Jfrog - How to get binary Hash code (SHA1, SHA256) through jfrog CLI

Is there a way to get the binary HASH code(SHA1, SHA256) from artifactory through jfrog cli?
Or at least to download only binaries with specific HASH.
I tried to use the props like below, but it does not work.
jfrog rt download --props "sha1=506438fbfc4a029ffee8b001fdce3c2cbd5541ec --server-id af-server.com afrepo/test_repo/test.txt
"
an Artifact sha-1 is not a property but but an item.field.
You can therefore query an artifact by sha1 using the field name (it is actually "actual_sha1") using AQL.
You can use AQL with curl (see some examples here) or with the CLI using spec files (examples here)
(Note that the AQL structure is slightly different between spec files and curl)
With all that said, your sha1 search example would look like this.
CLI command:
jfrog rt s --spec="/MyPath/MySpecFile"
Spec file
{ "files": [
{
"aql": {
"items.find":
{
"repo": "my-local-repo",
"actual_sha1": {"$eq": "6aebc7357ba46916aab5e9d29b3f8e7180cd7089"}
}
}
}]}

How can I download last artifact in Artifactory?

I have some artifacts in Artifactory (OpenSource):
I can download an artifact from using jfrog CLI:
jfrog rt config --user=admin --password=**** --url=http://foo:8081/artifactory
jfrog rt download testproject/01_Develop/01_CI/HPCC-Package-70.zip --flat=true
How can I download the LATEST(highest number) artifact?
JFrog CLI recently started supporting 3 new options from many of the Artifactory commands: --sort-by --sort-order and --limit.
You can use these options to search, download, copy, move or delete the latest artifact created in Artifactory. For example, to download the latest file uploaded to the all-my-frogs folder in the my-local-repo repository, use the following command:
jfrog rt dl "my-local-repo/all-my-frogs/" --sort-by=created
--sort-order=desc --limit=1
You can use the JFrog Cli search command.
jfrog rt s "testproject/01_Develop/01_CI/HPCC-Package-*.zip"
The search command will return a list of paths which you can then sort using external tools such as jq.
If you wish to do it from a Jenkins groovy file, you could use:
def downloadSpec = """{
"files": [
{
"pattern": pattern,
"target": "",
"flat" : "true",
"sortBy": ["name"],
"sortOrder": "desc",
"limit": 1
}
]
}"""

retrieve artifact with maven timestamp from artifactory

Is there a way to retrieve an artifact with the maven timestamp as it was originally uploaded by maven?
from jenkins logs:
Uploading: http://artifactory.foo/artifactory/libs-snapshot-local/com/foo/foo-web-service/1.16.0-SNAPSHOT/foo-web-service-1.16.0-20160504.182015-2.tar.gz
Results from artifactory REST api:
$ curl -X GET 'http://artifactory.foo/artifactory/api/search/gavc?g=com.foo&a=foo-web-service&v=1.16.0-SNAPSHOT&c=*&repos=libs-snapshot-local'
{
"results" : [ {
"uri" : "http://artifactory.foo/artifactory/api/storage/libs-snapshot-local/com/foo/foo-web-service/1.16.0-SNAPSHOT/foo-web-service-1.16.0-SNAPSHOT-sources.jar"
}, {
"uri" : "http://artifactory.foo/artifactory/api/storage/libs-snapshot-local/com/foo/foo-web-service/1.16.0-SNAPSHOT/foo-web-service-1.16.0-SNAPSHOT.pom"
}, {
"uri" : "http://artifactory.foo/artifactory/api/storage/libs-snapshot-local/com/foo/foo-web-service/1.16.0-SNAPSHOT/foo-web-service-1.16.0-SNAPSHOT.tar.gz"
}, {
"uri" : "http://artifactory.foo/artifactory/api/storage/libs-snapshot-local/com/foo/foo-web-service/1.16.0-SNAPSHOT/foo-web-service-1.16.0-SNAPSHOT.war"
} ]
}
I'd like to get the the same name as it was uploaded to via a wget or equivalent...
What I want to acheive:
jenkins uploads foo-web-service-1.16.0-20160504.182015-2.tar.gz to libs-snapshot-local
query REST api to get latest artifact link that includes the timestamps in the name with parameters a=foo-web-service&version=1.16.0&...
wget $artifact_link_with_timestamp
What I currently acheive that does not satisfy my need:
jenkins uploads foo-web-service-1.16.0-20160504.182015-2.tar.gz to libs-snapshot-local
query REST api via gavc search with parameters a=foo-web-service&version=1.16.0&...
wget $artifact_link
Conclusion as stated in the accepted answer, the problem was in the artifactory config itself. To achieve what I wanted, I needed the snapshots to be unique.
As long as your repository is configured to use unique snapshots (or to use client snapshot policy and you use Maven 3 and up), you can always use the Maven timestamp as a version. Replacing it with -SNAPSHOT is a "runtime" trick to make the resolution easier.
If your repository is configured to use non-unique snapshots, the files are actually stored with -SNAPSHOT instead of version and override previous snapshots (don't do that).

Resources