2017. április 26., szerda

Downloading artifacts from Artifactory with JFrog CLI

You shall not pass! Unless you are a DevOps guy...or developer...tester...or a curious manager...so IT guy.

In case you wonder how to download some artefacts, files from the repository application called JFrog's Artifactory under Linux without using the UI, do not hesitate to read on. 

I faced the same problem when we started to use Artifactory besides Sonatpye Nexus. From Nexus you can download items with wget easily, but for Artifactory there is a command line interface based on REST with which it is pretty straightforward to download or upload anything and it has some other functionalities as well to manage your repos.

Sounds good? Let's see.

Installation of the jfrog cli tool to Linux is not so difficult.
  1. Log in with the user with which you want to use the tool, go to a directory which is good for you (like /tmp or /jfrog) and type: curl -fL https://getcli.jfrog.io | sh
  2.  Try out the tool like this : /tmp/jfrog h
Now you should see the help text of the tool. Easy, hm?

Next let's try to download something from Artifactory with this tool. When you type /tmp/jfrog rt dl URL, where URL is the path to the file in Artifactory starting with the repository name, not with http(s), then the tool will ask if you want to set up the configuration for Artifactory, like server url, username, etc. Choose yes and you will face five questions:

Server URL - this should be something like this : http://artifactory.company-name.com:8081/artifactory
Server ID - ask the Artifactory caretaker for this, he/she should know it
API key - you can set up a key, but it's easier now to hit enter
User - your Artifactory user name 
Password - straightforward

The tool will save this data and from that moment on you can download or upload anything without typing these credentials again (with your current Linux user). The files will be downloaded into the local directory.

Example : 

/tmp/jfrog rt dl --flat=true libs-releases-local/com/companyname/product/app/webapp/maven-metadata.xml

There are many options to use the tool, you can check these here.I used the --flat option so that no directory structure should be created for the downloaded file. 

How to download the latest version of a given artefact from Artifactory? I guess for many people this is an important question. 

If you use Maven for building and uploading the artefacts, you will have a maven-metadata.xml for each of your apps in Artifactory. In this xml all the versions are recorded. With some bash magic you can download the xml, get out the latest version of the application from it and then download the file itself as you already know which one to get. 

Here is an example how to download the latest rpm (we are using rpms on CentOS) of an app. It's written in one single line but if you prefer code readability and / or you like clean code, then feel free to create a nice .sh file for this. Time to practice reading some bash magic (I like bash!):

/tmp/jfrog rt dl --flat=true libs-releases-local/com/companyname/product/app/webapp/maven-metadata.xml && readlink -f maven-metadata.xml | xargs -I{} cat {} | grep release | cut -c 14- | rev | cut -c 11- | rev | xargs -I{} /tmp/jfrog rt dl --flat=true libs-releases-local/com/companyname/product/app/webapp/{}/*.rpm

If you don't use Maven, you have to figure out how to get the information which tells you what is the latest release. From that point it is easy to get the latest wit bash and jfrog CLI.

Good luck and may the force be with DevOps!