Last active
January 20, 2024 13:34
-
-
Save jihchi/05a51eba148c31d388c4 to your computer and use it in GitHub Desktop.
Add new deployment key to BitBucket via API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
KEY=public-key | |
LABEL=key123 | |
### | |
# Method 1. Authenticate by your account & password | |
### | |
AUTH=user:password | |
curl -X POST --user $AUTH \ | |
https://api.bitbucket.org/1.0/repositories/plaxieappier/cmpboard/deploy-keys \ | |
--data "key=$KEY&label=$LABEL" | |
### | |
# Method 2. Authenticate by OAuth2 (Google) | |
# | |
# ref. https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html#OAuthonBitbucketCloud-OAuth2.0 | |
### | |
CLIENT_ID=client-id # As well as "Key" in OAuth consumers | |
SECRET=secret | |
# Step 1. Go to following URL to get the "code" | |
# https://bitbucket.org/site/oauth2/authorize?client_id=$CLIENT_ID&response_type=code | |
# Then, Copy the "code" value from URL query string | |
$CODE=from-browser-url | |
# Step 2. Get the access token | |
curl -X POST -u "$CLIENT_ID:$SECRET" \ | |
https://bitbucket.org/site/oauth2/access_token \ | |
-d grant_type=authorization_code -d code=$CODE | |
# API will returns an JSON object including "access_token", for example: | |
# { | |
# "access_token": "...Z3O1OXk...", | |
# "scopes": "repository:write", | |
# "expires_in": 3600, | |
# "refresh_token": "...ySRtPV...", | |
# "token_type": "bearer" | |
# } | |
ACCESS_TOKEN=from-step2-response | |
# Step 3. Request API with access token | |
curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \ | |
https://api.bitbucket.org/1.0/repositories/plaxieappier/cmpboard/deploy-keys \ | |
--data "key=$KEY&label=$LABEL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTICE: A public key contains characters need to be escaped before sending it as a
POST data
( urlencode )BitBucket API document: https://confluence.atlassian.com/bitbucket/deploy-keys-resource-296095243.html#deploy-keysResource-POSTanewkey