Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Creating repo in bitbucket cloud via shell script & SSH keys

weqwebsites March 22, 2020

Hi there,

 

i need assistant to understand where i am going wrong. Basically i am trying to develop a shell script that create a REPO on bitbucket cloud and then configured the same repo with git. But am facing problem in doing so. 

Below the curl command which i was using. This command was working fine and creating a new repo on the BitBucket cloud account.

curl -X POST -v -u 'XXXXXX@gmail.com':'MY_PASSWORD' -H "Content-Type: application/json" \ https://api.bitbucket.org/2.0/repositories/weqwebsites/${REPO_NAME} \ -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks" }'

 Now i have configured SSH keys with bitbucket account to use the git remote add origin command. After using ssh key the above command is not working for me. I had configured SSH so that i dont need to manually type the password and my shell script can ran independently.

Error which i am getting. 

* About to connect() to api.bitbucket.org port 443 (#0)
* Trying 18.205.93.8...
* Connected to api.bitbucket.org (18.205.93.8) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* subject: CN=*.bitbucket.org,OU=Bitbucket,O="Atlassian, Inc.",L=San Francisco,ST=California,C=US
* start date: Jun 12 00:00:00 2017 GMT
* expire date: Jun 16 12:00:00 2020 GMT
* common name: *.bitbucket.org
* issuer: CN=DigiCert SHA2 High Assurance Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US
* Server auth using Basic with user 'XXXXX@gmail.com'
> POST /2.0/repositories/weqwebsites/ HTTP/1.1
------REMOVED THE AUTH LINE FOR SECURING MY ACCOUNT------
> User-Agent: curl/7.29.0
> Host: api.bitbucket.org
> Accept: */*
> Content-Type: application/json
> Content-Length: 71
>
* upload completely sent off: 71 out of 71 bytes
< HTTP/1.1 405 Method Not Allowed
< Server: nginx
< Vary: Authorization
< Content-Type: text/html; charset=utf-8
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
< Date: Sun, 22 Mar 2020 20:02:04 GMT
< X-Served-By: app-1130
< X-Static-Version: b26e4d56649f
< X-Credential-Type: password
< X-Render-Time: 0.0173208713531
< Connection: Keep-Alive
< X-Version: b26e4d56649f
< X-Request-Count: 1216
< Allow: GET
< X-Frame-Options: SAMEORIGIN
< Content-Length: 0
<
* Connection #0 to host api.bitbucket.org left intact

 

Complete Code block in sequence of their execution given below:

curl -X POST -v -u 'XXXXXX@gmail.com':'MY_PASSWORD' -H "Content-Type: application/json" \ https://api.bitbucket.org/2.0/repositories/weqwebsites/${REPO_NAME} \ -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks" }'

git init >> ${LOG}
git add * >> ${LOG}
git commit -m 'First Commit $DATE' >> ${LOG}
git remote add origin ssh://git@bitbucket.org/weqwebsites/${REPO_NAME}.git >> ${LOG}
git push origin master >> ${LOG}

 

1 answer

1 accepted

1 vote
Answer accepted
Daniil Penkin
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 22, 2020

Hello @weqwebsites ,

Welcome to the Community!

The problem in your request is missing repository name. Note this line in the verbose curl log:

POST /2.0/repositories/weqwebsites/ HTTP/1.1

It looks like REPO_NAME environment variable is not set when your script is executed, since the endpoint to create a repository is this: 

/2.0/repositories/weqwebsites/${REPO_NAME}

Also, note that unfortunately you leaked your credentials in the verbose log (Authorization HTTP header is Base64 encoded username and password). To protect your account we've deactivated it, you should receive an email from our Support team with instructions on how to restore your account.

Hope this helps. Let me know if you have any questions.

Cheers,
Daniil

weqwebsites March 22, 2020

Hi Daniil, Thanks for securing my account. I didnt knew that verbose log revealed my account details.

 

Related to setting of the REPO_NAME environment variable, i printed the variable a line before that and it had proper name value in it.

The curl command give an error, but the line add origin execute properly and tries to find the REPO_NAME repository

git remote add origin ssh://git@bitbucket.org/weqwebsites/${REPO_NAME}.git >> ${LOG}
Daniil Penkin
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 22, 2020

So I can see one recent successful attempt to create a repository using curl in your account. Three previous requests within last 24 hours are missing repository path segment.

I can only think of something with the env variable substitution. Can you try calling it like this?

curl -X POST ... "https://api.bitbucket.org/2.0/repositories/weqwebsites/"${REPO_NAME} ...

Can you also print your variable like this to verify there are no leading or trailing special characters?

echo '"'${REPO_NAME}'"'

It should print the value in double quotes.

As for the Git remote update, that changes only your local Git repository settings, as in it doesn't make any network calls nor verifies the URL. Can you check the URL configured by running git remote -v in the local repo created?

Cheers,
Daniil

weqwebsites March 22, 2020

Okay, have done the way you suggested. please find the verbose log output below.

still the bitbucket cloud repo is not created.

> POST /2.0/repositories/weqwebsites/WT.7.growthpep.com HTTP/1.1

-----removed line that reveal the authentication password.

* upload completely sent off: 71 out of 71 bytes
< HTTP/1.1 401 Unauthorized
< Server: nginx
* Authentication problem. Ignoring this.
< WWW-Authenticate: Basic realm="Bitbucket.org HTTP"
< Content-Type: text/html; charset=utf-8
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
< Date: Mon, 23 Mar 2020 05:56:20 GMT
< X-Served-By: app-1119
< X-Static-Version: b26e4d56649f
< X-Render-Time: 0.19837808609
< Connection: Keep-Alive
< X-Version: b26e4d56649f
< X-Request-Count: 4633
< X-Frame-Options: SAMEORIGIN
< Content-Length: 0

 

-----------------

Also the when the git remote add origin execute now it gives me below error. I have SSH keys install on my linux environment yesterday itself. Has changing the password for my account made the public key invalid?

Permission denied (publickey).
fatal: Could not read from remote repository. 

 

weqwebsites March 22, 2020

Hey Daniil

 

Ignore my last reply, i had not changed my password in script after changing it due to deactivation. Now after correcting that i am still getting below error.

I have search for below error and it is mostly due to REPO NAME. the repo name that my REPO_NAME variable has is WT_7_growthpep.com

< Connection: close
< X-Version: b26e4d56649f
< X-Request-Count: 4161
< X-Frame-Options: SAMEORIGIN
< Content-Length: 148
<
* Closing connection 0
{"type": "error", "error": {"message": "Invalid slug. Slugs must be lowercase, alphanumerical, and may also contain underscores, dashes, or dots."}}

Repository weqwebsites/wt_7_growthpep.com not found
fatal: Could not read from remote repository.

Please make sure you have the

 

weqwebsites March 22, 2020

Hey Daniil,

 

You were right, it is not getting the correct name format which is causing the problem. I have figured the problem out and hopefully will solve it in couple more minutes.

 

Thanks for your help.

 

Regards,

Sahil (WEQwebsites)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events