I recently created repositories in a new Artifactory instance. This was a testing instance and since I dind’t work with Artifactory much before this, I created them in the web-frontend by hand.

I then wanted to get them as code so I could recreate them in the production Artifactory instance without doing it all by hand.

Since I had admin-privileges in the testing instance, I used the /repositories/configurations-endpoint to get the repositories and their configuration in json-format. I then wanted to create these repositories on the production instance.

For this I built myself this shell one-liner.

curl -s -u <USER>:<PASSWORD> "https://artifactory-test.example.com/artifactory/api/repositories/configurations" | jq .REMOTE | curl --location --request PUT -u <USER>:<PASSWORD> "https://artifactory.example.com/artifactory/api/v2/repositories/batch" --header 'Content-Type: application/json' --data @-

It first reads all repositories from the testing instance, uses jq to get the remote-repositories, then PUT these into the production-instance, using the /repositories/batch-endpoint.

This would have worked, had I got admin-privileges on the production instance, too. But sadly I didn’t!

The /repositories/configurations-endpoint can only be used as an admin and there is no endpoint to get the configurations of all repositories you have access to.

But there is an endpoint to get all the repositories without the configurations. So all I had to do is get the names of the repositories (in the json-key .key) I had access to, iterate over them and then get the configuration of each repository.

for repo in $(curl -s -u <USER>:<PASSWORD> "https://artifactory.example.com/artifactory/api/repositories/?project=ocp" | jq -r .[].key); do curl -s -u <USER>:<PASSWORD> "https://artifactory.example.com/artifactory/api/repositories/$repo" | jq .; done | jq -s '.' > repos.json

With the repos.json-file containing the configuration of all the repositories I have access to, I can now create (by PUTing them) the repositories on the production instance:

curl -X PUT -s -u <USER>:<PASSWORD> 'https://artifactory.example.com/artifactory/api/v2/repositories/batch' --header 'Content-Type: application/json' --data @repos.json

If I want to update them, I can use almost the same command, but instead I need to POST them:

curl -X POST -s -u <USER>:<PASSWORD> 'https://artifactory.example.com/artifactory/api/v2/repositories/batch' --header 'Content-Type: application/json' --data @repos.json

Next step (?) - put them into an Ansible-module.



Related posts: