upload-artifact/entrypoint.sh

112 lines
3.1 KiB
Bash
Raw Permalink Normal View History

2025-01-02 03:26:57 +00:00
#!/bin/bash -e
2025-01-08 06:21:32 +00:00
API="$GITHUB_SERVER_URL/api/actions_pipeline/_apis/pipelines/workflows/$GITHUB_RUN_ID/artifacts"
2025-01-08 06:27:35 +00:00
[ -n "$INPUT_RETENTION_DAYS" ] && RETENTION="retentionDays=$INPUT_RETENTION_DAYS"
2025-01-02 03:26:57 +00:00
CURL=(curl --fail -v -D/dev/stdout
2025-01-08 06:21:32 +00:00
--header "Authorization: Bearer $ACTIONS_RUNTIME_TOKEN"
--data '{"Type":"actions_storage","Name":"'"$INPUT_NAME"'"}'
2025-01-02 03:26:57 +00:00
)
2025-01-08 06:21:32 +00:00
"${CURL[@]}" "$API" -oresp0.json
2025-01-02 03:26:57 +00:00
cat resp0.json
UPLOAD_URL=$(jq -r '.fileContainerResourceUrl' resp0.json)
rm -f resp0.json
for artifact in $INPUT_PATH; do
2025-01-02 03:26:57 +00:00
content_length=$(stat -c '%s' "$artifact")
2025-01-08 06:41:18 +00:00
md5="$(openssl md5 -binary "$artifact" | base64)"
2025-01-02 03:26:57 +00:00
"${CURL[@]}" -XPUT -o resp1.json \
--header "x-actions-results-md5: $md5" \
--header "x-tfs-filelength: $content_length" \
--header "content-range: bytes 0-$((content_length - 1))/$content_length" \
2025-01-08 06:27:35 +00:00
"$UPLOAD_URL?$RETENTION&itemPath=$INPUT_NAME%2F$artifact"
2025-01-02 03:26:57 +00:00
cat resp1.json
"${CURL[@]}" -XPATCH -oresp2.json "$API&artifactName=$INPUT_NAME"
2025-01-02 03:26:57 +00:00
cat resp2.json
rm -f resp1.json resp2.json
done
2025-01-08 06:21:32 +00:00
#function get_ids_from_token() {
# local ids
#
# mapfile -t ids \
# < <(jq -Rr 'split(".")[1] | @base64d | fromjson | .scp | split(":")' <<< "$ACTIONS_RUNTIME_TOKEN")
#
# WORKFLOWRUNID="${ids[1]}"
# WORKFLOWJOBRUNID="${ids[2]}"
#}
#
#get_ids_from_token
#
# const artifactClient = internalArtifactTwirpClient()
#
# const createArtifactReq: CreateArtifactRequest = {
# "$WORKFLOWRUNID"
# "$WORKFLOWJOBRUNID"
# name,
# version: 4
# }
#
# // if there is a retention period, add it to the request
# const expiresAt = getExpiration(options?.retentionDays)
# if (expiresAt) {
# createArtifactReq.expiresAt = expiresAt
# }
#
# const createArtifactResp =
# await artifactClient.CreateArtifact(createArtifactReq)
# if (!createArtifactResp.ok) {
# throw new InvalidResponseError(
# 'CreateArtifact: response from backend was not ok'
# )
# }
#
# const zipUploadStream = await createZipUploadStream(
# zipSpecification,
# options?.compressionLevel
# )
#
# // Upload zip to blob storage
# const uploadResult = await uploadZipToBlobStorage(
# createArtifactResp.signedUploadUrl,
# zipUploadStream
# )
#
# // finalize the artifact
# const finalizeArtifactReq: FinalizeArtifactRequest = {
# workflowRunBackendId: backendIds.workflowRunBackendId,
# workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
# name,
# size: uploadResult.uploadSize ? uploadResult.uploadSize.toString() : '0'
# }
#
# if (uploadResult.sha256Hash) {
# finalizeArtifactReq.hash = StringValue.create({
# value: `sha256:${uploadResult.sha256Hash}`
# })
# }
#
# core.info(`Finalizing artifact upload`)
#
# const finalizeArtifactResp =
# await artifactClient.FinalizeArtifact(finalizeArtifactReq)
# if (!finalizeArtifactResp.ok) {
# throw new InvalidResponseError(
# 'FinalizeArtifact: response from backend was not ok'
# )
# }
#
# const artifactId = BigInt(finalizeArtifactResp.artifactId)
# core.info(
# `Artifact ${name}.zip successfully finalized. Artifact ID ${artifactId}`
# )
#
# return {
# size: uploadResult.uploadSize,
# digest: uploadResult.sha256Hash,
# id: Number(artifactId)
# }
#}
#