Automat-ITip - When you want to migrate your project's code to a new SCM using GIT...




AutomatTip:

When you want to migrate your projects code to new SCM using GIT you can use the next script template,

Read the instructions and change the parameters to yours.

Make sure you have a project ready to get all those repos on your SCM.

Enjoy,

#!/bin/bash 

#############################################################

# Create a folder containing the next structure (2 files and one folder)              

# All the repositories you want to upload to bitbucket sould ne in the 'repo_folder'  

#                                                                                     

# |_ root folder                                                                      

#   |_ .gitignore                                                                     

#   |_ Move_To_Bitbucket.sh                                                           

#     |_ repo_folder                                                                  

#       |_repo_1                                                                      

#       |_repo_2                                                                      

#                                                                                    

#############################################################

username=         [YourAdminUsername]

password=          [YourAdminPassword]

bitbucket_url=    [YourBitbucketURL]/rest/api/1.0/projects/

bitbucket_server= [BitbucketServerHostName]

ssh_port=         [SSHPort]

project_key=      [TargetProjectKey]

repo_folder=      "repo_folder"

echo -e "Target Project is:" $project_key

repos=(`ls $repo_folder | cut -d '.' -f1`) 

read -p "Are you sure? (n/Y) " -n 1 -r 

if [[ ! $REPLY =~ ^[Yy]$ ]] 

then 

    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1

fi 

for i in "${repos[@]}"

do

    echo "Repo Name:" $i

 curl -u $username:$password -H "Content-Type: application/json" -X POST -d '{"name": "'$i'","scmId": "git","forkable": true}' $bitbucket_url/$project_key/repos -k

 sleep 1

 cp .gitignore $repo_folder//$i #Remark if you don't have .gitignore file

 cd "$repo_folder//$i"

 git init 

 git remote add origin ssh://git@$bitbucket_server:$ssh_port/$project_key/$i

 git checkout -b init

 git add .

 git commit -am "Initial commit"

 git push --set-upstream origin init

 cd ../..

 echo

done


For more DevOps tips, to learn, share, and socialize join Automat-IT DevOps Experts Forum: https://t.me/joinchat/E61TXg-lkaNusP9FdqtxMg