docker/rust/magic-release.sh
2026-03-06 23:01:23 +01:00

71 lines
2.6 KiB
Bash
Executable file

#!/bin/sh
set -e
# --- 1. Configuration ---
TOKEN="${PLUGIN_TOKEN}"
CRATES_TOKEN="${PLUGIN_CRATES_IO_TOKEN}"
REPO_FULL_NAME="${CI_REPO}"
API_URL="https://codeberg.org/api/v1/repos/${REPO_FULL_NAME}"
# --- 2. Get Workspace Packages in Topological Order ---
# Requires 'jq' to be installed in the environment
PACKAGES=$(cargo metadata --format-version 1 --no-deps | jq -r '.packages | .[] | "\(.name) \(.version) \(.manifest_path)"')
echo "Starting independent release process..."
# --- 3. Process each package ---
echo "$PACKAGES" | while read -r PKG_NAME PKG_VERSION MANIFEST_PATH; do
TAG_NAME="${PKG_NAME}-v${PKG_VERSION}"
echo "--- Checking $PKG_NAME ($PKG_VERSION) ---"
# A. Check if this version is already on crates.io
status_code=$(curl -s -o /dev/null -w "%{http_code}" "https://crates.io/api/v1/crates/${PKG_NAME}/${PKG_VERSION}")
if [ "$status_code" = "200" ]; then
echo " - $PKG_NAME v$PKG_VERSION is already published. Skipping."
continue
fi
echo " - New version detected. Proceeding with release..."
# B. Create Git Tag if it doesn't exist
if git rev-parse "$TAG_NAME" >/dev/null 2>&1; then
echo " - Tag $TAG_NAME already exists locally/remotely."
else
echo " - Tagging $TAG_NAME..."
git tag "$TAG_NAME"
# Use the token for authenticated push
git push "https://$TOKEN@codeberg.org/${REPO_FULL_NAME}.git" "$TAG_NAME"
fi
# C. Create Codeberg Release
# We check if the release exists via API first to avoid 409 errors
RELEASE_CHECK=$(curl -s -H "Authorization: token $TOKEN" "$API_URL/releases/tags/$TAG_NAME")
if echo "$RELEASE_CHECK" | grep -q "\"id\":"; then
echo " - Codeberg release for $TAG_NAME already exists."
else
echo " - Creating Codeberg release for $TAG_NAME..."
RELEASE_PAYLOAD=$(cat <<EOF
{
"tag_name": "$TAG_NAME",
"name": "$PKG_NAME v$PKG_VERSION",
"body": "Automated release for crate **$PKG_NAME** at version \`$PKG_VERSION\`. \n\nSee CHANGELOG.md for details.",
"draft": false,
"prerelease": false
}
EOF
)
curl -s -X 'POST' "$API_URL/releases" \
-H "Authorization: token $TOKEN" \
-H "Content-Type: application/json" \
-d "$RELEASE_PAYLOAD" > /dev/null
fi
# D. Publish to Crates.io
echo " - Publishing $PKG_NAME to crates.io..."
# --allow-dirty handles cases where the CI environment modified files (like lockfiles)
cargo publish --token "$CRATES_TOKEN" -p "$PKG_NAME" --allow-dirty
done
echo "--- All independent releases processed ---"