28

I'm writing a script to easily deploy an application. The code for the application is stored in a private BitBucket repository.

I'd like to be able to download a zip file of the commit. I've tried authenticating with the following code:

https://user:pass@bitbucket.org/user/repo/get/commit.zip

However, instead of accomplishing the task it redirects to the login page on BitBucket.

GrowlTiger
  • 496
  • 4
  • 11
user2117190
  • 545
  • 2
  • 6
  • 10

7 Answers7

36

Personally, I didn't want to put my password into my script to accomplish this. So the trick was to run the following command, after adding your public key to your bitbucket account:

git archive --remote=ssh://git@bitbucket.org/your_bitbucket_username/your_repository.git --format=zip --output="name_of_your_desired_zip_file.zip" master

I have multiple keys on my system, if you do too, you will want to create a config file within your ~/.ssh directory that specifies to use a specific key for bitbucket connections.

~/.ssh/config

Host bitbucket.org
  StrictHostKeyChecking no
  IdentityFile /home/me/.ssh/my_private_key
Nick
  • 1,180
  • 1
  • 10
  • 15
  • 2
    This is just the way to do, any other way to authenticate with clear text password is wrong, unless you don't care about security. – thomas Mar 09 '17 at 07:30
28

In order to download a zipped copy of a private Bitbucket repository from the command line, use the following:

curl --digest --user <username>:<password> https://bitbucket.org/<username>/<repository>/get/<branchname>.zip -o <branchname>.zip

where <username> and <password> are the Bitbucket account name and password, <repository> is the repo name and <branchname> is the branch. If you'd rather download a specific commit, use the SHA-1 hash of the commit in place of <branchname>.

The --digest flag is for your security, and is highly recommended. It accomplishes authentication so that your username and password are not sent in the clear. The -o flag sends the output of the curl command to disk as a file, instead of streaming across your terminal screen.

Note: Bitbucket's authentication scheme isn't compatible with wget. That is why you must use curl.

For public Bitbucket repositories the command is:

curl https://bitbucket.org/<username>/<repository>/get/<branchname>.zip -o <branchname>.zip

Or alternately, you may use wget for public repositories since no authentication is required:

wget https://bitbucket.org/<username>/<repository>/get/<branchname>.zip

In addition to .zip format, you may download repositories in .gz and .bz2 flavors. Simply replace .zip in the code above with either .gz or .bz2to download the repository in the compressed format of your choice.

GrowlTiger
  • 496
  • 4
  • 11
  • 4
    I tried `curl --digest --user : https://bitbucket.org///downloads/ -o ` to download from the Download area, but it doesn't work. Any idea why not? – Faheem Mitha Sep 06 '13 at 12:40
  • Worked like a charm for me. Thanks! Now I created an auto-updater script for my website! – alexander7567 Jan 16 '14 at 20:00
  • 1
    @FaheemMitha, to download from the Download area, you need to add the `-L` option, like this: `curl -L --digest --user : https://bitbucket.org///downloads/ -o ` – user1027169 May 25 '14 at 22:21
  • 1
    Is `--digest` necessary since it's over https? – amos Jun 04 '14 at 16:26
  • 4
    BitBucket has deprecated and then turned off support for Digest access authentication. [More info.](https://blog.bitbucket.org/2015/04/03/fare-thee-well-digest-access-authentication) – naXa stands with Ukraine Aug 17 '15 at 11:25
  • 1
    Note that wget now seems to work with Bitbucket's authentication scheme. See: [laike9m's](http://stackoverflow.com/a/28381930/1448678) and [mstra's](http://stackoverflow.com/a/25683149/1448678) answers. – Jonny Oct 05 '16 at 14:39
  • What you describe works, but it downloads not the repository but a checkout of the specified version of the repository. My connection is very bad, and `git clone` just aborts for a 10MB repo. However I can download a 11MB dump of the latest version easily. How can I get the repo itself with `curl`? (Only the `.git` directory would be good enough.) – peschü Feb 09 '17 at 15:35
11

The --digest flag is for your security, and is highly recommended. It accomplishes authentication so that your username and password are not sent in the clear.

This not true.

Bitbucket exclusively uses TLS and so at no point does anything go over the wire in clear text. As a result, Digest provides no benefit over Basic Auth. In fact, considering that Digest is server-initiated, you incur an additional server round-trip requesting the server-provided nonce.

Our use of Digest has been redundant and deprecated ever since we stopped offering unencrypted HTTP access several years ago and was kept only because there were curl-based scripts doing --digest as suggested by @GrowlTiger.

In fact, we are about to turn off Digest altogether on May 1st, after which curl --digest will cease to work.

More info can be found: https://blog.bitbucket.org/2015/04/03/fare-thee-well-digest-access-authentication/

Erik van Zijst
  • 2,282
  • 2
  • 17
  • 14
  • 2
    This is not an answer to the initial question, but a commentary to another answer. Please re-post as a comment to the offending answer (or attached to the question itself). – Stéphane Gourichon Mar 01 '18 at 16:14
8

For those who want to download single file from private repo on bitbucket, I've tried the above but none worked. Finally I got it working with the below command:

wget --user=<user> --password=<password> https://bitbucket.org/<user>/<repo>/raw/master/<filename>
laike9m
  • 18,344
  • 20
  • 107
  • 140
  • 3
    This worked for me too, though I prefer using `--ask-password` rather than `--password=` so as not to have my password visible and/or stored anywhere as plain text. – Jonny Oct 05 '16 at 14:34
3

GrowlTigers answer is great, just an update: with wget it seems to work now, too:

wget --user=<username> --password='<password>' https://bitbucket.org/<user>/<repo>/get/<filename>.tar.gz
mstra001
  • 138
  • 7
1

Does the tool you're using support basic authentication credentials in the URL? Curl request with digest auth in PHP for download Bitbucket private repository strongly implies that:

curl --user user:pass https://bitbucket.org/user/repo/get/commit.zip >commit.zip

works.

Community
  • 1
  • 1
Joe
  • 29,416
  • 12
  • 68
  • 88
1

I found that this worked in the newer version of Bitbucket

repository_list=" a b c"
for repository in ${repository_list}
do
    echo "Getting: ${repository}"
    curl --user userid:password \
         http://bitjira.xxx.com:7990/rest/api/latest/projects/WP/repos/${repository}/archive?format=zip  \
         -o ${repository}.zip
done
Pretasoc
  • 1,116
  • 12
  • 22
Peter Lenahan
  • 61
  • 1
  • 6