Skip to content

Publish to Registry #18

Publish to Registry

Publish to Registry #18

Workflow file for this run

name: Publish to Registry
on:
push:
tags: ["v*"]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- name: Check the tag against the manifest
id: meta
run: |
VERSION="${GITHUB_REF_NAME#v}"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
DECLARED=$(node -p "require('./package.json').version")
if [ "$DECLARED" != "$VERSION" ]; then
echo "Tag $GITHUB_REF_NAME does not match package.json version $DECLARED"
exit 1
fi
- name: Determine dist-tag
id: channel
run: |
VERSION="${{ steps.meta.outputs.version }}"
if [[ "$VERSION" == *-* ]]; then
PRERELEASE="${VERSION#*-}"
echo "tag=${PRERELEASE%%.*}" >> "$GITHUB_OUTPUT"
exit 0
fi
CURRENT=$(curl -sf -H "Authorization: Bearer ${{ secrets.REGISTRY_TOKEN }}" \
https://registry.lightside.media/com.unity.ugui 2>/dev/null \
| node -e "try{const d=JSON.parse(require('fs').readFileSync(0,'utf8'));console.log(d['dist-tags']?.latest||'')}catch{}" 2>/dev/null)
# A release below the current latest keeps its own line's tag so that `latest`
# stays on the newest uGUI line whatever order the lines are published in.
NEWEST=$(CURRENT="$CURRENT" VERSION="$VERSION" node -e "
const parse = value => value.split('.').map(Number);
const [a, b] = [parse(process.env.VERSION), parse(process.env.CURRENT || '0.0.0')];
const order = a.findIndex((part, i) => part !== b[i]);
console.log(order >= 0 && a[order] > b[order]);
")
if [ -z "$CURRENT" ] || [ "$NEWEST" = "true" ]; then
echo "tag=latest" >> "$GITHUB_OUTPUT"
else
echo "tag=v${VERSION%.*}" >> "$GITHUB_OUTPUT"
fi
- name: Pack tarball
run: npm pack
- name: Upload tarball to R2
run: |
npx wrangler r2 object put "registry-packages/com.unity.ugui-${{ steps.meta.outputs.version }}.tgz" \
--file="com.unity.ugui-${{ steps.meta.outputs.version }}.tgz" \
--content-type="application/octet-stream" \
--remote
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- name: Register metadata
run: |
MANIFEST=$(node -e "
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
pkg.dist = { tarball: 'https://registry.lightside.media/com.unity.ugui/-/com.unity.ugui-${{ steps.meta.outputs.version }}.tgz' };
console.log(JSON.stringify(pkg));
")
curl -svf -X POST "https://registry.lightside.media/-/admin/publish" \
-H "Authorization: Bearer ${{ secrets.REGISTRY_TOKEN }}" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"com.unity.ugui\",
\"version\": \"${{ steps.meta.outputs.version }}\",
\"tag\": \"${{ steps.channel.outputs.tag }}\",
\"manifest\": $MANIFEST
}"
echo ""
echo "Published com.unity.ugui@${{ steps.meta.outputs.version }} with tag=${{ steps.channel.outputs.tag }}"