2

The documentation of the Bitbucket Cloud API specifies how to add an SSH key to a specific user.

However, the Bitbucket website also allows to add several SSH "Access keys" (a.k.a. Deployment keys) to a repository, enabling read-only access to the key holders.

Is it possible to add the access keys via the Bitbucket Cloud API?

Paolo Brandoli
  • 4,681
  • 26
  • 38

1 Answers1

0

Deploy keys are managed through their own API endpoint:

/repositories/{workspace}/{repo_slug}/deploy-keys

Just POST a JSON payload to that URL that contains the key and label, e.g.

{
  "key": "ssh-ed25519 AAAAc3z...",
  "label": "my key"
}

If you generate an App Password, then you can add a deploy key to any repo that you have admin access to as follows (separate shell vars for readability):

BB_WORKSPACE="myworkspace"
BB_USER="myusername"
BB_PASS="oKoungoe7morerandomcharsae6PeewooFee"
BB_REPO="myrepo"
BB_KEY="ssh-ed25519 AAAAC3zNaCClZDImorerandomcharsTy0AC0YGKAMtZ+/Zk5teMaBvyrj3gtL6sFDdQQ"

# via curl...
curl -u "$BB_USER:$BB_PASS" \
  -H "Content-Type: application/json" \
  -d '{"key":"'"$BB_KEY"'", "label":"mykey@myserver"}' \
  https://api.bitbucket.org/2.0/repositories/$BB_WORKSPACE/$BB_REPO/deploy-keys

# via httpie...
http -a $BB_USER:$BB_PASS \
  https://api.bitbucket.org/2.0/repositories/$BB_WORKSPACE/$BB_REPO/deploy-keys \
  key="$BB_KEY" \
  label="mykey@myserver"
Drew
  • 195
  • 3
  • 7