Tar

如何從 Github 下載壓縮包

  • June 29, 2021

我有這個:

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

我只是想從 github 下載一個 tarball 並將其解壓縮到現有目錄。問題是我收到此錯誤:

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

有誰知道為什麼它不是 tar 檔案?如果您在瀏覽器中訪問 github.com 並輸入此模式,它將下載一個 tar.gz 存檔:

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

所以不知道為什麼它不工作。

所以最終是因為 Github 想要憑據。如果沒有 2-factor auth,您可以使用 curl 執行此操作:

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

但是如果你有 2-factor auth 設置,那麼你需要使用 Github 訪問令牌,並且你應該使用 api.github.com 而不是 github.com,如下所示:

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

訪問令牌的事情記錄在這裡: https ://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line

引用自:https://unix.stackexchange.com/questions/573455