Cloning another Bitbucket repository in Bitbucket Pipelines

I'll show you how to allow one Bitbucket repo to clone another during a Pipeline build.

I recently had a use-case where I wanted to clone another Bitbucket repository during a Pipelines execution. Doing this is very simple, but there is a lot of conflicting information online, so I thought I would document the steps here.

Imagine a very simple Pipeline that looks like this:

pipelines:
  default:
    - step:
        script:
          - git clone git@bitbucket.org:ayushsharma/my-submodules.git

The repository that triggers the Pipeline will need permission to clone my-submodules.

Create SSH keys for the main repository

In Bitbucket, go to the repository SSH keys page under Settings > Pipelines > SSH keys.

Cloning another Bitbucket repository in Bitbucket Pipelines - Settings > Pipelines > SSH keys
Enlarge — Cloning another Bitbucket repository in Bitbucket Pipelines - Settings > Pipelines > SSH keys

Next, click on Generate keys to let Bitbucket auto-generate a random, secure SSH key-pair. You can also upload a custom key-pair if you want.

Cloning another Bitbucket repository in Bitbucket Pipelines - Generate keys
Enlarge — Cloning another Bitbucket repository in Bitbucket Pipelines - Generate keys

Once completed, click Copy public key. We will need to paste this in the next step.

Add SSH public key in the target repository

Go to the my-submodules repository. Under Settings > General > Access keys, you should see the option to add SSH public keys to gain read-only access.

Cloning another Bitbucket repository in Bitbucket Pipelines - Settings > General > Access keys
Enlarge — Cloning another Bitbucket repository in Bitbucket Pipelines - Settings > General > Access keys

Click Add key, enter a label, and paste the public key we copied in the previous step.

Cloning another Bitbucket repository in Bitbucket Pipelines - Add key
Enlarge — Cloning another Bitbucket repository in Bitbucket Pipelines - Add key

Our main repository now has read-only permissions to clone my-submodules from within its Pipelines. After the above configuration, executing a build for the repository will show all-green.

Cloning another Bitbucket repository in Bitbucket Pipelines - Successful deployment!
Enlarge — Cloning another Bitbucket repository in Bitbucket Pipelines - Successful deployment!

Resources

BITBUCKET PIPELINES series

  1. Automating AWS Lambda deployments using Bitbucket Pipelines and Bitbucket Pipes
  2. Automating Serverless framework deployments using Bitbucket Pipelines
  3. Exporting Bitucket repositories and Pipelines with Python
  4. Automating Amazon Elastic Container (ECR) container builds using Bitbucket Pipelines
  5. Deploying git submodules in Bitbucket Pipelines
  6. Cloning another Bitbucket repository in Bitbucket Pipelines
  7. Replicating Bitbucket Pipelines on your laptop for local debugging
  8. ← previous post
    Automating Amazon Elastic Container (ECR) container builds using Bitbucket Pipelines

    next post →
    Deploying git submodules in Bitbucket Pipelines

    85TechnologyView source