1
+
#!/usr/bin/env bash
2
+
set -e
3
+
4
+
# This script downloads and unzips the artifacts produced in a workflow run. It
5
+
# also checks that the workflow commit corresponds to the tag commit that these
6
+
# artifacts will be released under. The script has several pre-requisites:
7
+
# - some standard Bash tools (curl, unzip) and one slightly more rare one (jq)
8
+
# - an already-created tag in the repository (this marks the code to release)
9
+
# - the ID of a workflow run that has run successfully--this is where we
10
+
# retrieve the artifacts from
11
+
# - a GitHub access token, see https://github.com/settings/tokens
12
+
#
13
+
# Usage: download-workflow-artifacts.sh <release tag> <workflow run ID> <token>
14
+
15
+
TAG=$1
16
+
WORKFLOW_RUN_ID=$2
17
+
GITHUB_TOKEN=$3
18
+
GITHUB_API_VERSION=2022-11-28
19
+
GITHUB_API_URL=https://api.github.com/repos/WebAssembly/wasi-sdk
20
+
TMP_DIR=$(mktemp -d -t wasi-sdk-artifacts.XXXXXXX)
21
+
22
+
if [ -z "${TAG}" ] || [ -z "${WORKFLOW_RUN_ID}" ] || [ -z "${GITHUB_TOKEN}" ]; then
23
+
>&2 echo "Missing parameter; exiting..."
24
+
>&2 echo "Usage: download-worfklow-artifacts.sh <release tag> <workflow run ID> <token>"
25
+
exit 1
26
+
fi
27
+
28
+
# Get the commit SHA for the passed tag.
29
+
# See https://docs.github.com/en/rest/commits/commits#get-a-commit
30
+
MATCHING_COMMIT=$(curl \
31
+
-H "Accept: application/vnd.github+json" \
32
+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
33
+
-H "X-GitHub-Api-Version: ${GITHUB_API_VERSION}" \
34
+
"${GITHUB_API_URL}/commits/${TAG}")
35
+
COMMIT=$(echo $MATCHING_COMMIT | jq -r '.sha')
36
+
>&2 echo "===== Found commit for tag ${TAG}: ${COMMIT} ====="
37
+
38
+
# Check that the commit of the workflow run matches the tag commit and that the
39
+
# workflow was successful.
40
+
# See https://docs.github.com/en/rest/actions/workflow-runs#get-a-workflow-run
41
+
WORKFLOW_RUN=$(curl \
42
+
-H "Accept: application/vnd.github+json" \
43
+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
44
+
-H "X-GitHub-Api-Version: ${GITHUB_API_VERSION}" \
45
+
"${GITHUB_API_URL}/actions/runs/${WORKFLOW_RUN_ID}")
46
+
WORKFLOW_COMMIT=$(echo $WORKFLOW_RUN | jq -r '.head_sha')
47
+
WORKFLOW_STATUS=$(echo $WORKFLOW_RUN | jq -r '.status')
48
+
>&2 echo "===== Found commit for workflow ${WORKFLOW_RUN_ID}: ${WORKFLOW_COMMIT} ====="
49
+
if [ "${COMMIT}" != "${WORKFLOW_COMMIT}" ]; then
50
+
>&2 echo "Commit at tag ${TAG} did not match the commit for workflow ${WORKFLOW_RUN_ID}, exiting...:"
51
+
>&2 echo " ${COMMIT} != ${WORKFLOW_COMMIT}"
52
+
exit 1
53
+
fi
54
+
if [ "${WORKFLOW_STATUS}" != "completed" ]; then
55
+
>&2 echo "Workflow ${WORKFLOW_RUN_ID} did not end successfully, exiting...:"
56
+
>&2 echo " status = ${WORKFLOW_STATUS}"
57
+
exit 1
58
+
fi
59
+
60
+
# List out the artifacts in the given workflow run.
61
+
# See https://docs.github.com/en/rest/actions/artifacts#list-workflow-run-artifacts
62
+
ARTIFACTS=$(curl \
63
+
-H "Accept: application/vnd.github+json" \
64
+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
65
+
-H "X-GitHub-Api-Version: ${GITHUB_API_VERSION}" \
66
+
"${GITHUB_API_URL}/actions/runs/${WORKFLOW_RUN_ID}/artifacts" \
67
+
| jq -r '.artifacts[] | [(.id|tostring), .name, .archive_download_url] | join(",")')
68
+
69
+
for A in $ARTIFACTS; do
70
+
ID=$(echo $A | cut -d ',' -f 1)
71
+
NAME=$(echo $A | cut -d ',' -f 2)
72
+
URL=$(echo $A | cut -d ',' -f 3)
73
+
TO=$TMP_DIR/$NAME.zip
74
+
>&2 echo "===== Downloading: ${TO} ====="
75
+
76
+
# Download the artifacts to the temporary directory.
77
+
# See https://docs.github.com/en/rest/actions/artifacts#download-an-artifact
78
+
curl \
79
+
-H "Accept: application/vnd.github+json" \
80
+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
81
+
-H "X-GitHub-Api-Version: ${GITHUB_API_VERSION}" \
82
+
--location --output "${TO}" \
83
+
"${GITHUB_API_URL}/actions/artifacts/${ID}/zip"
84
+
done
85
+
86
+
# Unzip the workflow artifacts into a `release` directory.
87
+
pushd $TMP_DIR > /dev/null
88
+
mkdir release
89
+
ls -1 *.zip | xargs -n1 unzip -q -o -d release
90
+
# Some explanation:
91
+
# -1 prints each file on a separate line
92
+
# -n1 runs the command once for each item
93
+
# -q means quietly
94
+
# -o allows unzip to overwrite existing files (e.g., multiple copies of `libclang_rt.builtins-wasm32-wasi-...`)
95
+
# -d tells unzip which directory to place things in
96
+
>&2 echo "===== Files to release: ${TMP_DIR}/release ====="
97
+
>&2 ls -1 release
98
+
popd > /dev/null
99
+
100
+
>&2 echo
101
+
>&2 echo "Ensure the above artifacts look correct, then run \`draft-release.sh\` with the following directory:"
102
+
echo "${TMP_DIR}/release"
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4