diff --git a/README.md b/README.md index bdc0749..cd729a3 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,11 @@ The release version to fetch from in the form `tags/` or ` ### `target` -Target file path. Only supports paths to subdirectories of the GitHub Actions workspace directory +Target file path. Only supports paths to subdirectories of the GitHub Actions workspace directory. + +### `regex` + +Boolean indicating if `file` is to be interpreted as regular expression. Defaults to `false`. ## Outputs @@ -40,6 +44,17 @@ The body (description) of a release. ## Example usage +Save a single asset as a new file `/plague-linux.zip`: +```yaml +uses: dsaltares/fetch-gh-release-asset@master +with: + repo: "dsaltares/godot-wild-jam-18" + version: "tags/v0.1.18" + file: "plague-linux.zip" + token: ${{ secrets.GITHUB_TOKEN }} +``` + +Save a single asset as a new file `/subdir/plague-linux.zip`: ```yaml uses: dsaltares/fetch-gh-release-asset@master with: @@ -50,6 +65,29 @@ with: token: ${{ secrets.GITHUB_TOKEN }} ``` +Save a range of assets as new files in directory ``: +```yaml +uses: dsaltares/fetch-gh-release-asset@master +with: + repo: "dsaltares/godot-wild-jam-18" + version: "tags/v0.1.18" + regex: true + file: "plague-.*\\.zip" + token: ${{ secrets.GITHUB_TOKEN }} +``` + +Save a range of assets as new files in directory `/subdir`: +```yaml +uses: dsaltares/fetch-gh-release-asset@master +with: + repo: "dsaltares/godot-wild-jam-18" + version: "tags/v0.1.18" + regex: true + file: "plague-.*\\.zip" + target: "subdir/" + token: ${{ secrets.GITHUB_TOKEN }} +``` + ## Support This action only supports Linux runners as this is a [docker container](https://docs.github.com/en/actions/creating-actions/about-actions#types-of-actions) action. If you encounter `Error: Container action is only supported on Linux` then you are using non-linux runner. diff --git a/action.yaml b/action.yaml index 5073df3..e0482ac 100644 --- a/action.yaml +++ b/action.yaml @@ -20,7 +20,10 @@ inputs: description: 'The GitHub token. Typically this will be secrets.GITHUB_TOKEN' required: false default: ${{ github.token }} - + regex: + description: 'Use regex for file input' + type: boolean + required: false runs: using: 'docker' image: 'Dockerfile' @@ -30,6 +33,7 @@ runs: - ${{ inputs.file }} - ${{ inputs.target }} - ${{ inputs.token }} + - ${{ inputs.regex }} outputs: version: diff --git a/fetch_github_asset.sh b/fetch_github_asset.sh index a77f458..38e0329 100755 --- a/fetch_github_asset.sh +++ b/fetch_github_asset.sh @@ -15,8 +15,14 @@ if [[ -n ${INPUT_REPO} ]]; then REPO=$INPUT_REPO fi +echo "INPUT_REGEX:$INPUT_REGEX" + # Optional target file path -TARGET=$INPUT_FILE +if [[ "$INPUT_REGEX" == "true" ]]; then + TARGET="" +else + TARGET=$INPUT_FILE +fi if [[ -n ${INPUT_TARGET} ]]; then TARGET=$INPUT_TARGET fi @@ -28,6 +34,7 @@ if [[ -n ${INPUT_TOKEN} ]]; then fi API_URL="https://api.github.com/repos/$REPO" +echo "$API_URL/releases/${INPUT_VERSION}" RELEASE_DATA=$(curl ${TOKEN:+"-H"} ${TOKEN:+"Authorization: token ${TOKEN}"} \ "$API_URL/releases/${INPUT_VERSION}") MESSAGE=$(echo "$RELEASE_DATA" | jq -r ".message") @@ -44,8 +51,15 @@ if [[ "$MESSAGE" == "Not Found" ]]; then fi echo "MESSAGE: '$RELEASE_DATA'" +if [[ "$INPUT_REGEX" == "true" ]]; then + echo $INPUT_FILE + INPUT_FILE_ESCAPE=$(echo $INPUT_FILE | sed -e "s/\\\\/\\\\\\\/g") + echo $INPUT_FILE_ESCAPE + ASSET_ID=$(echo "$RELEASE_DATA" | jq -r ".assets | .[] | select(.name|test(\"${INPUT_FILE_ESCAPE}\")) | .id") +else + ASSET_ID=$(echo "$RELEASE_DATA" | jq -r ".assets | .[] | select(.name == \"${INPUT_FILE}\") | .id") +fi -ASSET_ID=$(echo "$RELEASE_DATA" | jq -r ".assets | .[] | select(.name == \"${INPUT_FILE}\") | .id") TAG_VERSION=$(echo "$RELEASE_DATA" | jq -r ".tag_name" | sed -e "s/^v//" | sed -e "s/^v.//") RELEASE_NAME=$(echo "$RELEASE_DATA" | jq -r ".name") RELEASE_BODY=$(echo "$RELEASE_DATA" | jq -r ".body") @@ -55,14 +69,29 @@ if [[ -z "$ASSET_ID" ]]; then exit 1 fi -curl \ - -J \ - -L \ - -H "Accept: application/octet-stream" \ - ${TOKEN:+"-H"} ${TOKEN:+"Authorization: token ${TOKEN}"} \ - "$API_URL/releases/assets/$ASSET_ID" \ - --create-dirs \ - -o "${TARGET}" +if [[ "$INPUT_REGEX" == "true" ]]; then + for SINGLE_ASSET_ID in $ASSET_ID + do + NAME=$(echo "$RELEASE_DATA" | jq -r ".assets | .[] | select(.id == ${SINGLE_ASSET_ID}) | .name") + curl \ + -J \ + -L \ + -H "Accept: application/octet-stream" \ + ${TOKEN:+"-H"} ${TOKEN:+"Authorization: token ${TOKEN}"} \ + "$API_URL/releases/assets/$SINGLE_ASSET_ID" \ + --create-dirs \ + -o "${TARGET}${NAME}" + done +else + curl \ + -J \ + -L \ + -H "Accept: application/octet-stream" \ + ${TOKEN:+"-H"} ${TOKEN:+"Authorization: token ${TOKEN}"} \ + "$API_URL/releases/assets/$ASSET_ID" \ + --create-dirs \ + -o "${TARGET}" +fi echo "::set-output name=version::$TAG_VERSION" echo "::set-output name=name::$RELEASE_NAME"