How to clone all projects of a group at once in GitLab?

后端 未结 15 1501
[愿得一人]
[愿得一人] 2020-12-23 02:02

In my GitLab repository, I have a group with 20 projects. I want to clone all projects at once. Is that possible?

15条回答
  •  再見小時候
    2020-12-23 02:44

    In response to @Kosrat D. Ahmad as I had the same issue (with nested subgroups - mine actually went as much as 5 deep!)

    #!/bin/bash
    URL="https://mygitlaburl/api/v4"
    TOKEN="mytoken"
    
    function check_subgroup {
    echo "checking $gid"
    if [[ $(curl --header "PRIVATE-TOKEN: $TOKEN" $URL/groups/$gid/subgroups/ | jq .[].id -r) != "" ]]; then
      for gid in $(curl --header "PRIVATE-TOKEN: $TOKEN" $URL/groups/$gid/subgroups/ | jq .[].id -r)
      do
        check_subgroup
      done
    else
      echo $gid >> top_level
    fi
    }
    
    > top_level #empty file
    > repos #empty file
    for gid in $(curl --header "PRIVATE-TOKEN: $TOKEN" $URL/groups/ | jq .[].id -r)
    do
      check_subgroup
    done
    # This is necessary because there will be duplicates if each group has multiple nested groups. I'm sure there's a more elegant way to do this though!
    for gid in $(sort top_level | uniq)
    do
      curl --header "PRIVATE-TOKEN: $TOKEN" $URL/groups/$gid | jq .projects[].http_url_to_repo -r >> repos
    done
    
    while read repo; do
      git clone $repo
    done 

    Note: I use jq .projects[].http_url_to_repo this can be replaced with .ssh_url_to_repo if you'd prefer.

    Alternatively strip out the rm's and look at the files individually to check the output etc.

    Admittedly this will clone everything, but you can tweak it however you want.

    Resources: https://docs.gitlab.com/ee/api/groups.html#list-a-groups-subgroups

提交回复
热议问题