upload-artifact-with-wget/action.yml

136 lines
6.7 KiB
YAML
Raw Normal View History

2024-08-27 18:49:54 +02:00
# SPDX-License-Identifier: 0BSD
name: "Upload an artifact with wget"
author: "Chl"
2024-08-27 18:49:54 +02:00
description: |
Upload an artifact for a workflow.
This is a lighter version of upload-artifact, it only needs a shell, zip
2024-08-27 18:49:54 +02:00
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
2024-08-27 19:53:04 +02:00
compression:
description: 'Set to false to upload the (already zipped by yourself) file directly. (default: true)'
2024-08-27 19:53:04 +02:00
default: true
compression-level:
description: 'The level of compression for Zlib to be applied to the artifact archive (between 0 and 9).'
default: '6'
2024-08-27 18:49:54 +02:00
outputs:
artifact-id:
description: A unique identifier for the artifact that was just uploaded.
value: ${{ steps.uploading.outputs.artifact-id }}
artifact-url:
description: The URL for the uploaded artifact. Access may require authorization.
value: ${{ steps.uploading.outputs.artifact-url }}
2024-08-27 18:49:54 +02:00
runs:
2024-08-27 19:53:04 +02:00
using: 'composite'
2024-08-27 18:49:54 +02:00
steps:
# ACTIONS_RUNTIME_TOKEN is not yet available (2024-08) for composite actions on Github :
# https://github.com/actions/runner/issues/3046
# Forgejo tries to download this remote action even if it's not needed :
# uncomment if you want to run this action in Github.
#- name: Expose GitHub Runtime
# if: env.ACTIONS_RUNTIME_TOKEN == ''
# uses: "https://github.com/crazy-max/ghaction-github-runtime@v3"
2024-08-27 18:49:54 +02:00
- name: Upload artifact (using v4)
2024-08-27 19:53:04 +02:00
shell: sh
# id for reference in the outputs extraction
id: uploading
2024-08-27 18:49:54 +02:00
run: |
# Some optional help for debugging.
set -ex
# Compatibility layer for Github
test -z "$GITHUB_TOKEN" && GITHUB_TOKEN="${{ github.token }}"
2024-08-27 19:53:04 +02:00
# Compress the input paths into a zip archive
# (note: busybox' mktemp doesn't have the --suffix option)
MYUPLOAD="$( mktemp -u ).zip"
if [ "${{ inputs.compression }}" = "true" ]; then
2024-08-27 19:53:04 +02:00
# inputs.path can be a list of files (with wildcards and spaces) and
# the shell's command substitution + field splitting + pathname
# expansion gives a behaviour pretty close to the original Github's
# action.
# (TODO: the original can also do exclude patterns)
zip -r -"${{ inputs.compression-level }}" "$MYUPLOAD" -- $( echo "${{ inputs.path }}" ) ;
else
MYUPLOAD="${{ inputs.path }}"
fi
2024-08-27 18:49:54 +02:00
# General note:
2024-08-27 19:53:04 +02:00
# instead of using a proper JSON parser like 'jq', we use the generally
2024-08-27 18:49:54 +02:00
# available 'sed' in order to help this code being more easily reusable
2024-08-27 19:53:04 +02:00
# (at the cost of and breaking if the JSON formating happens to change,
# and readability...)
2024-08-27 18:49:54 +02:00
2024-08-27 19:53:04 +02:00
# First we extract the second field from the token (e.g. xxx.yyy.zzz),
# then we de-base64 and last, we extract the two id from
# 'Actions.Results:22:33'
2024-08-27 18:49:54 +02:00
# (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
# Github compatibility layer: ACTIONS_RESULTS_URL already ends with a '/'
ACTIONS_RESULTS_URL="$( echo "$ACTIONS_RESULTS_URL" | sed 's/\/$//' )"
2024-08-27 19:53:04 +02:00
# Request an upload URL
2024-08-27 18:49:54 +02:00
RESPONSE="$( wget -O - \
--header 'Content-Type:application/json' \
--header "Authorization: Bearer $ACTIONS_RUNTIME_TOKEN" \
2024-08-27 19:53:04 +02:00
--post-data "$( printf '{"version":4, "name":"%s", "workflow_run_backend_id":"%s", "workflow_job_run_backend_id":"%s"}' "${{ inputs.name }}" "$WORKFLOW_RUN_BACKEND_ID" "$WORKFLOW_JOB_RUN_BACKEND_ID" )" \
"$ACTIONS_RESULTS_URL"/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact
2024-08-27 18:49:54 +02:00
)"
# 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/.*"signed_\?[uU]pload_\?[uU]rl" *: *"\([^"]\+\)".*/\1/p' )"
2024-08-27 18:49:54 +02:00
# Upload our file
# (note: adding '&comp=block' at the end of the URL for Forgejo)
2024-08-27 19:53:04 +02:00
# (note 2: if it fails here, it probably means you are using the busybox
# variant of wget which can't (as of 2024-08-26) use the PUT method.
# Install the full one beforehand : apt install wget / pkg add wget / ...)
2024-08-31 02:18:51 +02:00
if [ "$GITHUB_SERVER_URL" != "https://github.com" ]; then
SIGNED_UPLOAD_URL="$SIGNED_UPLOAD_URL&comp=block"
fi
wget -O /dev/null \
--method PUT \
--body-file "$MYUPLOAD" \
--header "x-ms-blob-content-type: zip" \
--header "x-ms-blob-type: BlockBlob" \
2024-08-31 02:18:51 +02:00
"$SIGNED_UPLOAD_URL"
2024-08-27 18:49:54 +02:00
# Finalize the artifact
RESPONSE="$( wget -O - \
--header 'Content-Type:application/json' \
--header "Authorization: Bearer $ACTIONS_RUNTIME_TOKEN" \
2024-08-27 19:53:04 +02:00
--post-data "$( printf '{"hash":"sha256:%s", "name":"%s", "size":"%d", "workflow_run_backend_id":"%s", "workflow_job_run_backend_id":"%s"}' "$( sha256sum $MYUPLOAD | sed 's/[[:space:]]\+.*//' )" "${{ inputs.name }}" "$( stat -c %s $MYUPLOAD )" "$WORKFLOW_RUN_BACKEND_ID" "$WORKFLOW_JOB_RUN_BACKEND_ID" )" \
"$ACTIONS_RESULTS_URL"/twirp/github.actions.results.api.v1.ArtifactService/FinalizeArtifact
2024-08-27 18:49:54 +02:00
)"
# Store the outputs
ARTIFACT_ID="$( echo "$RESPONSE" | sed -n 's/.*"artifact_\?Id" *: *"\([^"]\+\)".*/\1/ip' )"
echo artifact-id="$ARTIFACT_ID" >> $GITHUB_OUTPUT
if [ "$GITHUB_SERVER_URL" = "https://github.com" ]; then
# note: as an alternative, there is https://api.github.com/repos/OWNER/REPO/actions/artifacts/ARTIFACT_ID
echo artifact-url="$GITHUB_SERVER_URL"/"$GITHUB_REPOSITORY"/actions/runs/"$GITHUB_RUN_ID"/artifacts/"$ARTIFACT_ID" >> $GITHUB_OUTPUT
else
# Gitea & Forgejo : github.run_number instead of github.run_id and name of the artifact instead of artifact_id
echo artifact-url="$GITHUB_SERVER_URL"/"$GITHUB_REPOSITORY"/actions/runs/"$GITHUB_RUN_NUMBER"/artifacts/"$INPUT_NAME" >> $GITHUB_OUTPUT
fi
2024-08-27 19:53:04 +02:00
# Cleanup
if [ "${{ inputs.compression }}" = "true" ]; then
2024-08-27 19:53:04 +02:00
rm -f "$MYUPLOAD"
fi