GitHub – How to Download Tarball from GitHub

curlgithubtar

I have this:

curl -L "https://github.com/cmtr/cp-go-api/tarball/$commit_id" | tar x -C "$project_dir/"

I am just trying to download a tarball from github and extract it to an existing directory. The problem is I am getting this error:

Step 10/13 : RUN curl -L "https://github.com/channelmeter/cp-go-api/tarball/$commit_id" | tar x -C "$project_dir/"
 ---> Running in a883449de956
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     9  100     9    0     0     35      0 --:--:-- --:--:-- --:--:--    35
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
The command '/bin/sh -c curl -L "https://github.com/channelmeter/cp-go-api/tarball/$commit_id" | tar x -C "$project_dir/"' returned a non-zero code: 2

does anyone know why it's not a tar archive? If you go to github.com in the browser and put in this pattern, it will download a tar.gz archive:

https://github.com/<org>/<repo>/tarball/<sha>

so not sure why it's not working.

Best Answer

So ultimately it's because Github wants credentials. Without 2-factor auth, you can just do this with curl:

curl -u username:password https://github.com/<org>/<repo>/tarball/<sha>

but if you have 2-factor auth setup, then you need to use a Github access token, and you should use api.github.com instead of github.com, like so:

 curl -L "https://api.github.com/repos/<org>/<repo>/tarball/$commit_sha?access_token=$github_token" | tar -xz -C "$extract_dir/"

the access token thing is documented here: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line

Related Question