Skip to content

Instantly share code, notes, and snippets.

@mrsarm
Last active April 19, 2024 22:25
Show Gist options
  • Save mrsarm/95279381f3d8bf4269499fb437888e2c to your computer and use it in GitHub Desktop.
Save mrsarm/95279381f3d8bf4269499fb437888e2c to your computer and use it in GitHub Desktop.
github-file.sh: GitHub Action script to get a file from a repo/branch, otherwise fallback to another branch.
#!/usr/bin/env sh
# Get a file from a GitHub repository, trying first to download it
# from a branch passed by argument, if the file doesn't exist,
# fallback to another branch version.
#
# Script to be called from Github Actions as following:
#
# - name: Get GitHub downloader
# run: wget https://gist.github.com/mrsarm/95279381f3d8bf4269499fb437888e2c/raw/github-file.sh
# - name: Get GitHub file
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# run: bash ./github-file.sh [file] [user/repo] [branch] [fallback_branch] $GITHUB_TOKEN
FILE="$1"
REPO="$2"
BRANCH="$3"
FALLBACK_BRANCH="$4"
TOKEN="$5"
URL="https://raw.githubusercontent.com/$REPO/$BRANCH/$FILE"
set -x
curl -sS -w "%{http_code}\n" -H "Authorization: token ${TOKEN}" "$URL" -o "$FILE" | grep "200"
if [ "$?" != 0 ] ; then
URL="https://raw.githubusercontent.com/$REPO/$FALLBACK_BRANCH/$FILE"
curl -sS -w "%{http_code}\n" -H "Authorization: token ${TOKEN}" "$URL" -o "$FILE" | grep "200"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment