How to clone all project repositories from BitBucket with one script


Author: Artyom Tsvirko Published on: June 28, 2020

If you’re a new developer in the company or start development on a new laptop from scratch, you may wander how to pull all the BitBucket project repos to your local computer easily.

Well, what if I say it’s possible to do that by running just one script?

Interested? - Then follow these steps:

  1. Navigate to the account settings on Bitbucket

  1. Create new app password with READ Project permissions.
  2. Open Terminal and create a new shell script with exec permissions.
    touch clone_bitbucket_repositories.sh
    chmod +x clone_bitbucket_repositories.sh
    nano clone_bitbucket_repositories.sh
    
  3. Paste script. Insert your username, previously generated password (line 4&5) and project key(line 3). Save file, exit.
    #!/bin/bash
    
    BITBUCKET_API="https://api.bitbucket.org/2.0/repositories/cldrizon?q=project.key=\"PROJECTREACTOR\"&pagelen=100"
    BITBUCKET_USERNAME="your_bitbucket_username"
    BITBUCKET_PASSWORD="your_bitbucket_password"
    
    # Get the list of repositories from the Bitbucket API
    repositories=$(curl -s -u "$BITBUCKET_USERNAME:$BITBUCKET_PASSWORD" "$BITBUCKET_API" | jq -r '.values[] | .links.clone[] | select(.name == "ssh") | .href')
    
    # Clone each repository
    for repo_url in $repositories; do
        git clone "$repo_url"
    done
    
  4. Execute script.
     ./clone_bitbucket_repositories.sh
    
  5. Go grab some coffee while it’s downloading the Internet ;)

NOTES:

The script implies that you have the appropriate programs:

  • curl
  • jq
  • git (with ssh keys setup)

This script is for Unix system users. If you’re a windows user, feel free to adjust the script for yourself.

This script is using Bitbucket Cloud API.

You can find project keys by navigating to https://bitbucket.org/{WORKSPACE_NAME}/workspace/projects/ and selecting key from the projects grid.