Skip to content

Commit

Permalink
feat: automate release mirroring (#212)
Browse files Browse the repository at this point in the history
We fall behind SWC releases quite quickly
  • Loading branch information
alexeagle authored Nov 9, 2023
1 parent 9b5095b commit 90c01a0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 88 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/mirror.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Mirror Releases
on:
# Trigger manually in the UI
workflow_dispatch:
# Trigger daily at 06:50 UTC
schedule:
- cron: "50 6 * * *"

jobs:
mirror:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
./scripts/mirror_releases.sh
npx @bazel/buildifier swc/private/versions.bzl
bazel run docs:update
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
commit-message: "chore: mirror external releases"
34 changes: 0 additions & 34 deletions docs/repositories.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scripts/filter.jq
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
map(select(.tag_name | contains("nightly") | not))
|
map(
{
"key": .tag_name,
Expand Down
53 changes: 33 additions & 20 deletions scripts/mirror_releases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@

set -o errexit -o nounset -o pipefail

requested_version="${1-}"
url="https://api.github.com/repos/swc-project/swc/releases"
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
VERSIONS_BZL="$SCRIPT_DIR/../swc/private/versions.bzl"

if [[ -z "$requested_version" ]]; then
url="${url}?per_page=1"
else
# Get a lot of releases to match against.
url="${url}?per_page=50"
fi

releases=$(curl -sSL -H 'Accept: application/vnd.github.v3+json' $url | jq -f $(pwd)/scripts/filter.jq)
releases=$(curl -sSL -H 'Accept: application/vnd.github.v3+json' https://api.github.com/repos/swc-project/swc/releases?per_page=20 | jq -f "$SCRIPT_DIR/filter.jq")
versions=$(echo $releases | jq --raw-output 'keys[]')

# Combine the new versions with the existing ones.
# New versions should appear first, but existing content should overwrite new
OUT=$(mktemp)
python3 -c "import json; exec(open('$VERSIONS_BZL').read()); print(json.dumps(TOOL_VERSIONS))" > $OUT

# Sadly swc doesn't publish the checksums for their releases, so we have to compute them ourselves
EACH_VERSION=$(mktemp)
for version in $versions; do
if [[ -z "$requested_version" ]] || [[ "$requested_version" == "$version" ]]; then
echo " \"$version\": {"
assets=$(echo $releases | jq --raw-output --arg v "$version" '.["\($v)"] | keys[]')
for asset in $assets; do
url=$(echo $releases | jq --raw-output --arg v "$version" --arg a "$asset" '.["\($v)"] | .["\($a)"]')
echo " \"${asset%.exe}\": \"sha384-$(curl -sSL $url | shasum -b -a 384 | awk "{ print \$1 }" | xxd -r -p | base64)\","
done
echo " },"
fi
assets=$(echo $releases | jq --raw-output --arg v "$version" '.["\($v)"] | keys[]')

ASSETS_FOR_VERSION=""
for asset in $assets; do
url=$(echo $releases | jq --raw-output --arg v "$version" --arg a "$asset" '.["\($v)"] | .["\($a)"]')
sha=$(curl -sSL $url | shasum -b -a 384 | awk "{ print \$1 }" | xxd -r -p | base64)
ASSETS_FOR_VERSION="${ASSETS_FOR_VERSION} \"${asset%.exe}\": \"sha384-${sha}\","
done

# Remove final trailing comma so it's valid JSON
ASSETS_FOR_VERSION=${ASSETS_FOR_VERSION%,}
echo "{\"$version\": { ${ASSETS_FOR_VERSION} }}" >$EACH_VERSION

TMP=$(mktemp)
jq --slurp '.[0] * .[1]' $EACH_VERSION $OUT > $TMP
mv $TMP $OUT
done

echo "Now, paste the above output into swc/private/versions.bzl as the first entry in TOOL_VERSIONS"
# Locate the TOOL_VERSIONS declaration in the source file and replace it
NEW=$(mktemp)
sed '/TOOL_VERSIONS =/Q' $VERSIONS_BZL > $NEW
echo -n "TOOL_VERSIONS = " >> $NEW
cat $OUT >> $NEW
cat $NEW
cp $NEW $VERSIONS_BZL
34 changes: 0 additions & 34 deletions swc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,6 @@ swc_register_toolchains(
)
```
### Other versions
To use an swc version which is not mirrored to rules_swc, use `integrity_hashes`.
For example in `WORKSPACE`:
```starlark
swc_register_toolchains(
name = "swc",
integrity_hashes = {
"darwin-arm64": "sha384-IhP/76Zi5PEfsrGwPJj/CLHu2afxSBO2Fehp/qo4uHVXez08dcfyd9UzrcUI1z1q",
"darwin-x64": "sha384-s2wH7hzaMbTbIkgPpP5rAYThH/+H+RBQ/5xKbpM4lfwPMS6cNBIpjKVnathrENm/",
"linux-arm64-gnu": "sha384-iaBhMLrnHTSfXa86AVHM6zHqYbH3Fh1dWwDeH7sW9HKvX2gbQb6LOpWN6Wp4ddud",
"linux-x64-gnu": "sha384-R/y9mcodpNt8l6DulUCG5JsNMrApP+vOAAh3bTRChh6LQKP0Z3Fwq86ztfObpAH8",
},
swc_version = "v1.3.37",
)
```
You can use the [`mirror_releases.sh` script](https://github.com/aspect-build/rules_swc/blob/main/scripts/mirror_releases.sh) to generate the expected shas. For example:
```
> mirror_releases.sh v1.3.50
"v1.3.50": {
"darwin-arm64": "sha384-kXrPSxzwUCsB2y0ivQrCrBDULa+N9BwwtKzqo4hIgYmgZgBGP8cXfEWlM18Pe2mT",
"darwin-x64": "sha384-xRo3yRFsS8w5I7uWG7ZDpDiIhlJVUADpXzCWCNkYEsO4vJGD3izvTCUyWcF6HaRj",
"linux-arm-gnueabihf": "sha384-WoVw65RR2yq7fZGRpGKGDwyloteD2XjxMkqVDip2BkKuGVZMDjqldivLYx56Nhzq",
"linux-arm64-gnu": "sha384-f1pB/FU6PVYSW8KIFA799chHgXPeoaH2z8E82Mc2V21pQeJWITasy5h5wPHghZ9i",
"linux-x64-gnu": "sha384-MdR0sNOSZG4AfCBQFfqSGJ5A9Zi5mMgL7wdIeQpzqjkPICK2uDl5/MgJbO4D3kAM",
"win32-arm64-msvc": "sha384-PSmCSGrZBoFg8D+S7NqmlVr4HSedlWU2IsF0eci9jUQb+eBJeco3IO4V+IIhCiKw",
"win32-ia32-msvc": "sha384-HXRGllEV7LnLN/tgB5FfspniKG3y43C1bKIatDQIWk56gekAzm1ntV1W0qAYjz3M",
"win32-x64-msvc": "sha384-0oZDYXsh1Aeiqt9jA/HcWEM/yMXoC7fQvkPhDjUf0nVimZuPehj4BPWCyiIsrD1s",
},
```
"""

load("@bazel_skylib//lib:versions.bzl", "versions")
Expand Down

0 comments on commit 90c01a0

Please sign in to comment.