name: Publish to crates.io
on:
push:
tags:
- "v*"
workflow_dispatch:
jobs:
publish:
name: Publish digdigdig3
runs-on: ubuntu-latest
environment: crates-io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust stable toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache Cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index
~/.cargo/registry/cache
~/.cargo/git/db
key: ${{ runner.os }}-cargo-publish-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-publish-
- name: Publish
shell: bash
run: |
set +e
VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
echo "Publishing digdigdig3 v${VERSION}"
# Check if already published
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "https://crates.io/api/v1/crates/digdigdig3/$VERSION")
if [ "$HTTP_CODE" = "200" ]; then
echo "v${VERSION} already on crates.io, skipping"
exit 0
fi
for attempt in 1 2 3 4 5; do
echo "Attempt $attempt..."
OUTPUT=$(cargo publish --allow-dirty 2>&1)
CODE=$?
echo "$OUTPUT"
if [ $CODE -eq 0 ]; then
echo "Published successfully!"
exit 0
fi
if echo "$OUTPUT" | grep -qE "already uploaded|already exists"; then
echo "Already on crates.io, skipping"
exit 0
fi
if echo "$OUTPUT" | grep -q "429 Too Many Requests"; then
echo "Rate limited, waiting 300s..."
sleep 300
continue
fi
if [ $attempt -lt 5 ]; then
echo "Failed (exit $CODE), retrying in 60s..."
sleep 60
fi
done
echo "FAILED after 5 attempts"
exit 1