Search code examples
githubgithub-apiappveyor

How do I download a file from a GitHub draft


I am using AppVeyor to set up the CI for a GitHub repository and upload the build artifacts to a draft named CI builds. The file is e.g. located under

https://github.com/an_organisation/a_project/releases/tag/untagged-1111aaaacccc0000dddd/filename.tar.gz

and can be accessed and downloaded from a browser.

Now I would like to access those uploaded artifact from another AppVeyor project (i.e. an appveyor.yml script). I tried without success to download with AppVeyor DownloadFile command, curl, and wget using the following commands

  set DOWNLOAD_FILENAME=filename.tar.gz
  set DOWNLOAD_ADDRESS=https://github.com/an_organisation/a_project/releases/download/untagged-1111aaaacccc0000dddd/$DOWNLOAD_FILENAME

  wget --header "Authorization: token $GH_AUTH_TOKEN" --output-document=$DOWNLOAD_FILENAME $DOWNLOAD_ADDRESS

  wget --auth-no-challenge --header "Accept:application/octet-stream" --output-document=$DOWNLOAD_FILENAME "$DOWNLOAD_ADDRESS?access_token:$GH_AUTH_TOKEN"

  curl -fsSL -G --user "$APPVEYOR_ACCOUNT_NAME:$GH_AUTH_TOKEN" -o $DOWNLOAD_FILENAME $DOWNLOAD_ADDRESS

  curl -fsSL -G -H "Authorization: token $GH_AUTH_TOKEN" -H "Accept: application/octet-stream" -o $DOWNLOAD_FILENAME $DOWNLOAD_ADDRESS

  curl -fsSL -G -H "Authorization: token $GH_AUTH_TOKEN" -H "Accept: application/octet-stream" -o $DOWNLOAD_FILENAME https://api.github.com/repos/an_organisation/a_project/releases/download/untagged-1111aaaacccc0000dddd/

Slowly I get a feeling that a file download from a draft via GitHub API or download link is not possible.

What is the correct command to download such a file?


Solution

  • TLDR Use the Get Release asset API with header Accept: application/octet-stream :

    curl -OJ -L -H "Accept: application/octet-stream" \
        -H "Authorization: Token $YOUR_TOKEN" \
        "https://api.github.com/repos/$REPO/releases/assets/$ASSET_ID"
    

    You need to have the assetID. In order to have it you need the releaseID if you have not already this information use GET /repos/:user/:repo/releases :

    curl -s -H "Authorization: Token $YOUR_TOKEN" \
       "https://api.github.com/repos/$REPO/releases" | jq '.[] | {(.name): .id}'
    

    Then get the assets IDs use GET /repos/:user/:repo/releases/:release_id :

    curl -s -H "Authorization: Token $YOUR_TOKEN" \
        "https://api.github.com/repos/$REPO/releases/$RELEASE_ID" | \
        jq -r '.assets[] | {(.id |tostring): .name}'
    

    Then once you have assetID (maybe you already had it btw) you can finally use GET /repos/:user/:repo/releases/assets/:asset_id with header Accept: application/octet-stream. From the documentation :

    To download the asset's binary content, set the Accept header of the request to application/octet-stream. The API will either redirect the client to the location, or stream it directly if possible. API clients should handle both a 200 or 302 response.

    The following download the file locally :

    curl -OJ -L -H "Accept: application/octet-stream" \
        -H "Authorization: Token $YOUR_TOKEN" \
        "https://api.github.com/repos/$REPO/releases/assets/$ASSET_ID"