diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0f5c2db --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine:latest + +RUN apk add --no-cache \ + curl \ + jq + +COPY fetch_github_asset.sh /fetch_github_asset.sh + +ENTRYPOINT ["/fetch_github_asset.sh"] diff --git a/README.md b/README.md index 3f79d40..91e7a9f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,27 @@ -# fetch-gh-release-asset -Github Action to download an asset from a Github release +# Fetch GH Release Asset + +This action downloads an asset from a Github release. Private repos are supported. + +## Inputs + +### `repo` + +**Required** The `org/repo`. + +### `version` + +The release version to fetch from. Default `"latest"`. + +### `file` + +**Required** The name of the file in the release. + +## Example usage + +```yaml +uses: dsaltares/fetch-gh-release-asset@master +with: + repo: "dsaltares/godot-wild-jam-18" + version: "latest" + file: "plague-linux.zip" +``` diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..66f2505 --- /dev/null +++ b/action.yaml @@ -0,0 +1,26 @@ +name: 'Fetch Github Release Asset' +description: 'Downloads an asset from a Github release' + +inputs: + repo: + description: 'org/repo to download from' + required: true + version: + description: 'version of the release to download from' + required: false + default: 'latest' + file: + description: 'name of the file in the release to download' + required: true + +runs: + using: 'docker' + image: 'Dockerfile' + args: + - ${{ inputs.repo }} + - ${{ inputs.version }} + - ${{ inputs.file }} + +branding: + icon: 'download-cloud' + color: 'orange' diff --git a/fetch_github_asset.sh b/fetch_github_asset.sh new file mode 100755 index 0000000..6e8be28 --- /dev/null +++ b/fetch_github_asset.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +if [[ -z "$GITHUB_TOKEN" ]]; then + echo "Missing GITHUB_TOKEN env variable" + exit 1 +fi + +if [[ -z "$INPUT_REPO" ]]; then + echo "Missing repo input in the action" + exit 1 +fi + +if [[ -z "$INPUT_FILE" ]]; then + echo "Missing file input in the action" + exit 1 +fi + +API_URL="https://$GITHUB_TOKEN:@api.github.com/repos/$INPUT_REPO" +ASSET_ID=$(curl $API_URL/releases/${INPUT_VERSION} | jq -r ".assets | map(select(.name == \"${INPUT_FILE}\"))[0].id") + +if [[ -z "$ASSET_ID" ]]; then + echo "Could not find asset id" + exit 1 +fi + +curl \ + -J \ + -L \ + -H "Accept: application/octet-stream" \ + "$API_URL/releases/assets/$ASSET_ID" \ + -o ${INPUT_FILE}