upload-artifact-with-wget/action.yml
2024-08-27 19:32:14 +02:00

72 lines
3.4 KiB
YAML

# SPDX-License-Identifier: 0BSD
name: "Upload artifact with wget"
author: "Chl <chl@xlii.si>"
description: |
Upload an artifact for a workflow.
This is a far lighter version of upload-artifact, it only need a shell, zip
and the full version of wget (unfortunately, as of 2024-08-26, the busybox
variant isn't capable of using the PUT method).
inputs:
name:
description: 'Artifact name'
default: 'artifact'
path:
description: 'A file, directory or wildcard pattern that describes what to upload'
required: true
outputs:
artifact-id:
description: A unique identifier for the artifact that was just uploaded. Empty if the artifact upload failed.
runs:
steps:
- name: Upload artifact (using v4)
run: |
# Some optional help for debugging.
set -ex
# Zip the files
printf "%s\n" "$GITHUB_OUTPUT"
printf "%s\n" ${{ inputs.path }}
return
# General note:
# instead of using a proper JSON parser like 'jq', we use the readily
# available 'sed' in order to help this code being more easily reusable
# (at the cost of a breaking if the JSON formating happens to
# change...)
# We extract the 'Actions.Results:22:33' from ACTIONS_RUNTIME_TOKEN
# (note: base64 -d doesn't like when the '==' padding is missing, so 2>/dev/null and relying on the piping to forget about non-zero return code...)
read WORKFLOW_RUN_BACKEND_ID WORKFLOW_JOB_RUN_BACKEND_ID <<EOF
$( echo "$ACTIONS_RUNTIME_TOKEN" | sed 's/.*\.\(.*\)\..*/\1/' | base64 -d 2>/dev/null | sed 's/.*Actions.Results:\([^:]\+\):\([^:" ]\+\).*/\1 \2/' )
EOF
# Get the upload URL
RESPONSE="$( wget -O - \
--header 'Content-Type:application/json' \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--post-data "$( printf '{"version":4, "name":"%s", "workflow_run_backend_id":"%s", "workflow_job_run_backend_id":"%s"}' "$MYFILENAME" "$WORKFLOW_RUN_BACKEND_ID" "$WORKFLOW_JOB_RUN_BACKEND_ID" )" \
"$GITHUB_SERVER_URL"/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact
)"
# We get a JSON with an signedUploadUrl similar to :
# https://entrepot.xlii.si/twirp/github.actions.results.api.v1.ArtifactService/UploadArtifact?sig=yWWEI8tIIECp8D7E5TVh4_6G2pZxWaVdQcSYaCsx5s0=&expires=2024-08-26+07%3A20%3A49.886890537+%2B0200+CEST&artifactName=mymodule-1.2.3.zip&taskID=63
SIGNED_UPLOAD_URL="$( echo "$RESPONSE" | sed -n 's/.*"signedUploadUrl" *: *"\([^"]\+\)".*/\1/p' )"
# Upload our file
# (note: adding '&comp=block' at the end of the URL)
wget --method PUT --body-file "$MYFILENAME" "$SIGNED_UPLOAD_URL&comp=block"
# Finalize the artifact
RESPONSE="$( wget -O - \
--header 'Content-Type:application/json' \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--post-data "$( printf '{"hash":"sha256:%s", "name":"%s", "size":"%d", "workflow_run_backend_id":"%s", "workflow_job_run_backend_id":"%s"}' "$( sha256sum $MYFILENAME | sed 's/[[:space:]]\+.*//' )" "$MYFILENAME" "$( stat -c %s $MYFILENAME )" "$WORKFLOW_RUN_BACKEND_ID" "$WORKFLOW_JOB_RUN_BACKEND_ID" )" \
"$GITHUB_SERVER_URL"/twirp/github.actions.results.api.v1.ArtifactService/FinalizeArtifact
)"
# Store the outputs
echo artifact-id="$( echo "$RESPONSE" | sed -n 's/.*"artifactId" *: *"\([^"]\+\)".*/\1/p' )" >> $GITHUB_OUTPUT