Upload GitHub Actions artifacts to a CDN URL
This guide publishes a build report or ZIP from a workflow as a stable CDN link. The workflow below builds a synthetic report, uploads it with the three-call cdn22.net flow, and prints the link only after the upload is confirmed. Everything in it is illustrative: swap the build step for your own output.
The overview of this use case is on the CI/CD artifact storage page. This guide is the runnable recipe.
What the workflow does
- Presign.
POST /v1/files/{folderId}with afilesMetadataarray. The raw API key goes in theAuthorizationheader, with no Bearer prefix. The response returns aurlsarray withurl,idandkey. - Upload.
PUTthe raw bytes to the presigned URL with the same content type. No API key is sent on this request. - Confirm.
POST /v1/files/confirm-uploadwith{ "ids": [...] }. The response hassuccessandconfirmed, not a URL. The workflow requires success and exactly one confirmed file.
The presign response has no delivery URL field. The workflow checks the returned key and builds https://cdn.cdn22.net/<key> from it, then prints that link after the confirm step succeeds. If any step fails, the job stops with a generic message and no link.
The workflow
Add the API key as a repository secret named CDN22_API_KEY and the folder id as a repository variable named CDN22_FOLDER_ID. Both reach the shell through env, never through string interpolation in the script. The job has read-only permissions and a timeout, and the checkout action is pinned to a full commit SHA. It needs bash, curl and jq, which are on GitHub hosted Ubuntu runners.
name: Publish build report to cdn22.net
on:
workflow_dispatch:
permissions:
contents: read
jobs:
publish:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Check out
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- name: Build a synthetic report
id: build
run: |
set -euo pipefail
mkdir -p dist
{
echo "Synthetic build report"
echo "commit: $GITHUB_SHA"
} > dist/report.txt
tar -czf "dist/report-$GITHUB_SHA.tar.gz" -C dist report.txt
echo "artifact_path=dist/report-$GITHUB_SHA.tar.gz" >> "$GITHUB_OUTPUT"
- name: Upload to cdn22.net
id: cdn22
env:
CDN22_API_KEY: ${{ secrets.CDN22_API_KEY }}
CDN22_FOLDER_ID: ${{ vars.CDN22_FOLDER_ID }}
ARTIFACT_PATH: ${{ steps.build.outputs.artifact_path }}
ARTIFACT_TYPE: application/gzip
run: |
set -euo pipefail
umask 077
API="https://api.cdn22.net/v1"
CDN="https://cdn.cdn22.net"
fail() { echo "cdn22 upload failed: $1" >&2; exit 1; }
for name in CDN22_API_KEY CDN22_FOLDER_ID ARTIFACT_PATH ARTIFACT_TYPE; do
[ -n "${!name:-}" ] || fail "$name is not set"
done
[[ "$CDN22_FOLDER_ID" =~ ^[A-Za-z0-9_-]+$ ]] || fail "folder id has unexpected characters"
[[ "$ARTIFACT_TYPE" =~ ^[a-z]+/[a-z0-9.+-]+$ ]] || fail "content type has unexpected characters"
[ -f "$ARTIFACT_PATH" ] || fail "artifact file not found"
case "$CDN22_API_KEY" in
*$'\r'*|*$'\n'*) fail "API key has unexpected characters" ;;
esac
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
printf 'Authorization: %s\n' "$CDN22_API_KEY" > "$TMP/auth"
NAME="$(basename "$ARTIFACT_PATH")"
SIZE="$(wc -c < "$ARTIFACT_PATH" | tr -d ' ')"
CURL="curl --disable --silent --fail --proto =https --connect-timeout 10"
# 1. Ask for a presigned upload URL. The raw key goes in Authorization.
# Each call must return a 2xx status itself, so redirects fail.
jq -n --arg name "$NAME" --argjson size "$SIZE" --arg type "$ARTIFACT_TYPE" \
'{filesMetadata: [{name: $name, size: $size, type: $type}]}' > "$TMP/presign-request.json"
STATUS="$($CURL --write-out '%{http_code}' --max-time 60 -X POST -H "@$TMP/auth" \
-H "Content-Type: application/json" --data-binary "@$TMP/presign-request.json" \
--output "$TMP/presign.json" "$API/files/$CDN22_FOLDER_ID")" || fail "presign request failed"
[[ "$STATUS" =~ ^2[0-9][0-9]$ ]] || fail "presign request failed"
jq -s -e 'length == 1 and (.[0] | type == "object" and .success == true
and (.urls | type == "array" and length == 1)
and (.urls[0] | .duplicate == false and .private == false
and (.url | type == "string") and (.id | type == "string")
and (.key | type == "string")))' "$TMP/presign.json" > /dev/null 2>&1 \
|| fail "unexpected presign response"
# A trailing "." survives command substitution, so trailing newlines stay visible.
UPLOAD_URL="$(jq -r '.urls[0].url + "."' "$TMP/presign.json")"
FILE_ID="$(jq -r '.urls[0].id + "."' "$TMP/presign.json")"
KEY="$(jq -r '.urls[0].key + "."' "$TMP/presign.json")"
UPLOAD_URL="${UPLOAD_URL%.}"
FILE_ID="${FILE_ID%.}"
KEY="${KEY%.}"
URL_PATTERN='^https://[A-Za-z0-9._~:/?#@!$&()*+,;=%-]+$'
[[ "$UPLOAD_URL" =~ $URL_PATTERN ]] || fail "upload URL is not usable"
[[ "$FILE_ID" =~ ^[A-Za-z0-9-]{8,64}$ ]] || fail "file id is not usable"
[[ "$KEY" =~ ^[A-Za-z0-9][A-Za-z0-9._/-]*$ ]] || fail "key is not usable"
[[ "$KEY" != *..* && "$KEY" != *//* ]] || fail "key is not usable"
DELIVERY_URL="$CDN/$KEY"
# 2. PUT the raw bytes with the same content type. No API key here.
STATUS="$($CURL --write-out '%{http_code}' --max-time 300 -X PUT \
-H "Content-Type: $ARTIFACT_TYPE" --data-binary "@$ARTIFACT_PATH" \
--output /dev/null "$UPLOAD_URL")" || fail "byte upload failed"
[[ "$STATUS" =~ ^2[0-9][0-9]$ ]] || fail "byte upload failed"
# 3. Confirm. The response has success and confirmed, not a URL.
jq -n --arg id "$FILE_ID" '{ids: [$id]}' > "$TMP/confirm-request.json"
STATUS="$($CURL --write-out '%{http_code}' --max-time 60 -X POST -H "@$TMP/auth" \
-H "Content-Type: application/json" --data-binary "@$TMP/confirm-request.json" \
--output "$TMP/confirm.json" "$API/files/confirm-upload")" || fail "confirm request failed"
[[ "$STATUS" =~ ^2[0-9][0-9]$ ]] || fail "confirm request failed"
jq -s -e 'length == 1 and (.[0] | type == "object" and .success == true and .confirmed == 1)' \
"$TMP/confirm.json" > /dev/null 2>&1 \
|| fail "upload was not confirmed"
# Only a confirmed upload gets its URL published.
echo "Published: $DELIVERY_URL"
if [ -n "${GITHUB_OUTPUT:-}" ]; then
echo "cdn_url=$DELIVERY_URL" >> "$GITHUB_OUTPUT"
fi
Scroll the code block to read long lines and the remaining steps.
The script never prints the API key, the presigned upload URL or response bodies. It does not use set -x, curl runs with --silent --fail, and temporary files live in a mktemp directory that is removed on exit. The key and ids are checked against a strict character set before the link is written, so the output cannot carry a newline or an unexpected scheme.
Public folder assumption
The recipe assumes the target folder is public. It stops if the presign response reports private as anything other than false, and it does not send a private-upload flag. A public CDN link can be opened by anyone who has it and does not grant access to private files or your account. Publish only output that is safe to share. A successful confirm is not proof that the link is open or reachable for everyone: an upload can still be blocked by a scan, expiry or account state, so check the link before you share it.
Forks and secrets
Workflows triggered by pull requests from forks ordinarily do not receive repository secrets. That is why this workflow is manual. A manual trigger does not itself enforce which code runs, so when you start it, choose a reviewed or trusted ref. Never use pull_request_target to hand secrets to code from an untrusted branch. Use an API key you can revoke, keep it in repository or environment secrets only, and revoke it if it ever appears in a log or a public file.
When the native artifact action is enough
If only later jobs in the same run, or people with repository access, need the file, use actions/upload-artifact. Retention and size limits are set by GitHub and can change, and repository or organization settings can lower them. Retention differs between public and private repositories. Read the GitHub documentation on artifact retention for the current rules. Use a CDN link when you want a plain URL for release notes or people outside the repository.
Limits and cost
Uploads need an active funded account. This guide does not guarantee that any usage stays free, and recurring uploads add storage, delivery and credit use. The storage cost calculator and the file upload API page list the current limits. For the same flow outside CI, see the upload guide and the curl file upload page.
FAQ
Why does the workflow use workflow_dispatch?
A manual trigger lets you choose when to publish. Run it only on a reviewed or trusted ref; it does not isolate untrusted code from secrets. Pull requests from forks ordinarily do not receive repository secrets, so an upload step would fail there anyway. Do not switch to pull_request_target to work around that, because it runs with secrets in the context of the base repository and can expose them to untrusted code.
Does the confirm call return the file URL?
No. The confirm response contains success and a confirmed count. The workflow keeps the key from the presign response and builds the public URL from it, then prints that URL only after confirmed equals 1.
Is the uploaded file private?
The recipe assumes the folder is public and stops if the presign response says the file is private or a duplicate. A public CDN link is readable by anyone who has it, so upload only non-sensitive output. Public links do not grant private access to anything else in your account.
When should I use actions/upload-artifact instead?
Use the native action when only later jobs or people with repository access need the file. GitHub applies its own retention settings and limits, which can change, so check the current documentation before relying on a number.
What does this cost?
You pay for storage, delivery and credits as described on the cost calculator and file upload API pages. Check the current limits and prices there before wiring up a recurring job.
Get an API key
Create an account, make a public folder and an API key, then add them to your repository settings.