如何從 Github 下載 tarball

如何從 Github 下載 tarball

我有這個:

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>

所以不確定為什麼它不起作用。

答案1

所以歸根究底是因為 Github 想要憑證。如果沒有 2 因素身份驗證,您可以使用curl 執行此操作:

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

但如果您有 2 個因素身份驗證設置,那麼您需要使用 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

答案2

另一種方法是使用 GitHub cookie。它仍然以普通使用者/密碼開始,但在初始請求之後,您可以利用 cookie 發出進一步的請求。這是 PHP 的範例:

<?php

# GET
$get = curl_init('https://github.com/login');
curl_setopt($get, CURLOPT_COOKIEJAR, 'github.txt');
curl_setopt($get, CURLOPT_RETURNTRANSFER, true);
$log = curl_exec($get);
curl_close($get);

# POST
preg_match('/name="authenticity_token" value="([^"]*)"/', $log, $auth);
$pf['authenticity_token'] = $auth[1];
$pf['login'] = getenv('USER');
$pf['password'] = getenv('PASS');
$post = curl_init('https://github.com/session');
curl_setopt($post, CURLOPT_COOKIEFILE, 'github.txt');
curl_setopt($post, CURLOPT_POSTFIELDS, $pf);
curl_exec($post);

然後,您可以將 shell cURL 與 一起使用-b github.txt,或將 PHP cURL 與 一起 使用CURLOPT_COOKIEFILE github.txt。確保curl_close如上所示,否則 cookie 檔案將在需要後建立。

相關內容