Skip to content

Instantly share code, notes, and snippets.

@umohi
Created September 21, 2018 22:48
Show Gist options
  • Save umohi/bfc7ad9a845fc10289c03d532e3d2c2f to your computer and use it in GitHub Desktop.
Save umohi/bfc7ad9a845fc10289c03d532e3d2c2f to your computer and use it in GitHub Desktop.
use curl to download release artifact from github private repository
# in order to download release artifacts from github, you have to first retreive the
# list of asset URLs using the github repo REST API. Use the asset URL to download
# the artifact as a octet-stream data stream. You will need to get an access token
# from "settings -> developer settings -> personal access tokens" on the github UI
#!/bin/bash -e
owner="MY_ORG_NAME"
repo="MY_REPO_NAME"
tag="ARTIFACT_TAG"
artifact="ARTIFACT_NAME"
token="MY_ACCESS_TOKEN"
list_asset_url="https://api.github.com/repos/${owner}/${repo}/releases/tags/${tag}?access_token=${token}"
# get url for artifact with name==$artifact
asset_url=$(curl "${list_asset_url}" | jq ".assets[] | select(.name==\"${artifact}\") | .url" | sed 's/\"//g')
# download the artifact
curl -vLJO -H 'Accept: application/octet-stream' \
"${asset_url}?access_token=${token}"
@mavaddat
Copy link

mavaddat commented Feb 8, 2022

It is also possible without PAT.

# Download the curl binary from https://api.github.com/moparisthebest/static-curl/

USER=moparisthebest \
REPO=static-curl \
GITHUB_API=https://api.github.com/repos/${USER}/${REPO}/releases/latest

ARCH=$(dpkg --print-architecture) 
echo "Querying GitHub for the latest $ARCH release" 
LATEST=$(curl -L -s -H 'Accept: application/json' $GITHUB_API) 
LATEST_TAG=$(echo $LATEST_CURL | jq -r '.tag_name') 
echo "Found version $LATEST_TAG" \
LATEST_URL=$(echo $LATEST | jq -r ".assets[] | select(.name | contains(\"$ARCH\")) | .url") 
echo "Downloading curl $LATEST_TAG from $USER" 
curl -vLJO -H 'Accept: application/octet-stream' $LATEST_URL

@jasonm23
Copy link

jasonm23 commented May 8, 2023

For Workflow Artifacts

I arrived here from Google looking for how to download GH Workflow Artifacts.

This will get the most recent.

gh auth login --with-token <<<$ACCESS_TOKEN
asset=$(gh api /repos/{user|org}/{repo}/actions/artifacts --jq ".artifacts[0] .archive_download_url")
curl -L -o artifact.zip "$asset" \
   -H "Accept: application/vnd.github.v3+json" \
   -H "Authorization: Bearer $ACCESS_TOKEN"

# unzip artifact.zip

This is what is used to select the first listed (latest).

.artifacts[0]

You can also filter on .artifacts[] to fetch by workflow run id, etc.

The payload from .artifacts[] looks like:

{
  "archive_download_url": "https://api.github.com/repos/{user|org}/{repo}/actions/artifacts/{artifact id}/zip",
  "created_at": "{iso date time string}",
  "expired": {bool},
  "expires_at": "{iso date time string}",
  "id": {artifact id},
  "name": "{artifact name}",
  "node_id": "{node id}",
  "size_in_bytes": {int},
  "updated_at": "{iso date time string}",
  "url": "https://api.github.com/repos/{user|org}/{repo}/actions/artifacts/{artifact id}",
  "workflow_run": {
    "head_branch": "{branch}",
    "head_repository_id": {repo id},
    "head_sha": "{sha}",
    "id": {workflow run id},
    "repository_id": {repo id}
  }
}

Which should be useful to get the artifact you're looking for.

@thenets
Copy link

thenets commented Sep 13, 2024

Based on this work, I created a dedicated script:
https://github.com/thenets/bash-helpers/blob/main/scripts/github-release-downloader.sh

Example of usage:

$ ./github-release-downloader.sh \
    gohugoio \
    hugo \
    "_linux-amd64.tar.gz"

>> [INFO   ] Parameters:
>> [INFO   ]    GITHUB_ORG     : gohugoio
>> [INFO   ]    GITHUB_REPO    : hugo
>> [INFO   ]    SEARCH_PATTERN : _linux-amd64.tar.gz
>> [INFO   ] [get_releases] checking https://api.github.com/repos/gohugoio/hugo/releases
>> [INFO   ] [get_latest_release] latest release identified: name=v0.134.2 id=174307672
>> [INFO   ] [get_release_assets] release=v0.134.2 - total_of_assets=22
>> [INFO   ] [search_asset_by_name] found hugo_0.134.2_linux-amd64.tar.gz
>> [INFO   ] [search_asset_by_name] https://github.com/gohugoio/hugo/releases/download/v0.134.2/hugo_0.134.2_linux-amd64.tar.gz
>> [INFO   ] Downloading...
>> $ curl --progress-bar -o /home/user/path/hugo_0.134.2_linux-amd64.tar.gz https://github.com/gohugoio/hugo/releases/download/v0.134.2/hugo_0.134.2_linux-amd64.tar.gz                                                                                                                    
>> [SUCCESS] Saved on: /home/user/path/hugo_0.134.2_linux-amd64.tar.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment