Search code examples
artifactoryartifactory-query-lang

Create multiple artifactory repositories from json


I want to automate the process of importing existing repositories structure from another Artifactory through .json file. So far, I have managed to make single repo from json with the following command.

curl -X PUT --insecure -u admin -H "Content-type: application/json" -T repository-config.json "https://artifactory.test.net/artifactory/api/repositories/acqbo-docker-release-local"

Is there a way to import multiple/array of repositories from a single json file and a single curl?


Solution

  • Ended up writing my own bash script for this purpose. you will have to make a file with the repositories you want to copy:

    #!/bin/bash
    
    #############
    # This script copies the repository structure from one Artifactory server to another
    # repos.list file is required to have repositories that we want to copy, each in new line.
    # user with the admin rights is necessary
    #############
    
    
    #Where to copy repos from and to
    ARTIFACTORY_SOURCE="https://source.group.net/artifactory/api/repositories/"
    ARTIFACTORY_DESTINATION="https://destination.group.net/artifactory/api/repositories/"
    
    NOLINES=$(wc -l < repos.list)   #get total nuber of lines in repos.line
    COUNTER=1                       #Set the counter to 1
    
    while [ $COUNTER -le $NOLINES ] #loops times number of lines in the repos.line
    do
            REPONAME=$(awk "NR==$COUNTER" repos.list) #get only repo name, line by line
    
            curl -GET --insecure -u admin:adminpass  "$ARTIFACTORY_SOURCE$REPONAME" > xrep.json  #Obtain data from Artifactory source, repo by repo, and writes it to the xrep.json
            curl -X PUT --insecure -u admin:adminpass -H "Content-type: application/json" -T xrep.json "$ARTIFACTORY_DESTINATION$REPONAME"   #Sends data from json to the destination Artifactory server
    
            #print in blue color
            printf "\e[1;34m$COUNTER repo done\n\e[0m"
    
    
        ((COUNTER++))
    done
        printf "\e[1;34mAll repos exported!\n\e[0m"